Java Code Examples for org.xml.sax.Attributes#getURI()

The following examples show how to use org.xml.sax.Attributes#getURI() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EncodingAlgorithmAttributesImpl.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Copy an entire Attributes object.
 *
 * @param atts The attributes to copy.
 */
public void setAttributes(Attributes atts) {
    _length = atts.getLength();
    if (_length > 0) {

        if (_length >= _algorithmData.length) {
            resizeNoCopy();
        }

        int index = 0;
        for (int i = 0; i < _length; i++) {
            _data[index++] = atts.getURI(i);
            _data[index++] = atts.getLocalName(i);
            _data[index++] = atts.getQName(i);
            _data[index++] = atts.getType(i);
            _data[index++] = atts.getValue(i);
            index++;
            _toIndex[i] = false;
            _alphabets[i] = null;
        }
    }
}
 
Example 2
Source File: AttributesImpl.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Copy an entire Attributes object.
 *
 * <p>It may be more efficient to reuse an existing object
 * rather than constantly allocating new ones.</p>
 *
 * @param atts The attributes to copy.
 */
public void setAttributes (Attributes atts)
{
    clear();
    length = atts.getLength();
    if (length > 0) {
        data = new String[length*5];
        for (int i = 0; i < length; i++) {
            data[i*5] = atts.getURI(i);
            data[i*5+1] = atts.getLocalName(i);
            data[i*5+2] = atts.getQName(i);
            data[i*5+3] = atts.getType(i);
            data[i*5+4] = atts.getValue(i);
        }
    }
}
 
Example 3
Source File: SerializerBase.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Add the given attributes to the currently collected ones. These
 * attributes are always added, regardless of whether on not an element
 * is currently open.
 * @param atts List of attributes to add to this list
 */
public void addAttributes(Attributes atts) throws SAXException
{

    int nAtts = atts.getLength();
    for (int i = 0; i < nAtts; i++)
    {
        String uri = atts.getURI(i);

        if (null == uri)
            uri = "";

        addAttributeAlways(
            uri,
            atts.getLocalName(i),
            atts.getQName(i),
            atts.getType(i),
            atts.getValue(i),
            false);

    }
}
 
Example 4
Source File: SvgElementFactory.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
private static Map<String, DomAttr> toMap(final SgmlPage page, final Attributes attributes) {
    Map<String, DomAttr> attributeMap = null;
    if (attributes != null) {
        attributeMap = new LinkedHashMap<>(attributes.getLength());
        for (int i = 0; i < attributes.getLength(); i++) {
            final String qName = attributes.getQName(i);
            // browsers consider only first attribute (ex: <div id='foo' id='something'>...</div>)
            if (!attributeMap.containsKey(qName)) {
                String namespaceURI = attributes.getURI(i);
                if (namespaceURI != null && namespaceURI.isEmpty()) {
                    namespaceURI = null;
                }
                final DomAttr newAttr = new DomAttr(page, namespaceURI, qName, attributes.getValue(i), true);
                attributeMap.put(qName, newAttr);
            }
        }
    }
    return attributeMap;
}
 
Example 5
Source File: AttributesImpl.java    From HtmlCompat with Apache License 2.0 6 votes vote down vote up
/**
 * Copy an entire Attributes object.
 *
 * <p>It may be more efficient to reuse an existing object
 * rather than constantly allocating new ones.</p>
 *
 * @param atts The attributes to copy.
 */
public void setAttributes (Attributes atts)
{
    clear();
    length = atts.getLength();
    if (length > 0) {
        data = new String[length*5];
        for (int i = 0; i < length; i++) {
            data[i*5] = atts.getURI(i);
            data[i*5+1] = atts.getLocalName(i);
            data[i*5+2] = atts.getQName(i);
            data[i*5+3] = atts.getType(i);
            data[i*5+4] = atts.getValue(i);
        }
    }
}
 
Example 6
Source File: EncodingAlgorithmAttributesImpl.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Copy an entire Attributes object.
 *
 * @param atts The attributes to copy.
 */
public void setAttributes(Attributes atts) {
    _length = atts.getLength();
    if (_length > 0) {

        if (_length >= _algorithmData.length) {
            resizeNoCopy();
        }

        int index = 0;
        for (int i = 0; i < _length; i++) {
            _data[index++] = atts.getURI(i);
            _data[index++] = atts.getLocalName(i);
            _data[index++] = atts.getQName(i);
            _data[index++] = atts.getType(i);
            _data[index++] = atts.getValue(i);
            index++;
            _toIndex[i] = false;
            _alphabets[i] = null;
        }
    }
}
 
