@Override
  protected int nextChild() {
    ElementFrame<Node, Node> frame = getCurrentFrame();
    if (frame.currentChild == null) {
      content = getCurrentNode().getFirstChild();
    } else {
      content = frame.currentChild.getNextSibling();
    }

    frame.currentChild = content;
    switch (content.getNodeType()) {
      case Node.ELEMENT_NODE:
        return START_ELEMENT;
      case Node.TEXT_NODE:
        return CHARACTERS;
      case Node.COMMENT_NODE:
        return COMMENT;
      case Node.CDATA_SECTION_NODE:
        return CDATA;
      case Node.ENTITY_REFERENCE_NODE:
        return ENTITY_REFERENCE;
      default:
        throw new IllegalStateException("Found type: " + content.getClass().getName());
    }
  }
 public Node adoptNode(Node arg0) throws DOMException {
   // dynamic load to support jre 1.4 and 1.5
   try {
     Method m = doc.getClass().getMethod("adoptNode", new Class[] {arg0.getClass()});
     return Caster.toNode(m.invoke(doc, new Object[] {arg0}));
   } catch (Exception e) {
     throw new PageRuntimeException(Caster.toPageException(e));
   }
 }
  public static void main(String[] args) throws Throwable {

    final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

    dbFactory.setNamespaceAware(true);
    dbFactory.setXIncludeAware(true); // turn on xinclude
    final DocumentBuilder parser = dbFactory.newDocumentBuilder();

    final Document document = parser.parse(new File("xml/people-xinclude.xml"));

    System.out.println("Parsed/Validated without Exceptions");

    // Use XPath to get list of Person from the Document
    XPath xpath = XPathFactory.newInstance().newXPath();
    String expression = "/include-demo/people/person/name";
    NodeList list = (NodeList) xpath.evaluate(expression, document, XPathConstants.NODESET);
    for (int i = 0; i < list.getLength(); i++) {
      Node personName = list.item(i);
      System.out.printf("Person = %s (a %s)%n", personName.getTextContent(), personName.getClass());
    }
  }
