Example #1
0
  /**
   * Compress a list of js files into one combined string
   *
   * @param a list of js files
   * @return a string that contains all the compressed data
   * @throws EvaluatorException
   * @throws IOException
   */
  private static String compressJS(Collection<IDiagramPlugin> plugins, ServletContext context) {
    StringWriter sw = new StringWriter();
    for (IDiagramPlugin plugin : plugins) {
      sw.append("/* ").append(plugin.getName()).append(" */\n");
      InputStream input = plugin.getContents();
      try {
        JavaScriptCompressor compressor =
            new JavaScriptCompressor(new InputStreamReader(input), null);
        compressor.compress(sw, -1, false, false, false, false);
      } catch (EvaluatorException e) {
        _logger.error(e.getMessage(), e);
      } catch (IOException e) {
        _logger.error(e.getMessage(), e);
      } finally {
        try {
          input.close();
        } catch (IOException e) {
        }
      }

      sw.append("\n");
    }
    return sw.toString();
  }
Example #2
0
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    Document doc = (Document) _doc.clone();
    String profileName = request.getParameter("profile");
    if (profileName == null || profileName.length() < 1) {
      // default to jbpm
      profileName = "jbpm";
    }
    IDiagramProfile profile = _profileService.findProfile(request, profileName);
    if (profile == null) {
      _logger.error("No profile with the name " + profileName + " was registered");
      throw new IllegalArgumentException(
          "No profile with the name " + profileName + " was registered");
    }

    IDiagramPreprocessingUnit preprocessingUnit = null;
    if (_preProcess) {
      if (_logger.isInfoEnabled()) {
        _logger.info("Performing diagram information pre-processing steps. ");
      }
      preprocessingUnit = _preProcessingService.findPreprocessingUnit(request, profile);
      preprocessingUnit.preprocess(request, response, profile);
    }

    // output env javascript files
    if (_devMode) {
      for (String jsFile : _envFiles) {
        addScript(doc, oryx_path + jsFile, true);
      }
    } else {
      addScript(doc, oryx_path + "jsc/env_combined.js", true);
    }

    // generate script tags for plugins.
    // they are located after the initialization script.

    if (_pluginfiles.get(profileName) == null) {
      List<IDiagramPlugin> compressed = new ArrayList<IDiagramPlugin>();
      List<IDiagramPlugin> uncompressed = new ArrayList<IDiagramPlugin>();
      _pluginfiles.put(profileName, compressed);
      _uncompressedPlugins.put(profileName, uncompressed);
      for (String pluginName : profile.getPlugins()) {
        IDiagramPlugin plugin = _pluginService.findPlugin(request, pluginName);
        if (plugin == null) {
          _logger.warn(
              "Could not find the plugin "
                  + pluginName
                  + " requested by the profile "
                  + profile.getName());
          continue;
        }
        if (plugin.isCompressable()) {
          compressed.add(plugin);
        } else {
          uncompressed.add(plugin);
        }
      }

      if (!_devMode) {
        // let's call the compression routine
        String rs = compressJS(_pluginfiles.get(profileName), getServletContext());
        try {
          FileWriter w =
              new FileWriter(getServletContext().getRealPath("jsc/plugins_" + profileName + ".js"));
          w.write(rs.toString());
          w.close();
        } catch (Exception e) {
          _logger.error(e.getMessage(), e);
        }
      }
    }

    if (_devMode) {
      for (IDiagramPlugin jsFile : _pluginfiles.get(profileName)) {
        addScript(doc, oryx_path + "plugin/" + jsFile.getName() + ".js", true);
      }
    } else {
      addScript(doc, oryx_path + "jsc/plugins_" + profileName + ".js", false);
    }

    for (IDiagramPlugin uncompressed : _uncompressedPlugins.get(profileName)) {
      addScript(doc, oryx_path + "plugin/" + uncompressed.getName() + ".js", false);
    }

    // send the updated editor.html to client
    if (!isIE(request)) {
      response.setContentType("application/xhtml+xml");
    }
    XMLOutputter outputter = new XMLOutputter();
    Format format = Format.getPrettyFormat();
    format.setExpandEmptyElements(true);
    outputter.setFormat(format);
    String html = outputter.outputString(doc);
    StringTokenizer tokenizer = new StringTokenizer(html, "@", true);
    StringBuilder resultHtml = new StringBuilder();
    boolean tokenFound = false;
    boolean replacementMade = false;

    IDiagramPreference pref = PREFERENCE_FACTORY.createPreference(request);
    int autoSaveInt = pref.getAutosaveInterval();
    boolean autoSaveOn = pref.isAutoSaveEnabled();

    while (tokenizer.hasMoreTokens()) {
      String elt = tokenizer.nextToken();
      if ("title".equals(elt)) {
        resultHtml.append(profile.getTitle());
        replacementMade = true;
      } else if ("stencilset".equals(elt)) {
        resultHtml.append(profile.getStencilSet());
        replacementMade = true;
      } else if ("debug".equals(elt)) {
        resultHtml.append(_devMode);
        replacementMade = true;
      } else if ("autosaveinterval".equals(elt)) {
        resultHtml.append(autoSaveInt);
        replacementMade = true;
      } else if ("autosavedefault".equals(elt)) {
        resultHtml.append(autoSaveOn);
        replacementMade = true;
      } else if ("preprocessing".equals(elt)) {
        resultHtml.append(preprocessingUnit == null ? "" : preprocessingUnit.getOutData());
        replacementMade = true;
      } else if ("externalprotocol".equals(elt)) {
        resultHtml.append(ExternalInfo.getExternalProtocol(profile));
        replacementMade = true;
      } else if ("externalhost".equals(elt)) {
        resultHtml.append(ExternalInfo.getExternalHost(profile));
        replacementMade = true;
      } else if ("externalsubdomain".equals(elt)) {
        resultHtml.append(
            profile
                .getExternalLoadURLSubdomain()
                .substring(0, profile.getExternalLoadURLSubdomain().indexOf("/")));
        replacementMade = true;
      } else if ("designerversion".equals(elt)) {
        resultHtml.append(_designerVersion);
        replacementMade = true;
      } else if ("profileplugins".equals(elt)) {
        StringBuilder plugins = new StringBuilder();
        boolean commaNeeded = false;
        for (String ext : profile.getPlugins()) {
          if (commaNeeded) {
            plugins.append(",");
          } else {
            commaNeeded = true;
          }
          plugins.append("\"").append(ext).append("\"");
        }
        resultHtml.append(plugins.toString());
        replacementMade = true;
      } else if ("ssextensions".equals(elt)) {
        StringBuilder ssexts = new StringBuilder();
        boolean commaNeeded = false;
        for (String ext : profile.getStencilSetExtensions()) {
          if (commaNeeded) {
            ssexts.append(",");
          } else {
            commaNeeded = true;
          }
          ssexts.append("\"").append(ext).append("\"");
        }
        resultHtml.append(ssexts.toString());
        replacementMade = true;
      } else if ("@".equals(elt)) {
        if (replacementMade) {
          tokenFound = false;
          replacementMade = false;
        } else {
          tokenFound = true;
        }
      } else {
        if (tokenFound) {
          tokenFound = false;
          resultHtml.append("@");
        }
        resultHtml.append(elt);
      }
    }

    response.getWriter().write(resultHtml.toString());
  }