Example 7
Source File: EncodingAlgorithmAttributesImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Copy an entire Attributes object.
 *
 * @param atts The attributes to copy.
 */
public void setAttributes(Attributes atts) {
    _length = atts.getLength();
    if (_length > 0) {

        if (_length >= _algorithmData.length) {
            resizeNoCopy();
        }

        int index = 0;
        for (int i = 0; i < _length; i++) {
            _data[index++] = atts.getURI(i);
            _data[index++] = atts.getLocalName(i);
            _data[index++] = atts.getQName(i);
            _data[index++] = atts.getType(i);
            _data[index++] = atts.getValue(i);
            index++;
            _toIndex[i] = false;
            _alphabets[i] = null;
        }
    }
}
 
Example 8
Source File: AttributesImpl.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Copy an entire Attributes object.
 *
 * <p>It may be more efficient to reuse an existing object
 * rather than constantly allocating new ones.</p>
 *
 * @param atts The attributes to copy.
 */
public void setAttributes (Attributes atts)
{
    clear();
    length = atts.getLength();
    if (length > 0) {
        data = new String[length*5];
        for (int i = 0; i < length; i++) {
            data[i*5] = atts.getURI(i);
            data[i*5+1] = atts.getLocalName(i);
            data[i*5+2] = atts.getQName(i);
            data[i*5+3] = atts.getType(i);
            data[i*5+4] = atts.getValue(i);
        }
    }
}
 
Example 9
Source File: SAXDocumentSerializer.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
protected final int countAttributes(Attributes atts) {
    // Count attributes ignoring any in the XMLNS namespace
    // Note, such attributes may be produced when transforming from a DOM node
    int count = 0;
    for (int i = 0; i < atts.getLength(); i++) {
        final String uri = atts.getURI(i);
        if (uri == "http://www.w3.org/2000/xmlns/" || uri.equals("http://www.w3.org/2000/xmlns/")) {
            continue;
        }
        count++;
    }
    return count;
}
 
Example 10
Source File: SchemaParser.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
void attributes(Attributes atts) throws SAXException {
    int len = atts.getLength();
    for (int i = 0; i < len; i++) {
        String uri = atts.getURI(i);
        if (uri.length() == 0) {
            String name = atts.getLocalName(i);
            if (name.equals("name")) {
                setName(atts.getValue(i).trim());
            } else if (name.equals("ns")) {
                ns = atts.getValue(i);
            } else if (name.equals("datatypeLibrary")) {
                datatypeLibrary = atts.getValue(i);
                checkUri(datatypeLibrary);
                if (!datatypeLibrary.equals("")
                        && !Uri.isAbsolute(datatypeLibrary)) {
                    error("relative_datatype_library");
                }
                if (Uri.hasFragmentId(datatypeLibrary)) {
                    error("fragment_identifier_datatype_library");
                }
                datatypeLibrary = Uri.escapeDisallowedChars(datatypeLibrary);
            } else {
                setOtherAttribute(name, atts.getValue(i));
            }
        } else if (uri.equals(relaxngURI)) {
            error("qualified_attribute", atts.getLocalName(i));
        } else if (uri.equals(WellKnownNamespaces.XML)
                && atts.getLocalName(i).equals("base")) {
            xmlBaseHandler.xmlBaseAttribute(atts.getValue(i));
        } else {
            if (annotations == null) {
                annotations = schemaBuilder.makeAnnotations(null, getContext());
            }
            annotations.addAttribute(uri, atts.getLocalName(i), findPrefix(atts.getQName(i), uri),
                    atts.getValue(i), startLocation);
        }
    }
    endAttributes();
}
 
Example 11
Source File: AttributesImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Copy an entire Attributes object.
 *
 * <p>It may be more efficient to reuse an existing object
 * rather than constantly allocating new ones.</p>
 *
 * @param atts The attributes to copy.
 */
public void setAttributes (Attributes atts)
{
clear();
length = atts.getLength();
data = new String[length*5];
for (int i = 0; i < length; i++) {
    data[i*5] = atts.getURI(i);
    data[i*5+1] = atts.getLocalName(i);
    data[i*5+2] = atts.getQName(i);
    data[i*5+3] = atts.getType(i);
    data[i*5+4] = atts.getValue(i);
}
}
 