Exemple #4
0
  private static CTTextParagraphProperties unmarshalFormatting(NodeIterator lvlNpPr) {

    // Get the pPr node as a JAXB object,
    // so we can read it using our standard
    // methods.  Its a bit sad that we
    // can't just adorn our DOM tree with the
    // original JAXB objects?
    try {
      // CTTextListStyle lstStyle = null;
      CTTextParagraphProperties pPr = null;

      if (lvlNpPr != null) {
        Node n = lvlNpPr.nextNode();

        log.debug(n.getClass().getName());

        String str = XmlUtils.w3CDomNodeToString(n);
        // log.debug("'" + str + "'");

        // Convert to String first ...
        // unmarshalling the node directly doesn't work as expected
        // (see comment in XmlUtils)

        //			if (n!=null) {
        //				return  (CTTextParagraphProperties)XmlUtils.unmarshal(n, Context.jcPML,
        //						CTTextParagraphProperties.class);
        //			}

        if (!str.equals("")) {
          return (CTTextParagraphProperties)
              XmlUtils.unmarshalString(str, Context.jcPML, CTTextParagraphProperties.class);
        }
      }
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return null;
  }
 void doApply(
     Stylesheet stylesheet,
     QName mode,
     Node context,
     int pos,
     int len,
     Node parent,
     Node nextSibling)
     throws TransformerException {
   Node result = null;
   Document doc = (parent instanceof Document) ? (Document) parent : parent.getOwnerDocument();
   short nodeType = source.getNodeType();
   if (nodeType == Node.ATTRIBUTE_NODE && parent.getFirstChild() != null) {
     // Ignore attributes added after child elements
   } else {
     // Namespace aliasing
     if (nodeType == Node.ELEMENT_NODE) {
       String prefix = source.getPrefix();
       if (prefix == null) prefix = "#default";
       String resultPrefix = (String) stylesheet.namespaceAliases.get(prefix);
       if (resultPrefix != null) {
         if ("#default".equals(resultPrefix)) resultPrefix = null;
         String uri = source.lookupNamespaceURI(resultPrefix);
         String name = source.getNodeName();
         // Create a new element node in the result document
         result = doc.createElementNS(uri, name);
         // copy attributes
         NamedNodeMap srcAttrs = source.getAttributes();
         NamedNodeMap dstAttrs = result.getAttributes();
         int l = srcAttrs.getLength();
         for (int i = 0; i < l; i++) {
           Node attr = srcAttrs.item(i);
           if (!Stylesheet.XSL_NS.equals(attr.getNamespaceURI())) {
             attr = attr.cloneNode(true);
             attr = doc.adoptNode(attr);
             dstAttrs.setNamedItemNS(attr);
           }
         }
       }
     }
     if (result == null) {
       // Create result node
       result = source.cloneNode(false);
       // Remove any XSL attributes
       NamedNodeMap attrs = result.getAttributes();
       if (attrs != null) {
         int l = attrs.getLength();
         for (int i = 0; i < l; i++) {
           Node attr = attrs.item(i);
           if (Stylesheet.XSL_NS.equals(attr.getNamespaceURI())) {
             attrs.removeNamedItem(attr.getNodeName());
             i--;
             l--;
           }
         }
       }
       Node result2 = doc.adoptNode(result);
       if (result2 == null) {
         String msg =
             "Error adopting node to result tree: "
                 + result
                 + " ("
                 + result.getClass().getName()
                 + ")";
         DOMSourceLocator l = new DOMSourceLocator(context);
         throw new TransformerException(msg, l);
       }
       result = result2;
     }
     if (nextSibling != null) parent.insertBefore(result, nextSibling);
     else parent.appendChild(result);
     if (nodeType == Node.ELEMENT_NODE)
       stylesheet.addNamespaceNodes(source, result, doc, elementExcludeResultPrefixes);
     // children
     if (children != null) children.apply(stylesheet, mode, context, pos, len, result, null);
   }
   // next sibling
   if (next != null) next.apply(stylesheet, mode, context, pos, len, parent, nextSibling);
 }
Exemple #6
0
  /**
   * Create a new HTML element with the given tag name.
   *
   * @param tagName the tag name
   * @return the new HTML element, or NOT_FOUND if the tag is not supported
   */
  @JsxFunction
  public Object createElement(String tagName) {
    Object result = NOT_FOUND;
    try {
      final BrowserVersion browserVersion = getBrowserVersion();

      // FF3.6 supports document.createElement('div') or supports document.createElement('<div>')
      // but not document.createElement('<div name="test">')
      // IE9- supports also document.createElement('<div name="test">')
      // FF4+ and IE11 don't support document.createElement('<div>')
      if (browserVersion.hasFeature(BrowserVersionFeatures.JS_DOCUMENT_CREATE_ELEMENT_STRICT)
          && (tagName.contains("<") || tagName.contains(">"))) {
        LOG.info(
            "createElement: Provided string '"
                + tagName
                + "' contains an invalid character; '<' and '>' are not allowed");
        throw Context.reportRuntimeError("String contains an invalid character");
      } else if (!browserVersion.hasFeature(JS_DOCUMENT_CREATE_ELEMENT_EXTENDED_SYNTAX)
          && tagName.startsWith("<")
          && tagName.endsWith(">")) {
        tagName = tagName.substring(1, tagName.length() - 1);

        final Matcher matcher = TAG_NAME_PATTERN.matcher(tagName);
        if (!matcher.matches()) {
          LOG.info(
              "createElement: Provided string '" + tagName + "' contains an invalid character");
          throw Context.reportRuntimeError("String contains an invalid character");
        }
      }

      final SgmlPage page = getPage();
      final org.w3c.dom.Node element;
      if ("comment".equalsIgnoreCase(tagName)
          && browserVersion.hasFeature(JS_DOCUMENT_CREATE_ELEMENT_COMMENT)) {
        element = new DomComment(page, "");
      } else {
        element = page.createElement(tagName);
        if (element instanceof BaseFrameElement) {
          ((BaseFrameElement) element).markAsCreatedByJavascript();
        } else if (element instanceof HtmlInput) {
          ((HtmlInput) element).markAsCreatedByJavascript();
        } else if (element instanceof HtmlImage) {
          ((HtmlImage) element).markAsCreatedByJavascript();
        } else if (element instanceof HtmlKeygen) {
          ((HtmlKeygen) element).markAsCreatedByJavascript();
        } else if (element instanceof HtmlRp) {
          ((HtmlRp) element).markAsCreatedByJavascript();
        } else if (element instanceof HtmlRt) {
          ((HtmlRt) element).markAsCreatedByJavascript();
        } else if (element instanceof HtmlUnknownElement) {
          ((HtmlUnknownElement) element).markAsCreatedByJavascript();
        }
      }
      final Object jsElement;
      if ("event".equalsIgnoreCase(tagName)
          && browserVersion.hasFeature(JS_DOCUMENT_CREATE_ELEMENT_COMMENT)) {
        jsElement = new SimpleScriptable();
        ((SimpleScriptable) jsElement).setClassName("Object");
        ((SimpleScriptable) jsElement).setParentScope(window_);
      } else {
        jsElement = getScriptableFor(element);
      }

      if (jsElement == NOT_FOUND) {
        if (LOG.isDebugEnabled()) {
          LOG.debug(
              "createElement("
                  + tagName
                  + ") cannot return a result as there isn't a JavaScript object for the element "
                  + element.getClass().getName());
        }
      } else {
        result = jsElement;
      }
    } catch (final ElementNotFoundException e) {
      // Just fall through - result is already set to NOT_FOUND
    }
    return result;
  }