Beispiel #1
0
  /**
   * This will recursively invoke all of the callbacks for a particular element.
   *
   * @param element <code>Element</code> used in callbacks.
   * @param namespaces <code>List</code> stack of Namespaces in scope.
   */
  private void element(Element element, NamespaceStack stack) throws JDOMException {

    AttributesImpl nsAtts = null; // The namespaces as xmlns attributes

    // contentHandler.startPrefixMapping()
    stack.push(element);
    try {
      for (Namespace ns : stack.addedForward()) {
        startPrefixMapping(ns);
        nsAtts = this.addNsAttribute(nsAtts, ns);
      }

      // contentHandler.startElement()
      startElement(element, nsAtts);

      // handle content in the element
      elementContent(element.getContent(), stack);

      // update locator
      if (locator != null) {
        locator.setNode(element);
      }

      // contentHandler.endElement()
      endElement(element);
    } finally {
      stack.pop();
    }

    // contentHandler.endPrefixMapping()
    // de-map in reverse order to the mapping.
    for (Namespace ns : stack.addedReverse()) {
      endPrefixMapping(ns);
    }
  }
Beispiel #2
0
 public static void main(String[] args) throws Exception {
   String sourceUrlString = "data/test.html";
   if (args.length == 0)
     System.err.println("Using default argument of \"" + sourceUrlString + '"');
   else sourceUrlString = args[0];
   if (sourceUrlString.indexOf(':') == -1) sourceUrlString = "file:" + sourceUrlString;
   System.out.println("\nSource URL:");
   System.out.println(sourceUrlString);
   URL url = new URL(sourceUrlString);
   Source source = new Source(url);
   System.out.println("\nDocument Title:");
   Element titleElement = source.getFirstElement(HTMLElementName.TITLE);
   System.out.println(titleElement != null ? titleElement.getContent().toString() : "(none)");
   System.out.println("\nSource.getEncoding():");
   System.out.println(source.getEncoding());
   System.out.println("\nSource.getEncodingSpecificationInfo():");
   System.out.println(source.getEncodingSpecificationInfo());
   System.out.println("\nSource.getPreliminaryEncodingInfo():");
   System.out.println(source.getPreliminaryEncodingInfo());
 }
Beispiel #3
0
  /**
   * This will recursively invoke all of the callbacks for a particular element.
   *
   * @param element <code>Element</code> used in callbacks.
   * @param namespaces <code>List</code> stack of Namespaces in scope.
   */
  private void element(Element element, NamespaceStack namespaces) throws JDOMException {
    // used to check endPrefixMapping
    int previouslyDeclaredNamespaces = namespaces.size();

    // contentHandler.startPrefixMapping()
    Attributes nsAtts = startPrefixMapping(element, namespaces);

    // contentHandler.startElement()
    startElement(element, nsAtts);

    // handle content in the element
    elementContent(element.getContent(), namespaces);

    // update locator
    locator.setNode(element);

    // contentHandler.endElement()
    endElement(element);

    // contentHandler.endPrefixMapping()
    endPrefixMapping(namespaces, previouslyDeclaredNamespaces);
  }
 private static String getTitle(Source source) {
   Element titleElement = source.getFirstElement(HTMLElementName.TITLE);
   if (titleElement == null) return null;
   // TITLE element never contains other tags so just decode it collapsing whitespace:
   return CharacterReference.decodeCollapseWhiteSpace(titleElement.getContent());
 }