Example usage for org.xml.sax Attributes getURI

List of usage examples for org.xml.sax Attributes getURI

Introduction

In this page you can find the example usage for org.xml.sax Attributes getURI.

Prototype

public abstract String getURI(int index);

Source Link

Document

Look up an attribute's Namespace URI by index.

Usage

From source file:no.kantega.commons.util.XMLHelper.java

public static AttributesImpl getAttributesImpl(Attributes attributes) {
    AttributesImpl impl = new AttributesImpl();

    for (int i = 0; i < attributes.getLength(); i++) {
        impl.addAttribute(attributes.getURI(i), attributes.getLocalName(i), attributes.getQName(i),
                attributes.getType(i), attributes.getValue(i));
    }/*from  w w  w  .ja  v a  2 s  .  co m*/
    return impl;
}

From source file:com.swtxml.tinydom.TinyDomSaxHandler.java

private Map<INamespaceDefinition, Map<IAttributeDefinition, String>> processAttributes(
        INamespaceDefinition tagNamespace, ITagDefinition tagDefinition, Attributes attributes) {

    Map<INamespaceDefinition, Map<IAttributeDefinition, String>> attributeNsMap = new HashMap<INamespaceDefinition, Map<IAttributeDefinition, String>>();
    for (int i = 0; i < attributes.getLength(); i++) {
        String uri = attributes.getURI(i);
        INamespaceDefinition attributeNamespace = !StringUtils.isEmpty(uri) ? getNamespace(uri) : tagNamespace;
        Map<IAttributeDefinition, String> attributeMap = attributeNsMap.get(attributeNamespace);
        if (attributeMap == null) {
            attributeMap = new HashMap<IAttributeDefinition, String>();
            attributeNsMap.put(attributeNamespace, attributeMap);
        }/*from w w w.j a v  a  2s.  com*/
        String name = attributes.getLocalName(i);
        String value = attributes.getValue(i);
        IAttributeDefinition attributeDefinition;
        if (attributeNamespace.equals(tagNamespace)) {
            attributeDefinition = tagDefinition.getAttribute(name);
        } else {
            attributeDefinition = attributeNamespace.getForeignAttribute(name);
            if (attributeDefinition instanceof ITagScope
                    && !((ITagScope) attributeDefinition).isAllowedIn(tagDefinition)) {
                throw new ParseException("Attribute " + attributes.getQName(i) + " is not allowed for tag \""
                        + tagDefinition.getName() + "\"");
            }
        }

        if (attributeDefinition == null) {
            throw new ParseException("Unknown attribute \"" + attributes.getQName(i) + "\" for tag \""
                    + tagDefinition.getName() + "\" (available are: "
                    + CollectionUtils.sortedToString(tagDefinition.getAttributeNames()) + ")");
        }
        attributeMap.put(attributeDefinition, value);

    }
    if (attributeNsMap.isEmpty()) {
        return Collections.emptyMap();
    }
    return attributeNsMap;
}

From source file:net.sf.joost.emitter.DOMEmitter.java

/**
 * SAX2-Callback - Creates a DOM-element-node and memorizes it for the
 * {@link #endElement(String ,String ,String)} method by putting it onto the
 * top of this stack.// w ww .j  ava 2s  .  c o  m
 */
public void startElement(String uri, String local, String raw, Attributes attrs) throws SAXException {
    // create new element : iterate over all attribute-values
    Element elem = document.createElementNS(uri, raw);
    int nattrs = attrs.getLength();
    for (int i = 0; i < nattrs; i++) {
        String namespaceuri = attrs.getURI(i);
        String value = attrs.getValue(i);
        String qName = attrs.getQName(i);
        if ((namespaceuri == null) || (namespaceuri.equals(""))) {
            elem.setAttribute(qName, value);
        } else {
            elem.setAttributeNS(namespaceuri, qName, value);
        }
    }

    // append this new node onto current stack node
    insertNode(elem);
    // push this node into the global stack
    stack.push(elem);
}

From source file:fulcrum.xml.Parser.java

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
        throws SAXException {
    textBuilder.setLength(0);//w  w w  .ja  v a  2s.com
    Element c = new Element(qName, uri);
    xPathBuilder.append(FORWARD_SLASH).append(localName);

    addXPath(xPathBuilder.toString(), c);
    for (int i = 0; i < attributes.getLength(); i++) {
        Attribute a = new Attribute(attributes.getQName(i), attributes.getURI(i), attributes.getValue(i));
        addXPath(xPathBuilder.toString() + AT + a.getLocalName(), a);
        c.addAttribute(a);
    }
    elements.push(c);
}

