예제 #1
0
  private static HashSet<String> getJSRelativePaths(HashSet<String> fullPaths) {
    String relPath = null;
    HashSet<String> relativePaths = new HashSet<String>();

    for (String path : fullPaths) {
      relPath =
          new File(SessionManager.getInstance().getSourceFolder())
              .toURI()
              .relativize(new File(path).toURI())
              .getPath();
      relativePaths.add(relPath);
    }

    return relativePaths;
  }
예제 #2
0
  public WidgetAccess(String uri, boolean allowSubDomain) throws Exception {
    try {
      _uri = URI.create(uri);
      // Check for a protocol
      if (!(_uri.toString().equals("WidgetConfig.WIDGET_LOCAL_DOMAIN"))
          && (_uri.getScheme() == null || _uri.getScheme().length() == 0)) {
        throw new ValidationException("EXCEPTION_ACCESSURI_NO_PROTOCOL", uri.toString());
      }
      _allowSubDomain = allowSubDomain;

      String host = _uri.getHost();
      if (host != null
          && SessionManager.getInstance().getTLD().indexOf("$$" + host.toLowerCase().trim() + "$$")
              != -1
          && _allowSubDomain) {
        // Throw exception - exit compilation
        throw new ValidationException("EXCEPTION_CONFIGXML_TLD", uri);
      }
    } catch (IllegalArgumentException e) {
      throw new ValidationException("EXCEPTION_ACCESSURI_BADURI", uri.toString());
    }
  }
예제 #3
0
 private static String getTempExtensionPath() {
   return SessionManager.getInstance().getSourceFolder() + TEMPORARY_DIRECTORY + File.separator;
 }
예제 #4
0
 private static String getJavaScriptPath() {
   return SessionManager.getInstance().getSourceFolder()
       + File.separator
       + JS_DIRECTORY
       + File.separator;
 }
예제 #5
0
 private static String getExtensionPath() {
   return SessionManager.getInstance().getSourceFolder()
       + File.separator
       + EXTENSION_DIRECTORY
       + File.separator;
 }
예제 #6
0
  /**
   * Parses XML from the specified input file, replaces the text content of the &lt;content&gt; with
   * the specified replacement string, and writes the result to the specified output file. If the
   * &lt;content&gt; element is not found in the expected location, this method is at liberty to do
   * nothing.
   *
   * @param infile the input XML file, expected to be in app XML format.
   * @param outfile the destination file, to hold the result.
   * @param replacementText the replacement string.
   * @exception java.io.IOException if an i/o error occurs.
   * @throws ValidationException
   */
  private void prepareAppXML(File infile, File outfile, String replacementText)
      throws IOException, ValidationException {
    Writer w = null;
    try {
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document d = db.parse(infile);

      Element e = d.getDocumentElement();

      if (e != null) {
        // Replace initialWindow/content
        NodeList nl = e.getElementsByTagName(DOC_ELM_INITIALWINDOW);
        if (nl.getLength() > 0 && nl.item(0) instanceof Element) {
          Element e2 = (Element) nl.item(0);

          NodeList nl2 = e2.getElementsByTagName(DOC_ELM_CONTENT);
          if (nl2.getLength() > 0 && nl2.item(0) instanceof Element) {
            Element e3 = (Element) nl2.item(0);
            e3.setTextContent(replacementText);
          }

          // Add AutoOrientation
          if (_widgetConfig.getAutoOrientation() != null
              && _widgetConfig.getAutoOrientation().length() > 0) {
            Element autoOrientation = d.createElement(DOC_ELM_AUTOORIENTS);
            autoOrientation.setTextContent(_widgetConfig.getAutoOrientation());
            e2.appendChild(autoOrientation);
          }

          // Add Orientation
          if (_widgetConfig.getOrientation() != null
              && _widgetConfig.getOrientation().length() > 0) {
            Element orientation = d.createElement(DOC_ELM_ASPECTRATIO);
            orientation.setTextContent(_widgetConfig.getOrientation());
            e2.appendChild(orientation);
          }
        }

        // Replace id
        NodeList nl2 = e.getElementsByTagName(DOC_ELM_ID);
        if (nl2.getLength() > 0 && nl2.item(0) instanceof Element) {
          Element e3 = (Element) nl2.item(0);
          e3.setTextContent(
              SessionManager.getInstance().getArchiveName()
                  + genPackageName(SessionManager.getInstance().getArchiveName()));
        }

        // Replace name
        nl2 = e.getElementsByTagName(DOC_ELM_NAME);
        if (nl2.getLength() > 0 && nl2.item(0) instanceof Element) {
          Element e3 = (Element) nl2.item(0);
          e3.setTextContent(_widgetConfig.getName());
        }

        // Replace version
        nl2 = e.getElementsByTagName(DOC_ELM_VERSIONNUMBER);
        if (nl2.getLength() > 0 && nl2.item(0) instanceof Element) {
          Element e3 = (Element) nl2.item(0);
          e3.setTextContent(
              _widgetConfig.getVersionParts(0, 3)); // AIR supports only 3-part version strings
        }

        // Add description
        if (_widgetConfig.getDescription() != null && _widgetConfig.getDescription().length() > 0) {
          Element description = d.createElement(DOC_ELM_DESCRIPTION);
          description.setTextContent(_widgetConfig.getDescription());
          Node root = d.getFirstChild();
          root.appendChild(description);
        }

        // Add copyright
        if (_bbwpProperties.getCopyright().length() > 0) {
          Element copyright = d.createElement(DOC_ELM_COPYRIGHT);
          copyright.setTextContent(_bbwpProperties.getCopyright());
          Node root = d.getFirstChild();
          root.appendChild(copyright);
        }
      }

      TransformerFactory tf = TransformerFactory.newInstance();
      Transformer t = tf.newTransformer();
      DOMSource s = new DOMSource(d);
      w = new FileWriter(outfile);
      StreamResult r = new StreamResult(w);
      t.transform(s, r);
    } catch (ParserConfigurationException pce) {
      throw new IOException(pce);
    } catch (SAXException se) {
      throw new IOException(se);
    } catch (TransformerConfigurationException tce) {
      throw new IOException(tce);
    } catch (TransformerException te) {
      throw new IOException(te);
    } finally {
      if (w != null) {
        try {
          w.close();
        } catch (IOException ioe) {
        }
      }
    }
  }