Example 12
Source File: AttributesImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Copy an entire Attributes object.
 *
 * <p>It may be more efficient to reuse an existing object
 * rather than constantly allocating new ones.</p>
 *
 * @param atts The attributes to copy.
 */
public void setAttributes (Attributes atts)
{
clear();
length = atts.getLength();
data = new String[length*5];
for (int i = 0; i < length; i++) {
    data[i*5] = atts.getURI(i);
    data[i*5+1] = atts.getLocalName(i);
    data[i*5+2] = atts.getQName(i);
    data[i*5+3] = atts.getType(i);
    data[i*5+4] = atts.getValue(i);
}
}
 
Example 13
Source File: AttributesImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Copy an entire Attributes object.
 *
 * <p>It may be more efficient to reuse an existing object
 * rather than constantly allocating new ones.</p>
 *
 * @param atts The attributes to copy.
 */
public void setAttributes (Attributes atts)
{
clear();
length = atts.getLength();
data = new String[length*5];
for (int i = 0; i < length; i++) {
    data[i*5] = atts.getURI(i);
    data[i*5+1] = atts.getLocalName(i);
    data[i*5+2] = atts.getQName(i);
    data[i*5+3] = atts.getType(i);
    data[i*5+4] = atts.getValue(i);
}
}
 
Example 14
Source File: SAX2DOMEx.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void startElement(String namespace, String localName, String qName, Attributes attrs) {
    Node parent = nodeStack.peek();

    // some broken DOM implementation (we confirmed it with SAXON)
    // return null from this method.
    Element element = document.createElementNS(namespace, qName);

    if (element == null) {
        // if so, report an user-friendly error message,
        // rather than dying mysteriously with NPE.
        throw new AssertionError(
                Messages.format(Messages.DOM_IMPL_DOESNT_SUPPORT_CREATELEMENTNS,
                document.getClass().getName(),
                Which.which(document.getClass())));
    }

    // process namespace bindings
    for (int i = 0; i < unprocessedNamespaces.size(); i += 2) {
        String prefix = unprocessedNamespaces.get(i);
        String uri = unprocessedNamespaces.get(i + 1);

        namespace(element, prefix, uri);
    }
    unprocessedNamespaces.clear();


    if (attrs != null) {
        int length = attrs.getLength();
        for (int i = 0; i < length; i++) {
            String namespaceuri = attrs.getURI(i);
            String value = attrs.getValue(i);
            String qname = attrs.getQName(i);
            element.setAttributeNS(namespaceuri, qname, value);
        }
    }
    // append this new node onto current stack node
    parent.appendChild(element);
    // push this node onto stack
    nodeStack.push(element);
}
 
Example 15
Source File: FxModelBuilder.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Processes ?include directive
 * 
 * @param include 
 */
@NbBundle.Messages({
    "ERR_missingIncludeName=Missing include name",
    "# {0} - attribute name",
    "ERR_unexpectedIncludeAttribute=Unexpected attribute in fx:include: {0}"
})
private FxNode handleFxInclude(Attributes atts, String localName) {
    String include = null;
    String id = null;
    
    for (int i = 0; i < atts.getLength(); i++) {
        String uri = atts.getURI(i);
        String attName = atts.getLocalName(i);
        if (FX_ATTR_REFERENCE_SOURCE.equals(attName)) {
            include = atts.getValue(i);
            continue;
        }
        if (isFxmlNamespaceUri(uri)) {
            if (FX_ID.equals(attName)) {
                id = atts.getValue(i);
            } else {
                String qName = atts.getQName(i);
                addAttributeError(
                    qName,
                    "unexpected-include-attribute",
                    ERR_unexpectedIncludeAttribute(qName),
                    qName
                );
            }
        }
    }
    if (include == null) {
        // must be some text, otherwise = error
        addAttributeError(
            ContentLocator.ATTRIBUTE_TARGET, 
            "missing-included-name",
            ERR_missingIncludeName()
        );
        
        FxNode n = accessor.createErrorElement(localName);
        initElement(n);
        addError("invalid-fx-element", ERR_invalidFxElement(localName), localName);
        return n;
    }
    // guide: fnames starting with slash are treated relative to the classpath
    FxInclude fxInclude = accessor.createInclude(include, id);
    return fxInclude;
}
 
