/**
  * This method is always the second method of all callbacks in all handlers to be invoked
  * (setDocumentLocator is always first).
  */
 private void startDocument() throws JDOMException {
   try {
     contentHandler.startDocument();
   } catch (SAXException se) {
     throw new JDOMException("Exception in startDocument", se);
   }
 }
 /**
  * This will invoke the <code>endPrefixMapping</code> callback in the <code>ContentHandler</code>
  * when a namespace is goes out of scope in the <code>Document</code>.
  *
  * @param namespace Namespace leaving scope.
  */
 private void endPrefixMapping(Namespace namespace) throws JDOMException {
   try {
     contentHandler.endPrefixMapping(namespace.getPrefix());
   } catch (SAXException se) {
     throw new JDOMException("Exception in endPrefixMapping", se);
   }
 }
Exemple #3
0
  /**
   * This will invoke the <code>startElement</code> callback in the <code>ContentHandler</code>.
   *
   * @param element <code>Element</code> used in callbacks.
   * @param nsAtts <code>List</code> of namespaces to declare with the element or <code>null</code>.
   */
  private void startElement(Element element, Attributes nsAtts) throws JDOMException {
    String namespaceURI = element.getNamespaceURI();
    String localName = element.getName();
    String rawName = element.getQualifiedName();

    // Allocate attribute list.
    AttributesImpl atts = (nsAtts != null) ? new AttributesImpl(nsAtts) : new AttributesImpl();

    List attributes = element.getAttributes();
    Iterator i = attributes.iterator();
    while (i.hasNext()) {
      Attribute a = (Attribute) i.next();
      atts.addAttribute(
          a.getNamespaceURI(),
          a.getName(),
          a.getQualifiedName(),
          getAttributeTypeName(a.getAttributeType()),
          a.getValue());
    }

    try {
      contentHandler.startElement(namespaceURI, localName, rawName, atts);
    } catch (SAXException se) {
      throw new JDOMException("Exception in startElement", se);
    }
  }
 /**
  * This will be called for each chunk of character data encountered.
  *
  * @param elementText all text in an element, including whitespace.
  */
 private void characters(String elementText) throws JDOMException {
   char[] c = elementText.toCharArray();
   try {
     contentHandler.characters(c, 0, c.length);
   } catch (SAXException se) {
     throw new JDOMException("Exception in characters", se);
   }
 }
Exemple #5
0
 /** Convert an Object to a String and generate SAX characters events. */
 public static void objectToCharacters(Object o, ContentHandler contentHandler) {
   try {
     char[] charValue = (o == null) ? null : o.toString().toCharArray();
     if (charValue != null) contentHandler.characters(charValue, 0, charValue.length);
   } catch (Exception e) {
     throw new OXFException(e);
   }
 }
  /** Always the last method of all callbacks in all handlers to be invoked. */
  private void endDocument() throws JDOMException {
    try {
      contentHandler.endDocument();

      // reset locator
      locator = null;
    } catch (SAXException se) {
      throw new JDOMException("Exception in endDocument", se);
    }
  }
 /**
  * This will invoke the <code>ContentHandler.skippedEntity</code> callback when an entity
  * reference is encountered.
  *
  * @param entity <code>EntityRef</code>.
  */
 private void entityRef(EntityRef entity) throws JDOMException {
   if (entity != null) {
     try {
       // No need to worry about appending a '%' character as
       // we do not support parameter entities
       contentHandler.skippedEntity(entity.getName());
     } catch (SAXException se) {
       throw new JDOMException("Exception in entityRef", se);
     }
   }
 }
  /**
   * This will invoke the <code>endElement</code> callback in the <code>ContentHandler</code>.
   *
   * @param element <code>Element</code> used in callbacks.
   */
  private void endElement(Element element) throws JDOMException {
    String namespaceURI = element.getNamespaceURI();
    String localName = element.getName();
    String rawName = element.getQualifiedName();

    try {
      contentHandler.endElement(namespaceURI, localName, rawName);
    } catch (SAXException se) {
      throw new JDOMException("Exception in endElement", se);
    }
  }
 /**
  * This will invoke the <code>ContentHandler.processingInstruction</code> callback when a
  * processing instruction is encountered.
  *
  * @param pi <code>ProcessingInstruction</code> containing target and data.
  */
 private void processingInstruction(ProcessingInstruction pi) throws JDOMException {
   if (pi != null) {
     String target = pi.getTarget();
     String data = pi.getData();
     try {
       contentHandler.processingInstruction(target, data);
     } catch (SAXException se) {
       throw new JDOMException("Exception in processingInstruction", se);
     }
   }
 }