예제 #7
0
  /**
   * Packages the files in the specified source path, using the AIR packager to create a BAR file.
   *
   * @param sourcePath the folder containing the files to be packaged.
   * @param archiveName the name of the Widget archive.
   * @throws ValidationException
   */
  public int run() throws PackageException, ValidationException {
    try {
      String sourcePath = SessionManager.getInstance().getSourceFolder();
      String bindebugPath = sourcePath + File.separator + PATH_BIN_DEBUG;
      String archiveName = SessionManager.getInstance().getArchiveName();

      //
      // First we copy the files in the source path to the bin-debug folder.
      //
      deleteDirectory(new File(bindebugPath));
      new File(bindebugPath).mkdir(); // just in case

      List<File> fileList = new ArrayList<File>();
      File desFile;

      // Check if the icon file actually exists
      // Needs to be done first in order to avoid putting the icon.png filename into the fileList
      String iconPath = EMPTY_STRING;
      if (_widgetConfig.getIconSrc().size() > 0) {
        iconPath = _widgetConfig.getIconSrc().firstElement().toString();
        File iconFile = new File(sourcePath, iconPath);
        if (!iconFile.isFile()) {
          iconPath = EMPTY_STRING;
        } else {
          iconPath = PATH_ICON_APPICON_PNG;
          iconFile.renameTo(new File(sourcePath, iconPath));
        }
      }

      //
      // Create a splash screen consistent with the loading screen.
      // If the widget config doesn't specify loading screen data,
      // splashscreenFilename will be null.
      //
      String splashscreenFilename = createSplashscreen(sourcePath);

      //
      // Copy src files to the bin-debug folder
      //
      List<File> srcFiles =
          listFiles(
              sourcePath,
              new FileFilter() {
                public boolean accept(File pathname) {
                  return !pathname.getName().endsWith(FILE_EXT_AS)
                      && !pathname.getName().endsWith(APP_XML_SUFFIX)
                      && !pathname
                          .getName()
                          .equals(
                              SessionManager.getInstance().getArchiveName() + SWF_FILE_EXTENSION)
                      && !pathname.getName().contains(PATH_MACOSX);
                }
              });

      for (File f : srcFiles) {
        String relativePath = f.getAbsolutePath().substring(sourcePath.length() + 1);
        desFile = new File(bindebugPath, relativePath);
        FileManager.copyFile(f, desFile);
      }

      // Add the top level file/folder under bin-debug folder to the file list,
      // so it will greatly shorten the length of final command line
      File[] archiveFiles =
          new File(bindebugPath)
              .listFiles(
                  new FileFilter() {
                    // APP_XML_SUFFIX and SWF_FILE_EXTENSION will be added afterwards
                    public boolean accept(File pathname) {
                      return !pathname.getName().endsWith(APP_XML_SUFFIX)
                          && !pathname
                              .getName()
                              .endsWith(
                                  SessionManager.getInstance().getArchiveName()
                                      + SWF_FILE_EXTENSION)
                          && !pathname.getName().contains(PATH_MACOSX);
                    }
                  });

      for (File f : archiveFiles) {
        fileList.add(f);
      }

      //
      // Copy the SWF
      //
      String swfName = archiveName + SWF_FILE_EXTENSION;
      desFile = new File(bindebugPath, swfName);
      FileManager.copyFile(new File(sourcePath, swfName), desFile);
      fileList.add(desFile);

      //
      // Copy and replace the text in the app XML with the name of the SWF file.
      // Prepare the complete version of app.xml including a custom icon.
      //

      String appXmlName = archiveName + APP_XML_SUFFIX;
      desFile = new File(bindebugPath, appXmlName);
      prepareAppXML(new File(sourcePath, FILE_WEBWORKSAPPTEMPLATE_APP_XML), desFile, swfName);
      fileList.add(desFile);

      File bbt = new File(sourcePath, FILE_BLACKBERRY_TABLET_XML);
      File bbtDes = new File(bindebugPath, FILE_BLACKBERRY_TABLET_XML);
      prepareBBTXML(bbt, bbtDes, iconPath, splashscreenFilename);

      bbt.delete();

      int size = fileList.size();
      String[] files = new String[size];
      int i = 2;
      for (File f : fileList) {
        String relativePath = f.getAbsolutePath().substring(bindebugPath.length() + 1);
        if (relativePath.equalsIgnoreCase(appXmlName)) {
          files[0] = relativePath;
        } else if (relativePath.equalsIgnoreCase(swfName)) {
          files[1] = relativePath;
        } else {
          files[i++] = relativePath;
        }
      }

      //
      // Now we can package it all.
      //
      String outputFolder = SessionManager.getInstance().getOutputFolder();
      new File(outputFolder).mkdirs();
      String outputPath =
          SessionManager.getInstance()
              .getOutputFilepath(); // _airTemplatePath + File.separator + archiveName +
      // BAR_FILE_EXTENSION

      //
      // For AIR, the build number is specified separately from the
      // version string using the -buildId parameter.
      //
      // For signing, the bbwp command-line interface supports -buildId
      // as an override.
      //
      String buildId;
      String buildIdOverride = SessionManager.getInstance().getBuildId();
      if (!buildIdOverride.isEmpty()) {
        buildId = buildIdOverride;
      } else if (_widgetConfig.getNumVersionParts() > 3) {
        buildId = _widgetConfig.getVersionParts(3);
      } else {
        buildId = NUM_0;
      }

      String debugToken = _bbwpProperties.getDebugToken();
      String[] cmd;
      if (SessionManager.getInstance().requireSigning()) {
        cmd =
            new String[] {
              _airPackagerPath,
              FLAG_PACKAGE,
              FLAG_TARGET,
              FILE_EXT_BAR,
              FLAG_BUILDID,
              buildId,
              outputPath
            };
      } else if (!SessionManager.getInstance().debugMode() || debugToken.isEmpty()) {
        cmd =
            new String[] {
              _airPackagerPath,
              FLAG_PACKAGE,
              FLAG_DEV_MODE,
              FLAG_TARGET,
              SessionManager.getInstance().debugModeInternal() ? PATH_BAR_DEBUG : FILE_EXT_BAR,
              FLAG_BUILDID,
              buildId,
              outputPath
            };
      } else {
        if (!(new File(debugToken).isFile())) {
          //
          // It is an error for the <debug_token> element to
          // contain a pathname that does not point to a file.
          //
          throw new PackageException(EXCEPTION_DEBUG_TOKEN_INVALID);
        } else {
          cmd =
              new String[] {
                _airPackagerPath,
                FLAG_PACKAGE,
                FLAG_DEV_MODE,
                FLAG_DEBUG_TOKEN,
                debugToken,
                FLAG_TARGET,
                SessionManager.getInstance().debugModeInternal() ? PATH_BAR_DEBUG : FILE_EXT_BAR,
                FLAG_BUILDID,
                buildId,
                outputPath
              };
        }
      }
      int n = files.length;
      String[] join = new String[cmd.length + n];
      System.arraycopy(cmd, 0, join, 0, cmd.length);
      System.arraycopy(files, 0, join, cmd.length, n);
      Process p = buildProcess(join, new File(bindebugPath));

      OutputBuffer stdout = new OutputBuffer(p);
      ErrorBuffer stderr = new ErrorBuffer(p);
      ExitBuffer exitcode = new ExitBuffer(p);

      stdout.waitFor();
      stderr.waitFor();
      exitcode.waitFor();

      if (exitcode.getExitValue().intValue() != 0) {
        System.out.write(stderr.getStderr());
        System.out.write(stdout.getStdout());
        System.out.flush();
        return exitcode.getExitValue().intValue();
      }
    } catch (IOException ioe) {
      ioe.printStackTrace();
      throw new PackageException(EXCEPTION_AIRPACKAGER);
    } catch (InterruptedException ie) {
      throw new PackageException(EXCEPTION_AIRPACKAGER);
    }
    return 0;
  }