Exemplo n.º 1
0
 protected void printStartElement(QName element, XMLAttributes attrs) throws XNIException {
   if (element.rawname.equalsIgnoreCase("script") && attrs != null) {
     String value = attrs.getValue("type");
     if (value != null && value.equalsIgnoreCase(SCRIPT_TYPE)) {
       String src = attrs.getValue("src");
       if (src != null && src.equals(scriptURL)) {
         attrs.setValue(attrs.getIndex("src"), zazlPath);
         String dojoDataConfigStr = attrs.getValue(attrs.getIndex("data-dojo-config"));
         configScriptTag =
             configScriptTag.replace("__DOJOCONFIG__", "{" + dojoDataConfigStr + "}");
         attrs.removeAttributeAt(attrs.getIndex("data-dojo-config"));
         Matcher m = packagesRegex.matcher(dojoDataConfigStr);
         if (m.find()) {
           String packagesString = m.group(0);
           if (packagesString != null) {
             StringBuffer sb = new StringBuffer();
             try {
               Map<String, Object> packages =
                   (Map<String, Object>)
                       JSONParser.parse(new StringReader('{' + packagesString + '}'));
               List<Map<String, Object>> packageList =
                   (List<Map<String, Object>>) packages.get("packages");
               for (Map<String, Object> pkg : packageList) {
                 String name = (String) pkg.get("name");
                 if (name.equals("zazl")) {
                   continue;
                 }
                 String location = (String) pkg.get("location");
                 location = normalize(dojoLoaderPath + location);
                 sb.append("		'");
                 sb.append(name);
                 sb.append("' : '");
                 sb.append(location);
                 sb.append("',\n");
               }
               sb.deleteCharAt(sb.length() - 1);
               sb.deleteCharAt(sb.length() - 1);
               configScriptTag = configScriptTag.replace("__PATHS__", sb.toString());
             } catch (IOException e) {
               e.printStackTrace();
             }
           }
         }
       }
       super.printStartElement(element, attrs);
     } else {
       super.printStartElement(element, attrs);
     }
   } else {
     super.printStartElement(element, attrs);
   }
 }
  protected boolean register() {
    if (!registered && httpService != null) {
      InputStream is = null;
      List<String> bundleIdList = new ArrayList<String>(Arrays.asList(defaultBundleIds));
      Bundle b = null;
      String bundleId = System.getProperty("zazl.idsprovider");
      if (bundleId != null) {
        bundleIdList.add(bundleId);
        b = findBundle(bundleId);
      }
      if (b == null) {
        b = context.getBundle();
      }
      try {
        is = b.getResource("bundleids.json").openStream();
        Reader reader = new InputStreamReader(is);
        bundleIdList.addAll((List<String>) JSONParser.parse(reader));
      } catch (Throwable e) {
        e.printStackTrace();
      } finally {
        if (is != null) {
          try {
            is.close();
          } catch (IOException e) {
          }
        }
      }
      String[] bundleIds = new String[bundleIdList.size()];
      bundleIds = bundleIdList.toArray(bundleIds);
      ResourceLoader resourceLoader = new OSGiResourceLoader(context, bundleIds);
      RhinoClassLoader rhinoClassLoader = new RhinoClassLoader(resourceLoader);
      JSCompressorFactory jsCompressorFactory = null;
      Boolean jscompress = Boolean.valueOf(System.getProperty("zazl.jscompress", "false"));
      if (jscompress) {
        jsCompressorFactory = new ClosureJSCompressorFactory();
      }
      JSOptimizerFactory jsOptimizerFactory = new AMDJSOptimizerFactory();

      JSServlet jsServlet =
          new JSServlet(
              resourceLoader,
              jsOptimizerFactory,
              rhinoClassLoader,
              "zazl",
              null,
              null,
              jsCompressorFactory,
              true);
      ResourceServlet resourceServlet = new ResourceServlet(resourceLoader);
      try {
        System.out.println("Registering Zazl JavaScript servlet");
        httpService.registerServlet("/_javascript", jsServlet, null, null);
        System.out.println("Registering Resource servlet");
        httpService.registerServlet("/", resourceServlet, null, null);
        registered = true;
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return registered;
  }