Exemple #10
0
 /**
  * This will invoke the <code>endPrefixMapping</code> callback in the <code>ContentHandler</code>
  * when a namespace is goes out of scope in the <code>Document</code>.
  *
  * @param namespaces <code>List</code> stack of Namespaces in scope.
  * @param previouslyDeclaredNamespaces number of previously declared namespaces
  */
 private void endPrefixMapping(NamespaceStack namespaces, int previouslyDeclaredNamespaces)
     throws JDOMException {
   while (namespaces.size() > previouslyDeclaredNamespaces) {
     String prefix = namespaces.pop();
     try {
       contentHandler.endPrefixMapping(prefix);
     } catch (SAXException se) {
       throw new JDOMException("Exception in endPrefixMapping", se);
     }
   }
 }
Exemple #11
0
 /**
  * Read characters from a Reader and generate SAX characters events.
  *
  * <p>The caller has to close the Reader if needed.
  */
 public static void readerToCharacters(Reader reader, ContentHandler contentHandler) {
   try {
     // Work with buffered Reader
     reader = new BufferedReader(reader);
     // Read and write in chunks
     char[] buf = new char[1024];
     int count;
     while ((count = reader.read(buf)) != -1) contentHandler.characters(buf, 0, count);
   } catch (Exception e) {
     throw new OXFException(e);
   }
 }
Exemple #12
0
  /**
   * This will invoke the <code>ContentHandler.startPrefixMapping</code> callback when a new
   * namespace is encountered in the <code>Document</code>.
   *
   * @param element <code>Element</code> used in callbacks.
   * @param namespaces <code>List</code> stack of Namespaces in scope.
   * @return <code>Attributes</code> declaring the namespaces local to <code>element</code> or
   *     <code>null</code>.
   */
  private Attributes startPrefixMapping(Element element, NamespaceStack namespaces)
      throws JDOMException {
    AttributesImpl nsAtts = null; // The namespaces as xmlns attributes

    Namespace ns = element.getNamespace();
    if (ns != Namespace.XML_NAMESPACE) {
      String prefix = ns.getPrefix();
      String uri = namespaces.getURI(prefix);
      if (!ns.getURI().equals(uri)) {
        namespaces.push(ns);
        nsAtts = this.addNsAttribute(nsAtts, ns);
        try {
          contentHandler.startPrefixMapping(prefix, ns.getURI());
        } catch (SAXException se) {
          throw new JDOMException("Exception in startPrefixMapping", se);
        }
      }
    }

    // Fire additional namespace declarations
    List additionalNamespaces = element.getAdditionalNamespaces();
    if (additionalNamespaces != null) {
      Iterator itr = additionalNamespaces.iterator();
      while (itr.hasNext()) {
        ns = (Namespace) itr.next();
        String prefix = ns.getPrefix();
        String uri = namespaces.getURI(prefix);
        if (!ns.getURI().equals(uri)) {
          namespaces.push(ns);
          nsAtts = this.addNsAttribute(nsAtts, ns);
          try {
            contentHandler.startPrefixMapping(prefix, ns.getURI());
          } catch (SAXException se) {
            throw new JDOMException("Exception in startPrefixMapping", se);
          }
        }
      }
    }
    return nsAtts;
  }
Exemple #13
0
 public static void streamNullDocument(ContentHandler contentHandler) throws SAXException {
   contentHandler.startDocument();
   contentHandler.startPrefixMapping(XMLConstants.XSI_PREFIX, XMLConstants.XSI_URI);
   final AttributesImpl attributes = new AttributesImpl();
   attributes.addAttribute(XMLConstants.XSI_URI, "nil", "xsi:nil", "CDATA", "true");
   contentHandler.startElement("", "null", "null", attributes);
   contentHandler.endElement("", "null", "null");
   contentHandler.endPrefixMapping(XMLConstants.XSI_PREFIX);
   contentHandler.endDocument();
 }
  /**
   * This method tells you the line of the XML file being parsed. For an in-memory document, it's
   * meaningless. The location is only valid for the current parsing lifecycle, but the document has
   * already been parsed. Therefore, it returns -1 for both line and column numbers.
   *
   * @param document JDOM <code>Document</code>.
   */
  private void documentLocator(Document document) {
    locator = new JDOMLocator();
    String publicID = null;
    String systemID = null;

    if (document != null) {
      DocType docType = document.getDocType();
      if (docType != null) {
        publicID = docType.getPublicID();
        systemID = docType.getSystemID();
      }
    }
    locator.setPublicId(publicID);
    locator.setSystemId(systemID);
    locator.setLineNumber(-1);
    locator.setColumnNumber(-1);

    contentHandler.setDocumentLocator(locator);
  }
Exemple #15
0
 /** @see org.xml.sax.helpers.DefaultHandler#setDocumentLocator(org.xml.sax.Locator) */
 public void setDocumentLocator(Locator locator) {
   this.locator = locator;
   if (delegate != null && delegate instanceof ContentHandler)
     ((ContentHandler) delegate).setDocumentLocator(locator);
 }