public static void main(String[] args) throws Exception {
    // The path to the documents directory.
    gDataDir = Utils.getDataDir(KeepSourceTogether.class);

    Document dstDoc = new Document(gDataDir + "TestFile.Destination.doc");
    Document srcDoc = new Document(gDataDir + "TestFile.Source.doc");

    // Set the source document to appear straight after the destination document's content.
    srcDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);

    // Iterate through all sections in the source document.
    for (Paragraph para : (Iterable<Paragraph>) srcDoc.getChildNodes(NodeType.PARAGRAPH, true)) {
      para.getParagraphFormat().setKeepWithNext(true);
    }

    dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
    dstDoc.save(gDataDir + "TestDcc.KeepSourceTogether Out.doc");

    System.out.println("Documents appended successfully.");
  }
Exemplo n.º 2
0
      public void onDocument(Document document, String xpath) throws XmlParserException {
        if (xpath.equals(XPATH_IPEAK)) {
          Node node = document.getChildNodes().item(0);

          // check whether we're getting the correct ipeak
          Node typeattribute = node.getAttributes().getNamedItem(PeakMLWriter.TYPE);
          if (typeattribute == null)
            throw new XmlParserException("Failed to locate the type attribute.");
          if (!typeattribute.getNodeValue().equals(PeakMLWriter.TYPE_MASSCHROMATOGRAM))
            throw new XmlParserException(
                "IPeak ("
                    + typeattribute.getNodeValue()
                    + ") is not of type: '"
                    + PeakMLWriter.TYPE_MASSCHROMATOGRAM
                    + "'");

          // parse this node as a mass chromatogram
          MassChromatogram<? extends Peak> masschromatogram = parseMassChromatogram(node);
          if (masschromatogram != null) peaks.add(masschromatogram);

          //
          if (_listener != null && result.header != null && result.header.getNrPeaks() != 0)
            _listener.update((100. * index++) / result.header.getNrPeaks());
        } else if (xpath.equals(XPATH_HEADER)) {
          result.header = parseHeader(document.getFirstChild());
        }
      }
  // Gets the specified XML Schema doc if one is mentioned in the file
  public static File getSchemaFile(Document xmlDoc) {
    /** @todo Must be an easier way of doing this... */
    logger.logComment("Getting schema file for: " + xmlDoc.getDocumentURI());

    NodeList nl = xmlDoc.getChildNodes();
    for (int j = 0; j < nl.getLength(); j++) {
      Node node = nl.item(j);
      logger.logComment("Type: " + node.getNodeType() + "Name: " + node.getNodeName());
      if (node.getNodeName().equals(XML_STYLESHEET_NODE)) {
        String nodeVal = node.getNodeValue();

        logger.logComment("Looking at: " + nodeVal);
        String xslFileName =
            nodeVal.substring(nodeVal.indexOf("href=\"") + 6, nodeVal.length() - 1);
        File xslFile = new File(xslFileName);
        return xslFile;
      }

      if (node.getAttributes().getLength() > 0) {
        logger.logComment("Attributes: " + node.getAttributes());
        if (node.getAttributes().getNamedItem(XML_SCHEMA_LOC_ATTR) != null) {
          String locString = node.getAttributes().getNamedItem(XML_SCHEMA_LOC_ATTR).getNodeValue();
          logger.logComment("Loc string: " + locString);
          String file = locString.split("\\s")[1];
          return new File(file);
        }
      }
    }
    logger.logError("No node found with name: " + XML_STYLESHEET_NODE);
    return null;
  }
