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

The following examples show how to use org.xml.sax.Attributes#getQName() . 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: SynthParser.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private void startBindKey(Attributes attributes) throws SAXException {
    if (_inputMapID == null) {
        // Not in an inputmap, bail.
        return;
    }
    if (_style != null) {
        String key = null;
        String value = null;
        for(int i = attributes.getLength() - 1; i >= 0; i--) {
            String aKey = attributes.getQName(i);

            if (aKey.equals(ATTRIBUTE_KEY)) {
                key = attributes.getValue(i);
            }
            else if (aKey.equals(ATTRIBUTE_ACTION)) {
                value = attributes.getValue(i);
            }
        }
        if (key == null || value == null) {
            throw new SAXException(
                "bindKey: you must supply a key and action");
        }
        _inputMapBindings.add(key);
        _inputMapBindings.add(value);
    }
}
 
Example 2
Source File: SynthParser.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private void startBindKey(Attributes attributes) throws SAXException {
    if (_inputMapID == null) {
        // Not in an inputmap, bail.
        return;
    }
    if (_style != null) {
        String key = null;
        String value = null;
        for(int i = attributes.getLength() - 1; i >= 0; i--) {
            String aKey = attributes.getQName(i);

            if (aKey.equals(ATTRIBUTE_KEY)) {
                key = attributes.getValue(i);
            }
            else if (aKey.equals(ATTRIBUTE_ACTION)) {
                value = attributes.getValue(i);
            }
        }
        if (key == null || value == null) {
            throw new SAXException(
                "bindKey: you must supply a key and action");
        }
        _inputMapBindings.add(key);
        _inputMapBindings.add(value);
    }
}
 
Example 3
Source File: MutableAttrListImpl.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * Add the contents of the attribute list to this list.
 *
 * @param atts List of attributes to add to this list
 */
public void addAttributes(Attributes atts)
{

  int nAtts = atts.getLength();

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

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

    String localName = atts.getLocalName(i);
    String qname = atts.getQName(i);
    int index = this.getIndex(uri, localName);
    // System.out.println("MutableAttrListImpl#addAttributes: "+uri+":"+localName+", "+index+", "+atts.getQName(i)+", "+this);
    if (index >= 0)
      this.setAttribute(index, uri, localName, qname, atts.getType(i),
                        atts.getValue(i));
    else
      addAttribute(uri, localName, qname, atts.getType(i),
                   atts.getValue(i));
  }
}
 
Example 4
Source File: SynthParser.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private void startGraphics(Attributes attributes) throws SAXException {
    SynthGraphicsUtils graphics = null;

    for(int i = attributes.getLength() - 1; i >= 0; i--) {
        String key = attributes.getQName(i);
        if (key.equals(ATTRIBUTE_IDREF)) {
            graphics = (SynthGraphicsUtils)lookup(attributes.getValue(i),
                                             SynthGraphicsUtils.class);
        }
    }
    if (graphics == null) {
        throw new SAXException("graphicsUtils: you must supply an idref");
    }
    if (_style != null) {
        _style.setGraphicsUtils(graphics);
    }
}
 
Example 5
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 6
Source File: SynthParser.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private void startImageIcon(Attributes attributes) throws SAXException {
 String path = null;
 String id = null;

 for(int i = attributes.getLength() - 1; i >= 0; i--) {
     String key = attributes.getQName(i);

     if (key.equals(ATTRIBUTE_ID)) {
         id = attributes.getValue(i);
     }
     else if (key.equals(ATTRIBUTE_PATH)) {
         path = attributes.getValue(i);
     }
 }
 if (path == null) {
     throw new SAXException("imageIcon: you must specify a path");
 }
 register(id, new LazyImageIcon(getResource(path)));
}
 
Example 7
Source File: SynthParser.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
private void startBindKey(Attributes attributes) throws SAXException {
    if (_inputMapID == null) {
        // Not in an inputmap, bail.
        return;
    }
    if (_style != null) {
        String key = null;
        String value = null;
        for(int i = attributes.getLength() - 1; i >= 0; i--) {
            String aKey = attributes.getQName(i);

            if (aKey.equals(ATTRIBUTE_KEY)) {
                key = attributes.getValue(i);
            }
            else if (aKey.equals(ATTRIBUTE_ACTION)) {
                value = attributes.getValue(i);
            }
        }
        if (key == null || value == null) {
            throw new SAXException(
                "bindKey: you must supply a key and action");
        }
        _inputMapBindings.add(key);
        _inputMapBindings.add(value);
    }
}
 
Example 8
Source File: SynthParser.java    From Java8CN with Apache License 2.0 6 votes vote down vote up
private void startGraphics(Attributes attributes) throws SAXException {
    SynthGraphicsUtils graphics = null;

    for(int i = attributes.getLength() - 1; i >= 0; i--) {
        String key = attributes.getQName(i);
        if (key.equals(ATTRIBUTE_IDREF)) {
            graphics = (SynthGraphicsUtils)lookup(attributes.getValue(i),
                                             SynthGraphicsUtils.class);
        }
    }
    if (graphics == null) {
        throw new SAXException("graphicsUtils: you must supply an idref");
    }
    if (_style != null) {
        _style.setGraphicsUtils(graphics);
    }
}
 