Example 16
Source File: DOMBuilder.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Receive notification of the beginning of an element.
 *
 * <p>The Parser will invoke this method at the beginning of every
 * element in the XML document; there will be a corresponding
 * endElement() event for every startElement() event (even when the
 * element is empty). All of the element's content will be
 * reported, in order, before the corresponding endElement()
 * event.</p>
 *
 * <p>If the element name has a namespace prefix, the prefix will
 * still be attached.  Note that the attribute list provided will
 * contain only attributes with explicit values (specified or
 * defaulted): #IMPLIED attributes will be omitted.</p>
 *
 *
 * @param ns The namespace of the node
 * @param localName The local part of the qualified name
 * @param name The element name.
 * @param atts The attributes attached to the element, if any.
 * @see #endElement
 * @see org.xml.sax.Attributes
 */
public void startElement(
        String ns, String localName, String name, Attributes atts)
          throws org.xml.sax.SAXException
{

  Element elem;

      // Note that the namespace-aware call must be used to correctly
      // construct a Level 2 DOM, even for non-namespaced nodes.
  if ((null == ns) || (ns.length() == 0))
    elem = m_doc.createElementNS(null,name);
  else
    elem = m_doc.createElementNS(ns, name);

  append(elem);

  try
  {
    int nAtts = atts.getLength();

    if (0 != nAtts)
    {
      for (int i = 0; i < nAtts; i++)
      {

        //System.out.println("type " + atts.getType(i) + " name " + atts.getLocalName(i) );
        // First handle a possible ID attribute
        if (atts.getType(i).equalsIgnoreCase("ID"))
          setIDAttribute(atts.getValue(i), elem);

        String attrNS = atts.getURI(i);

        if("".equals(attrNS))
          attrNS = null; // DOM represents no-namespace as null

        // System.out.println("attrNS: "+attrNS+", localName: "+atts.getQName(i)
        //                   +", qname: "+atts.getQName(i)+", value: "+atts.getValue(i));
        // Crimson won't let us set an xmlns: attribute on the DOM.
        String attrQName = atts.getQName(i);

        // In SAX, xmlns[:] attributes have an empty namespace, while in DOM they
        // should have the xmlns namespace
        if (attrQName.startsWith("xmlns:") || attrQName.equals("xmlns")) {
          attrNS = "http://www.w3.org/2000/xmlns/";
        }

        // ALWAYS use the DOM Level 2 call!
        elem.setAttributeNS(attrNS,attrQName, atts.getValue(i));
      }
    }

    // append(elem);

    m_elemStack.push(elem);

    m_currentNode = elem;

    // append(elem);
  }
  catch(java.lang.Exception de)
  {
    // de.printStackTrace();
    throw new org.xml.sax.SAXException(de);
  }

}
 
Example 17
Source File: SAX2DOM.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public void startElement(String namespace, String localName, String qName,
        Attributes attrs)
    {
        appendTextNode();
        if (needToSetDocumentInfo) {
            setDocumentInfo();
            needToSetDocumentInfo = false;
        }

        final Element tmp = (Element)_document.createElementNS(namespace, qName);

        // Add namespace declarations first
        if (_namespaceDecls != null) {
            final int nDecls = _namespaceDecls.size();
            for (int i = 0; i < nDecls; i++) {
                final String prefix = (String) _namespaceDecls.elementAt(i++);

                if (prefix == null || prefix.equals(EMPTYSTRING)) {
                    tmp.setAttributeNS(XMLNS_URI, XMLNS_PREFIX,
                        (String) _namespaceDecls.elementAt(i));
                }
                else {
                    tmp.setAttributeNS(XMLNS_URI, XMLNS_STRING + prefix,
                        (String) _namespaceDecls.elementAt(i));
                }
            }
            _namespaceDecls.clear();
        }

        // Add attributes to element
/*      final int nattrs = attrs.getLength();
        for (int i = 0; i < nattrs; i++) {
            if (attrs.getLocalName(i) == null) {
                tmp.setAttribute(attrs.getQName(i), attrs.getValue(i));
            }
            else {
                tmp.setAttributeNS(attrs.getURI(i), attrs.getQName(i),
                    attrs.getValue(i));
            }
        } */


        // Add attributes to element
        final int nattrs = attrs.getLength();
        for (int i = 0; i < nattrs; i++) {
            // checking if Namespace processing is being done
            String attQName = attrs.getQName(i);
            String attURI = attrs.getURI(i);
            if (attrs.getLocalName(i).equals("")) {
                tmp.setAttribute(attQName, attrs.getValue(i));
                if (attrs.getType(i).equals("ID")) {
                    tmp.setIdAttribute(attQName, true);
                }
            } else {
                tmp.setAttributeNS(attURI, attQName, attrs.getValue(i));
                if (attrs.getType(i).equals("ID")) {
                    tmp.setIdAttributeNS(attURI, attrs.getLocalName(i), true);
                }
            }
        }


        // Append this new node onto current stack node
        Node last = (Node)_nodeStk.peek();

        // If the SAX2DOM is created with a non-null next sibling node,
        // insert the result nodes before the next sibling under the root.
        if (last == _root && _nextSibling != null)
            last.insertBefore(tmp, _nextSibling);
        else
            last.appendChild(tmp);

        // Push this node onto stack
        _nodeStk.push(tmp);
        _lastSibling = null;
    }
 