Exemplo n.º 4
0
  static {
    try {
      URL url = SpellCheckActivator.bundleContext.getBundle().getResource(RESOURCE_LOC);

      InputStream stream = url.openStream();

      if (stream == null) throw new IOException();

      // strict parsing options
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

      factory.setValidating(false);
      factory.setIgnoringComments(true);
      factory.setIgnoringElementContentWhitespace(true);

      // parses configuration xml
      /*-
       * Warning: Felix is unable to import the com.sun.rowset.internal
       * package, meaning this can't use the XmlErrorHandler. This causes
       * a warning and a default handler to be attached. Otherwise this
       * should have: builder.setErrorHandler(new XmlErrorHandler());
       */
      DocumentBuilder builder = factory.newDocumentBuilder();
      Document doc = builder.parse(stream);

      // iterates over nodes, parsing contents
      Node root = doc.getChildNodes().item(1);

      NodeList categories = root.getChildNodes();

      for (int i = 0; i < categories.getLength(); ++i) {
        Node node = categories.item(i);
        if (node.getNodeName().equals(NODE_DEFAULTS)) {
          parseDefaults(node.getChildNodes());
        } else if (node.getNodeName().equals(NODE_LOCALES)) {
          parseLocales(node.getChildNodes());
        } else {
          logger.warn("Unrecognized category: " + node.getNodeName());
        }
      }
    } catch (IOException exc) {
      logger.error("Unable to load spell checker parameters", exc);
    } catch (SAXException exc) {
      logger.error("Unable to parse spell checker parameters", exc);
    } catch (ParserConfigurationException exc) {
      logger.error("Unable to parse spell checker parameters", exc);
    }
  }
Exemplo n.º 5
0
      public void onDocument(Document document, String xpath) throws XmlParserException {
        if (xpath.equals(XPATH_IPEAK)) {
          Node node = document.getChildNodes().item(0);

          // check whether we're getting the correct ipeak
          Node typeattribute = node.getAttributes().getNamedItem(PeakMLWriter.TYPE);
          if (typeattribute == null)
            throw new XmlParserException("Failed to locate a type attribute.");

          // ...
          //					IPeak peak = (_loadall ? parseIPeak(node) : parseCentroid(node));
          IPeak peak = parseIPeak(node);
          if (peak != null) _listener.onIPeak(peak);
        } else if (xpath.equals(XPATH_HEADER)) {
          Header header = parseHeader(document.getFirstChild());
          _listener.onHeader(header);
        }
      }
Exemplo n.º 6
0
      public void onDocument(Document document, String xpath) throws XmlParserException {
        if (xpath.equals(XPATH_IPEAK)) {
          Node node = document.getChildNodes().item(0);

          // check whether we're getting the correct ipeak
          Node typeattribute = node.getAttributes().getNamedItem(PeakMLWriter.TYPE);
          if (typeattribute == null
              || !typeattribute.getNodeValue().equals(PeakMLWriter.TYPE_PEAKSET))
            throw new XmlParserException("Failed to locate a type attribute.");

          // parse this node as a mass chromatogram
          IPeakSet<? extends IPeak> peakset = parsePeakSet(node);
          if (peakset != null) peaks.add(peakset);

          //
          if (_listener != null && result.header != null && result.header.getNrPeaks() != 0)
            _listener.update((100. * index++) / result.header.getNrPeaks());
        } else if (xpath.equals(XPATH_HEADER)) {
          result.header = parseHeader(document.getFirstChild());
        }
      }
Exemplo n.º 7
0
  private static void showDocument(Document doc) {
    StringBuffer content = new StringBuffer();
    Node node = doc.getChildNodes().item(0);
    ApplicationNode appNode = new ApplicationNode(node);

    content.append("Application \n");

    List<ClassNode> classes = appNode.getClasses();

    for (int i = 0; i < classes.size(); i++) {
      ClassNode classNode = classes.get(i);
      content.append(SPACE + "Class: " + classNode.getName() + " \n");

      List<MethodNode> methods = classNode.getMethods();

      for (int j = 0; j < methods.size(); j++) {
        MethodNode methodNode = methods.get(j);
        content.append(SPACE + SPACE + "Method: " + methodNode.getName() + " \n");
      }
    }

    System.out.println(content.toString());
  }