Example 9
Source File: SynthParser.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private void startBindKey(Attributes attributes) throws SAXException {
    if (_inputMapID == null) {
        // Not in an inputmap, bail.
        return;
    }
    if (_style != null) {
        String key = null;
        String value = null;
        for(int i = attributes.getLength() - 1; i >= 0; i--) {
            String aKey = attributes.getQName(i);

            if (aKey.equals(ATTRIBUTE_KEY)) {
                key = attributes.getValue(i);
            }
            else if (aKey.equals(ATTRIBUTE_ACTION)) {
                value = attributes.getValue(i);
            }
        }
        if (key == null || value == null) {
            throw new SAXException(
                "bindKey: you must supply a key and action");
        }
        _inputMapBindings.add(key);
        _inputMapBindings.add(value);
    }
}
 
Example 10
Source File: AttributesImpl.java    From hottub 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 11
Source File: JAXPValidatorComponent.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compares the given {@link Attributes} with {@link #fCurrentAttributes}
 * and update the latter accordingly.
 */
private void updateAttributes( Attributes atts ) {
    int len = atts.getLength();
    for( int i=0; i<len; i++ ) {
        String aqn = atts.getQName(i);
        int j = fCurrentAttributes.getIndex(aqn);
        String av = atts.getValue(i);
        if(j==-1) {
            // newly added attribute. add to the current attribute list.

            String prefix;
            int idx = aqn.indexOf(':');
            if( idx<0 ) {
                prefix = null;
            } else {
                prefix = symbolize(aqn.substring(0,idx));
            }

            j = fCurrentAttributes.addAttribute(
                new QName(
                    prefix,
                    symbolize(atts.getLocalName(i)),
                    symbolize(aqn),
                    symbolize(atts.getURI(i))),
                atts.getType(i),av);
        } else {
            // the attribute is present.
            if( !av.equals(fCurrentAttributes.getValue(j)) ) {
                // but the value was changed.
                fCurrentAttributes.setValue(j,av);
            }
        }

        /** Augmentations augs = fCurrentAttributes.getAugmentations(j);
        augs.putItem( Constants.TYPEINFO,
            typeInfoProvider.getAttributeTypeInfo(i) );
        augs.putItem( Constants.ID_ATTRIBUTE,
            typeInfoProvider.isIdAttribute(i)?Boolean.TRUE:Boolean.FALSE ); **/
    }
}
 
Example 12
Source File: XMLFieldConfigHelper.java    From datawave with Apache License 2.0 5 votes vote down vote up
void startNoMatch(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    final int sz = attributes.getLength();
    final Set<String> seenAttr = new HashSet<>(expectedDefaultAttributes);
    
    for (int i = 0; i < sz; i++) {
        final String qn = attributes.getQName(i);
        final String lv = attributes.getValue(i);
        
        if (STORED.equals(qn)) {
            fieldHelper.setNoMatchStored(Boolean.parseBoolean(lv));
            seenAttr.remove(STORED);
        } else if (INDEXED.equals(qn)) {
            fieldHelper.setNoMatchIndexed(Boolean.parseBoolean(lv));
            seenAttr.remove(INDEXED);
        } else if (REVERSE_INDEXED.equals(qn)) {
            fieldHelper.setNoMatchReverseIndexed(Boolean.parseBoolean(lv));
            seenAttr.remove(REVERSE_INDEXED);
        } else if (TOKENIZED.equals(qn)) {
            fieldHelper.setNoMatchTokenized(Boolean.parseBoolean(lv));
            seenAttr.remove(TOKENIZED);
        } else if (REVERSE_TOKENIZED.equals(qn)) {
            fieldHelper.setNoMatchReverseTokenized(Boolean.parseBoolean(lv));
            seenAttr.remove(REVERSE_TOKENIZED);
        } else if (INDEX_TYPE.equals(qn)) {
            if (this.ingestHelper != null) {
                this.ingestHelper.updateDatawaveTypes(null, lv);
            } else {
                log.warn("No BaseIngestHelper set, ignoring type information for nomatch in configuration file");
            }
            seenAttr.remove(INDEX_TYPE);
        } else {
            throw new IllegalArgumentException("Unexpected attribute encounteded in: " + uri + " in 'nomatch' tag: '" + qn + "'");
        }
    }
    
    if (!seenAttr.isEmpty()) {
        throw new IllegalArgumentException("nomatch tag incomplete, '" + seenAttr + "' attributes were missing");
    }
}
 
Example 13
Source File: SynthParser.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private void startInputMap(Attributes attributes) throws SAXException {
    _inputMapBindings.clear();
    _inputMapID = null;
    if (_style != null) {
        for(int i = attributes.getLength() - 1; i >= 0; i--) {
            String key = attributes.getQName(i);

            if (key.equals(ATTRIBUTE_ID)) {
                _inputMapID = attributes.getValue(i);
            }
        }
    }
}
 
Example 14
Source File: JAXPValidatorComponent.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compares the given {@link Attributes} with {@link #fCurrentAttributes}
 * and update the latter accordingly.
 */
private void updateAttributes( Attributes atts ) {
    int len = atts.getLength();
    for( int i=0; i<len; i++ ) {
        String aqn = atts.getQName(i);
        int j = fCurrentAttributes.getIndex(aqn);
        String av = atts.getValue(i);
        if(j==-1) {
            // newly added attribute. add to the current attribute list.

            String prefix;
            int idx = aqn.indexOf(':');
            if( idx<0 ) {
                prefix = null;
            } else {
                prefix = symbolize(aqn.substring(0,idx));
            }

            j = fCurrentAttributes.addAttribute(
                new QName(
                    prefix,
                    symbolize(atts.getLocalName(i)),
                    symbolize(aqn),
                    symbolize(atts.getURI(i))),
                atts.getType(i),av);
        } else {
            // the attribute is present.
            if( !av.equals(fCurrentAttributes.getValue(j)) ) {
                // but the value was changed.
                fCurrentAttributes.setValue(j,av);
            }
        }

        /** Augmentations augs = fCurrentAttributes.getAugmentations(j);
        augs.putItem( Constants.TYPEINFO,
            typeInfoProvider.getAttributeTypeInfo(i) );
        augs.putItem( Constants.ID_ATTRIBUTE,
            typeInfoProvider.isIdAttribute(i)?Boolean.TRUE:Boolean.FALSE ); **/
    }
}
 
Example 15
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 16
Source File: FxModelBuilder.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@NbBundle.Messages({
    "# {0} - tag name",
    "ERR_invalidPropertyName=Invalid property name: {0}"
})
private FxNode handlePropertyTag(String propName, Attributes atts) {
    PropertyValue pv;
    
    int errorAttrs = 0;
    for (int i = 0; i < atts.getLength(); i++) {
        String uri = atts.getURI(i);
        if (uri != null) {
            String qn = atts.getQName(i);
            errorAttrs++;
            addAttributeError(qn, 
                "property-namespaced-attribute",
                ERR_propertyElementNamespacedAttribute(qn),
                qn
            );
        }
    }
    
    int stProp = FxXmlSymbols.findStaticProperty(propName);
    switch (stProp) {
        case -1:
            // simple property
            if (!Utilities.isJavaIdentifier(propName)) {
                addError(new ErrorMark(
                    start, end,
                    "invalid-property-name",
                    ERR_invalidPropertyName(propName),
                    propName
                ));
            }
            if (errorAttrs == atts.getLength()) {
                pv = handleSimpleProperty(propName, atts);
            } else {
                pv = handleMapProperty(propName, atts);
            }
            break;
            
        case -2:
            // broken name, but must create a node
            pv = accessor.makeBroken(accessor.createProperty(propName, false));
            // do not add the property to the parent, it's broken beyond repair
            addError(new ErrorMark(
                start, end,
                "invalid-property-name",
                ERR_invalidPropertyName(propName),
                propName
            ));
            break;
            
        default:
            // static property, just ignore for now
            pv = handleStaticProperty(propName.substring(0, stProp), 
                    propName.substring(stProp + 1), atts);
            break;
    }
    
    return pv;
}
 
Example 17
Source File: SetPropertyRule.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Process the beginning of this element.
 *
 * @param namespace the namespace URI of the matching element, or an 
 *   empty string if the parser is not namespace aware or the element has
 *   no namespace
 * @param theName the local name if the parser is namespace aware, or just 
 *   the element name otherwise
 * @param attributes The attribute list for this element
 * 
 * @exception NoSuchMethodException if the bean does not
 *  have a writable property of the specified name
 */
@Override
public void begin(String namespace, String theName, Attributes attributes)
        throws Exception {

    // Identify the actual property name and value to be used
    String actualName = null;
    String actualValue = null;
    for (int i = 0; i < attributes.getLength(); i++) {
        String name = attributes.getLocalName(i);
        if ("".equals(name)) {
            name = attributes.getQName(i);
        }
        String value = attributes.getValue(i);
        if (name.equals(this.name)) {
            actualName = value;
        } else if (name.equals(this.value)) {
            actualValue = value;
        }
    }

    // Get a reference to the top object
    Object top = digester.peek();

    // Log some debugging information
    if (digester.log.isDebugEnabled()) {
        digester.log.debug("[SetPropertyRule]{" + digester.match +
                "} Set " + top.getClass().getName() + " property " +
                actualName + " to " + actualValue);
    }

    // Set the property (with conversion as necessary)
    if (!digester.isFakeAttribute(top, actualName) 
            && !IntrospectionUtils.setProperty(top, actualName, actualValue) 
            && digester.getRulesValidation()) {
        digester.log.warn("[SetPropertyRule]{" + digester.match +
                "} Setting property '" + name + "' to '" +
                value + "' did not find a matching property.");
    }

}
 
Example 18
Source File: DOMBuilder.java    From jdk1.8-source-analysis 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);
  }

}
 
Example 19
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);
  }

}
 
Example 20
Source File: DOMBuilder.java    From openjdk-jdk8u-backup 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);
  }

}