Example 18
Source File: SAX2DOM.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
public void startElement(String namespace, String localName, String qName,
        Attributes attrs)
    {
        appendTextNode();
        if (needToSetDocumentInfo) {
            setDocumentInfo();
            needToSetDocumentInfo = false;
        }

        final Element tmp = _document.createElementNS(namespace, qName);

        // Add namespace declarations first
        if (_namespaceDecls != null) {
            final int nDecls = _namespaceDecls.size();
            for (int i = 0; i < nDecls; i++) {
                final String prefix = _namespaceDecls.get(i++);

                if (prefix == null || prefix.equals(EMPTYSTRING)) {
                    tmp.setAttributeNS(XMLNS_URI, XMLNS_PREFIX,
                        _namespaceDecls.get(i));
                }
                else {
                    tmp.setAttributeNS(XMLNS_URI, XMLNS_STRING + prefix,
                        _namespaceDecls.get(i));
                }
            }
            _namespaceDecls.clear();
        }

        // Add attributes to element
/*      final int nattrs = attrs.getLength();
        for (int i = 0; i < nattrs; i++) {
            if (attrs.getLocalName(i) == null) {
                tmp.setAttribute(attrs.getQName(i), attrs.getValue(i));
            }
            else {
                tmp.setAttributeNS(attrs.getURI(i), attrs.getQName(i),
                    attrs.getValue(i));
            }
        } */


        // Add attributes to element
        final int nattrs = attrs.getLength();
        for (int i = 0; i < nattrs; i++) {
            // checking if Namespace processing is being done
            String attQName = attrs.getQName(i);
            String attURI = attrs.getURI(i);
            String type = (attrs.getType(i) == null) ?
                    XMLSymbols.fCDATASymbol : attrs.getType(i);
            if (attrs.getLocalName(i).equals("")) {
                tmp.setAttribute(attQName, attrs.getValue(i));
                if (type.equals("ID")) {
                    tmp.setIdAttribute(attQName, true);
                }
            } else {
                tmp.setAttributeNS(attURI, attQName, attrs.getValue(i));
                if (type.equals("ID")) {
                    tmp.setIdAttributeNS(attURI, attrs.getLocalName(i), true);
                }
            }
        }


        // Append this new node onto current stack node
        Node last = _nodeStk.peek();

        // If the SAX2DOM is created with a non-null next sibling node,
        // insert the result nodes before the next sibling under the root.
        if (last == _root && _nextSibling != null)
            last.insertBefore(tmp, _nextSibling);
        else
            last.appendChild(tmp);

        // Push this node onto stack
        _nodeStk.push(tmp);
        _lastSibling = null;
    }
 
Example 19
Source File: DOMBuilder.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Receive notification of the beginning of an element.
 *
 * <p>The Parser will invoke this method at the beginning of every
 * element in the XML document; there will be a corresponding
 * endElement() event for every startElement() event (even when the
 * element is empty). All of the element's content will be
 * reported, in order, before the corresponding endElement()
 * event.</p>
 *
 * <p>If the element name has a namespace prefix, the prefix will
 * still be attached.  Note that the attribute list provided will
 * contain only attributes with explicit values (specified or
 * defaulted): #IMPLIED attributes will be omitted.</p>
 *
 *
 * @param ns The namespace of the node
 * @param localName The local part of the qualified name
 * @param name The element name.
 * @param atts The attributes attached to the element, if any.
 * @see #endElement
 * @see org.xml.sax.Attributes
 */