From source file:com.ibm.jaql.lang.expr.xml.XmlToJsonFn.java

@Override
public void startElement(String uri, String localName, String name, Attributes attrs) throws SAXException {
    try {//w  w w .j a  va 2s .c  o  m
        SpilledJsonArray ca = new SpilledJsonArray();
        int n = attrs.getLength();
        for (int i = 0; i < n; i++) {
            name = "@" + attrs.getLocalName(i);
            uri = attrs.getURI(i);
            String v = attrs.getValue(i);
            BufferedJsonRecord r = new BufferedJsonRecord();
            if (uri != null && uri.length() > 0) {
                r.add(S_XMLNS, new JsonString(uri));
            }
            r.add(new JsonString(name), new JsonString(v));
            ca.addCopy(r);
        }
        stack.push(ca);
    } catch (IOException e) {
        throw new UndeclaredThrowableException(e);
    }
}

From source file:com.ibm.jaql.lang.expr.xml.TypedXmlToJsonFn.java

/**
 * Create a JSON record for new node. The node attributes are processed.
 *///from  w  w w .j av a2 s .co m
@Override
public void startElement(String uri, String localName, String name, Attributes attrs) throws SAXException {
    BufferedJsonRecord node = new BufferedJsonRecord();
    stack.push(node);
    lengths.add(text.length());
    int n = attrs.getLength();
    BufferedJsonRecord container;
    for (int i = 0; i < n; i++) {
        uri = attrs.getURI(i);

        // If attribute has a uri
        if (uri != null && uri.length() > 0) {
            JsonString jUri = new JsonString(uri);
            BufferedJsonRecord ur = (BufferedJsonRecord) node.get(jUri);
            if (ur == null) {
                ur = new BufferedJsonRecord();
                node.add(jUri, ur);
            }
            container = ur;
        } else {
            container = node;
        }

        String attrName = "@" + attrs.getLocalName(i);
        JsonString jName = new JsonString(attrName);
        String v = attrs.getValue(i);
        if (container.containsKey(jName)) {
            throw new RuntimeException("duplicate attribute name: " + attrName);
        }
        container.add(jName, cast(v));
    }
}

From source file:com.ibm.jaql.lang.expr.xml.XmlToJsonFn.java

/**
 * Create a JSON record for new node. The node attributes are processed.
 */// ww w . ja v  a2s. c  o m
@Override
public void startElement(String uri, String localName, String name, Attributes attrs) throws SAXException {
    BufferedJsonRecord node = new BufferedJsonRecord();
    stack.push(node);
    lengths.add(text.length());
    int n = attrs.getLength();
    BufferedJsonRecord container;
    for (int i = 0; i < n; i++) {
        uri = attrs.getURI(i);

        // If attribute has a uri
        if (uri != null && uri.length() > 0) {
            JsonString jUri = new JsonString(uri);
            BufferedJsonRecord ur = (BufferedJsonRecord) node.get(jUri);
            if (ur == null) {
                ur = new BufferedJsonRecord();
                node.add(jUri, ur);
            }
            container = ur;
        } else {
            container = node;
        }

        String attrName = "@" + attrs.getLocalName(i);
        JsonString jName = new JsonString(attrName);
        String v = attrs.getValue(i);
        if (container.containsKey(jName)) {
            throw new RuntimeException("duplicate attribute name: " + attrName);
        }
        container.add(jName, new JsonString(v));
    }
}

From source file:Sax2Dom.java

public void startElement(String namespace, String localName, String qName, Attributes attrs) {
    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));
            }//from  w w  w  .  ja  v  a 2 s .co m
        }
        _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));
        }
    }

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

    // Push this node onto stack
    _nodeStk.push(tmp);
}

From source file:com.avalara.avatax.services.base.ser.BeanDeserializer.java

/**
 * Set the bean properties that correspond to element attributes.
 * /*from  ww  w.  java2s.co  m*/
 * This method is invoked after startElement when the element requires
 * deserialization (i.e. the element is not an href and the value is not
 * nil.)
 * @param namespace is the namespace of the element
 * @param localName is the name of the element
 * @param prefix is the prefix of the element
 * @param attributes are the attributes on the element...used to get the
 *                   type
 * @param context is the DeserializationContext
 */
