예제 #1
0
  private void createNodeElement(String namespaceURI, String localName, String qName) {

    String prefix = null;
    // retrieves the prefix if any
    if (namespaceURI != null) {
      prefix = _context.getNamespacePrefix(namespaceURI);
    } else if (qName != null) {
      if ((qName.length() != 0) && (qName.indexOf(':') != -1))
        prefix = qName.substring(0, qName.indexOf(':'));
    }

    String name = null;
    // -- if namespace processing is disabled then the localName might be null, in that case
    // -- we use the localpart of the QName
    if (localName != null && localName.length() > 0) name = localName;
    else name = getLocalPart(qName);

    // creates the starting ELEMENT node
    // or a default ELEMENT node
    if ((_nodeStack.empty()) && (_startingNode == null)) {
      _startingNode = new AnyNode(AnyNode.ELEMENT, name, prefix, namespaceURI, null);
      _node = _startingNode;
    } else {
      _node = new AnyNode(AnyNode.ELEMENT, name, prefix, namespaceURI, null);
      // push the node in the stack
      _nodeStack.push(_node);
    }
  }
예제 #2
0
  public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
    _character = false;
    String name = null;
    // -- if namespace processing is disabled then the localName might be null, in that case
    // -- we use the QName
    if (localName != null && localName.length() > 0) {
      name = localName;
    } else {
      name = getLocalPart(qName);
    }

    // --if it is the starting element just returns
    if (_startingNode.getLocalName().equals(name) && _nodeStack.empty()) return;

    // --else just add the node we have built to the previous node
    _node = (AnyNode) _nodeStack.pop();

    // -- if the stack is empty, we have a new child for the root node
    // -- or a new sibling for the first child of the root node
    if (_nodeStack.empty()) {
      _startingNode.addChild(_node);
      _node = _startingNode;
    } else {
      AnyNode previousNode = (AnyNode) _nodeStack.peek();
      previousNode.addChild(_node);
      // --the node processing is finished -> come back to the previous node
      _node = previousNode;
    }
  }
예제 #3
0
  /** Implementation of {@link org.xml.sax.DocumentHandler#startElement} */
  public void startElement(String name, AttributeList atts) throws SAXException {
    _character = false;
    String qName;
    String value;
    AnyNode tempNode = null;

    // Namespace handling code to be moved once we integrate
    // the new event API
    ///////////////// NAMESPACE HANDLING/////////////////////
    _context = _context.createNamespaces();
    String prefix = "";
    String namespaceURI = null;
    int idx = name.indexOf(':');
    if (idx >= 0) {
      prefix = name.substring(0, idx);
    }
    namespaceURI = _context.getNamespaceURI(prefix);
    // --Overhead here since we process attributes twice
    for (int i = 0; i < atts.getLength(); ++i) {
      qName = atts.getName(i);
      value = atts.getValue(i);
      String nsPrefix = null;

      if (qName.startsWith(XMLNS_PREFIX)) {
        // handles namespace declaration
        // Extract the prefix if any
        nsPrefix = (qName.equals(XMLNS_PREFIX)) ? null : qName.substring(XMLNS_PREFIX_LENGTH);
        tempNode = new AnyNode(AnyNode.NAMESPACE, getLocalPart(qName), nsPrefix, value, null);
        _context.addNamespace(nsPrefix, value);
        _namespaces.push(tempNode);
        if (prefix.equals(nsPrefix)) namespaceURI = value;
      }
    }
    //////////////////////// END OF NAMESPACE HANDLING///////////////

    createNodeElement(namespaceURI, getLocalPart(name), name);
    while (!_namespaces.empty()) {
      tempNode = (AnyNode) _namespaces.pop();
      _node.addNamespace(tempNode);
    }

    // process attributes
    for (int i = 0; i < atts.getLength(); ++i) {

      qName = atts.getName(i);
      value = atts.getValue(i);

      // Namespace handling already done
      if (!qName.startsWith(XMLNS_PREFIX)) {
        tempNode = new AnyNode(AnyNode.ATTRIBUTE, getLocalPart(qName), null, null, value);
        _node.addAttribute(tempNode);
      }
    }
    tempNode = null;
  }
예제 #4
0
 // --Namespace related (SAX2 Events)
 public void startPrefixMapping(String prefix, String uri) throws SAXException {
   AnyNode temp = new AnyNode(AnyNode.NAMESPACE, null, prefix, uri, null);
   _namespaces.push(temp);
   if (_processNamespace) {
     _context = _context.createNamespaces();
     _processNamespace = true;
   }
   _context.addNamespace(prefix, uri);
 }
예제 #5
0
  /** Implementation of {@link org.xml.sax.ContentHandler#startElement} */
  public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
      throws SAXException {
    AnyNode tempNode;

    // --SAX2 Parser has not processed the namespaces so we need to do it.
    if (_processNamespace) {
      // Namespace handling code to be moved once we integrate
      // the new event API
      ///////////////// NAMESPACE HANDLING/////////////////////
      _context = _context.createNamespaces();
      String prefix = "";
      int idx = qName.indexOf(':');
      if (idx >= 0) {
        prefix = qName.substring(0, idx);
      }
      namespaceURI = _context.getNamespaceURI(prefix);
      // --Overhead here since we process attributes twice
      for (int i = 0; i < atts.getLength(); ++i) {
        String attrqName = atts.getQName(i);
        String value = atts.getValue(i);
        String nsPrefix = null;
        // handles namespace declaration
        if (attrqName.startsWith(XMLNS_PREFIX)) {
          // Extract the prefix if any
          nsPrefix =
              (attrqName.equals(XMLNS_PREFIX)) ? null : attrqName.substring(XMLNS_PREFIX_LENGTH);
          tempNode = new AnyNode(AnyNode.NAMESPACE, getLocalPart(attrqName), nsPrefix, value, null);
          _context.addNamespace(nsPrefix, value);
          _namespaces.push(tempNode);
          if (prefix.equals(nsPrefix)) namespaceURI = value;
        }
      }
      //////////////////////// END OF NAMESPACE HANDLING///////////////
    }

    // create element
    createNodeElement(namespaceURI, localName, qName);

    // process attributes
    for (int i = 0; i < atts.getLength(); ++i) {

      String uri = atts.getURI(i);
      String attqName = atts.getQName(i);
      String value = atts.getValue(i);
      String prefix = null;

      // -- skip namespace declarations? (handled above)
      if (_processNamespace) if (attqName.startsWith(XMLNS_PREFIX)) continue;

      // --attribute namespace prefix?
      if ((attqName.length() != 0) && (attqName.indexOf(':') != -1))
        prefix = attqName.substring(0, attqName.indexOf(':'));

      // --namespace not yet processed?
      if (_processNamespace) {
        // attribute namespace
        if (prefix != null) uri = _context.getNamespaceURI(prefix);
      }
      // --add attribute
      tempNode = new AnyNode(AnyNode.ATTRIBUTE, getLocalPart(attqName), prefix, uri, value);
      _node.addAttribute(tempNode);
    }

    // --empty the namespace stack and add
    // --the namespace nodes to the current node.
    while (!_namespaces.empty()) {
      tempNode = (AnyNode) _namespaces.pop();
      _node.addNamespace(tempNode);
    }
    tempNode = null;
  }