public void startElement(
        String ns, String localName, String name, Attributes atts)
          throws org.xml.sax.SAXException
{

  Element elem;

      // Note that the namespace-aware call must be used to correctly
      // construct a Level 2 DOM, even for non-namespaced nodes.
  if ((null == ns) || (ns.length() == 0))
    elem = m_doc.createElementNS(null,name);
  else
    elem = m_doc.createElementNS(ns, name);

  append(elem);

  try
  {
    int nAtts = atts.getLength();

    if (0 != nAtts)
    {
      for (int i = 0; i < nAtts; i++)
      {

        //System.out.println("type " + atts.getType(i) + " name " + atts.getLocalName(i) );
        // First handle a possible ID attribute
        if (atts.getType(i).equalsIgnoreCase("ID"))
          setIDAttribute(atts.getValue(i), elem);

        String attrNS = atts.getURI(i);

        if("".equals(attrNS))
          attrNS = null; // DOM represents no-namespace as null

        // System.out.println("attrNS: "+attrNS+", localName: "+atts.getQName(i)
        //                   +", qname: "+atts.getQName(i)+", value: "+atts.getValue(i));
        // Crimson won't let us set an xmlns: attribute on the DOM.
        String attrQName = atts.getQName(i);

        // In SAX, xmlns[:] attributes have an empty namespace, while in DOM they
        // should have the xmlns namespace
        if (attrQName.startsWith("xmlns:") || attrQName.equals("xmlns")) {
          attrNS = "http://www.w3.org/2000/xmlns/";
        }

        // ALWAYS use the DOM Level 2 call!
        elem.setAttributeNS(attrNS,attrQName, atts.getValue(i));
      }
    }

    // append(elem);

    m_elemStack.push(elem);

    m_currentNode = elem;

    // append(elem);
  }
  catch(java.lang.Exception de)
  {
    // de.printStackTrace();
    throw new org.xml.sax.SAXException(de);
  }

}
 
Example 20
Source File: DOMBuilder.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Receive notification of the beginning of an element.
 *
 * <p>The Parser will invoke this method at the beginning of every
 * element in the XML document; there will be a corresponding
 * endElement() event for every startElement() event (even when the
 * element is empty). All of the element's content will be
 * reported, in order, before the corresponding endElement()
 * event.</p>
 *
 * <p>If the element name has a namespace prefix, the prefix will
 * still be attached.  Note that the attribute list provided will
 * contain only attributes with explicit values (specified or
 * defaulted): #IMPLIED attributes will be omitted.</p>
 *
 *
 * @param ns The namespace of the node
 * @param localName The local part of the qualified name
 * @param name The element name.
 * @param atts The attributes attached to the element, if any.
 * @see #endElement
 * @see org.xml.sax.Attributes
 */
public void startElement(
        String ns, String localName, String name, Attributes atts)
          throws org.xml.sax.SAXException
{

  Element elem;

      // Note that the namespace-aware call must be used to correctly
      // construct a Level 2 DOM, even for non-namespaced nodes.
  if ((null == ns) || (ns.length() == 0))
    elem = m_doc.createElementNS(null,name);
  else
    elem = m_doc.createElementNS(ns, name);

  append(elem);

  try
  {
    int nAtts = atts.getLength();

    if (0 != nAtts)
    {
      for (int i = 0; i < nAtts; i++)
      {

        //System.out.println("type " + atts.getType(i) + " name " + atts.getLocalName(i) );
        // First handle a possible ID attribute
        if (atts.getType(i).equalsIgnoreCase("ID"))
          setIDAttribute(atts.getValue(i), elem);

        String attrNS = atts.getURI(i);

        if("".equals(attrNS))
          attrNS = null; // DOM represents no-namespace as null

        // System.out.println("attrNS: "+attrNS+", localName: "+atts.getQName(i)
        //                   +", qname: "+atts.getQName(i)+", value: "+atts.getValue(i));
        // Crimson won't let us set an xmlns: attribute on the DOM.
        String attrQName = atts.getQName(i);

        // In SAX, xmlns[:] attributes have an empty namespace, while in DOM they
        // should have the xmlns namespace
        if (attrQName.startsWith("xmlns:") || attrQName.equals("xmlns")) {
          attrNS = "http://www.w3.org/2000/xmlns/";
        }

        // ALWAYS use the DOM Level 2 call!
        elem.setAttributeNS(attrNS,attrQName, atts.getValue(i));
      }
    }

    // append(elem);

    m_elemStack.push(elem);

    m_currentNode = elem;

    // append(elem);
  }
  catch(java.lang.Exception de)
  {
    // de.printStackTrace();
    throw new org.xml.sax.SAXException(de);
  }

}