public void onStartElement(String namespace, String localName, String prefix, Attributes attributes,
        DeserializationContext context) throws SAXException {

    // The value should have been created or assigned already.
    // This code may no longer be needed.
    if (value == null && constructorToUse == null) {
        // create a value
        try {
            value = javaType.newInstance();
        } catch (Exception e) {
            throw new SAXException(Messages.getMessage("cantCreateBean00", javaType.getName(), e.toString()));
        }
    }

    // If no type description meta data, there are no attributes,
    // so we are done.
    if (typeDesc == null)
        return;

    // loop through the attributes and set bean properties that 
    // correspond to attributes
    for (int i = 0; i < attributes.getLength(); i++) {
        QName attrQName = new QName(attributes.getURI(i), attributes.getLocalName(i));
        String fieldName = typeDesc.getFieldNameForAttribute(attrQName);
        if (fieldName == null)
            continue;

        FieldDesc fieldDesc = typeDesc.getFieldByName(fieldName);

        // look for the attribute property
        BeanPropertyDescriptor bpd = (BeanPropertyDescriptor) propertyMap.get(fieldName);
        if (bpd != null) {
            if (constructorToUse == null) {
                // check only if default constructor
                if (!bpd.isWriteable() || bpd.isIndexed())
                    continue;
            }

            // Get the Deserializer for the attribute
            Deserializer dSer = getDeserializer(fieldDesc.getXmlType(), bpd.getType(), null, context);
            if (dSer == null) {
                dSer = context.getDeserializerForClass(bpd.getType());
            }
            if (dSer == null)
                throw new SAXException(Messages.getMessage("unregistered00", bpd.getType().toString()));

            if (!(dSer instanceof SimpleDeserializer))
                throw new SAXException(
                        Messages.getMessage("AttrNotSimpleType00", bpd.getName(), bpd.getType().toString()));

            // Success!  Create an object from the string and set
            // it in the bean
            try {
                dSer.onStartElement(namespace, localName, prefix, attributes, context);
                Object val = ((SimpleDeserializer) dSer).makeValue(attributes.getValue(i));
                if (constructorToUse == null) {
                    bpd.set(value, val);
                } else {
                    // add value for our constructor
                    if (constructorTarget == null) {
                        constructorTarget = new ConstructorTarget(constructorToUse, this);
                    }
                    constructorTarget.set(val);
                }
            } catch (Exception e) {
                throw new SAXException(e);
            }

        } // if
    } // attribute loop
}

From source file:TypeInfoWriter.java

/** Start element. */
public void startElement(String uri, String localName, String qname, Attributes attributes)
        throws SAXException {

    TypeInfo type;/*from  w  ww . j  a  va 2s . co  m*/
    printIndent();
    fOut.print("startElement(");
    fOut.print("name=");
    printQName(uri, localName);
    fOut.print(',');
    fOut.print("type=");
    if (fTypeInfoProvider != null && (type = fTypeInfoProvider.getElementTypeInfo()) != null) {
        printQName(type.getTypeNamespace(), type.getTypeName());
    } else {
        fOut.print("null");
    }
    fOut.print(',');
    fOut.print("attributes=");
    if (attributes == null) {
        fOut.println("null");
    } else {
        fOut.print('{');
        int length = attributes.getLength();
        for (int i = 0; i < length; i++) {
            if (i > 0) {
                fOut.print(',');
            }
            String attrURI = attributes.getURI(i);
            String attrLocalName = attributes.getLocalName(i);
            fOut.print('{');
            fOut.print("name=");
            printQName(attrURI, attrLocalName);
            fOut.print(',');
            fOut.print("type=");
            if (fTypeInfoProvider != null && (type = fTypeInfoProvider.getAttributeTypeInfo(i)) != null) {
                printQName(type.getTypeNamespace(), type.getTypeName());
            } else {
                fOut.print("null");
            }
            fOut.print(',');
            fOut.print("id=");
            fOut.print(
                    fTypeInfoProvider != null && fTypeInfoProvider.isIdAttribute(i) ? "\"true\"" : "\"false\"");
            fOut.print(',');
            fOut.print("specified=");
            fOut.print(
                    fTypeInfoProvider == null || fTypeInfoProvider.isSpecified(i) ? "\"true\"" : "\"false\"");
            fOut.print('}');
        }
        fOut.print('}');
    }
    fOut.println(')');
    fOut.flush();
    fIndent++;

}