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:com.polarion.alm.ws.client.internal.encoding.BeanDeserializer.java

/**
 * Set the bean properties that correspond to element attributes.
 * //from w  w w  .  ja  v  a  2  s . c om
 * 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());

                // The java type is an array, but the context didn't
                // know that we are an attribute. Better stick with
                // simple types..
                if (dSer instanceof ArrayDeserializer) {
                    SimpleListDeserializerFactory factory = new SimpleListDeserializerFactory(bpd.getType(),
                            fieldDesc.getXmlType());
                    dSer = (Deserializer) factory.getDeserializerAs(dSer.getMechanismType());
                }
            }

            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:SAXTreeValidator.java

/**
 * <p>/*  w  ww  .  j av  a 2 s .  com*/
 *   This reports the occurrence of an actual element. It includes
 *     the element's attributes, with the exception of XML vocabulary
 *     specific attributes, such as
 *     <code>xmlns:[namespace prefix]</code> and
 *     <code>xsi:schemaLocation</code>.
 * </p>
 *
 * @param namespaceURI <code>String</code> namespace URI this element
 *               is associated with, or an empty <code>String</code>
 * @param localName <code>String</code> name of element (with no
 *               namespace prefix, if one is present)
 * @param qName <code>String</code> XML 1.0 version of element name:
 *                [namespace prefix]:[localName]
 * @param atts <code>Attributes</code> list for this element
 * @throws <code>SAXException</code> when things go wrong
 */
public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
        throws SAXException {

    DefaultMutableTreeNode element = new DefaultMutableTreeNode("Element: " + localName);
    current.add(element);
    current = element;

    // Determine namespace
    if (namespaceURI.length() > 0) {
        String prefix = (String) namespaceMappings.get(namespaceURI);
        if (prefix.equals("")) {
            prefix = "[None]";
        }
        DefaultMutableTreeNode namespace = new DefaultMutableTreeNode(
                "Namespace: prefix = '" + prefix + "', URI = '" + namespaceURI + "'");
        current.add(namespace);
    }

    // Process attributes
    for (int i = 0; i < atts.getLength(); i++) {
        DefaultMutableTreeNode attribute = new DefaultMutableTreeNode(
                "Attribute (name = '" + atts.getLocalName(i) + "', value = '" + atts.getValue(i) + "')");
        String attURI = atts.getURI(i);
        if (attURI.length() > 0) {
            String attPrefix = (String) namespaceMappings.get(namespaceURI);
            if (attPrefix.equals("")) {
                attPrefix = "[None]";
            }
            DefaultMutableTreeNode attNamespace = new DefaultMutableTreeNode(
                    "Namespace: prefix = '" + attPrefix + "', URI = '" + attURI + "'");
            attribute.add(attNamespace);
        }
        current.add(attribute);
    }
}

From source file:XMLWriter.java

/**
 * Write out an attribute list, escaping values.
 *
 * The names will have prefixes added to them.
 *
 * @param atts The attribute list to write.
 * @exception org.xml.SAXException If there is an error writing
 *            the attribute list, this method will throw an
 *            IOException wrapped in a SAXException.
 */// w w  w.  jav a  2  s . c om
private void writeAttributes(Attributes atts) throws SAXException {
    int len = atts.getLength();
    for (int i = 0; i < len; i++) {
        char ch[] = atts.getValue(i).toCharArray();
        write(' ');
        writeName(atts.getURI(i), atts.getLocalName(i), atts.getQName(i), false);
        if (htmlMode && booleanAttribute(atts.getLocalName(i), atts.getQName(i), atts.getValue(i)))
            break;
        write("=\"");
        writeEsc(ch, 0, ch.length, true);
        write('"');
    }
}

From source file:DocumentTracer.java

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

    printIndent();// w w  w.j  av  a  2s .c o  m
    fOut.print("startElement(");
    fOut.print("uri=");
    printQuotedString(uri);
    fOut.print(',');
    fOut.print("localName=");
    printQuotedString(localName);
    fOut.print(',');
    fOut.print("qname=");
    printQuotedString(qname);
    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 attrLocalName = attributes.getLocalName(i);
            String attrQName = attributes.getQName(i);
            String attrURI = attributes.getURI(i);
            String attrType = attributes.getType(i);
            String attrValue = attributes.getValue(i);
            fOut.print('{');
            fOut.print("uri=");
            printQuotedString(attrURI);
            fOut.print(',');
            fOut.print("localName=");
            printQuotedString(attrLocalName);
            fOut.print(',');
            fOut.print("qname=");
            printQuotedString(attrQName);
            fOut.print(',');
            fOut.print("type=");
            printQuotedString(attrType);
            fOut.print(',');
            fOut.print("value=");
            printQuotedString(attrValue);
            fOut.print('}');
        }
        fOut.print('}');
    }
    fOut.println(')');
    fOut.flush();
    fIndent++;

}

From source file:org.apache.axiom.om.impl.serialize.StreamWriterToContentHandlerConverter.java

/**
 * Method startElement.//from  ww w  .ja  v  a  2s .c  o m
 *
 * @param namespaceURI
 * @param localName
 * @param qName
 * @param atts
 * @throws SAXException
 */
public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
        throws SAXException {
    try {
        log.info("writing element {" + namespaceURI + '}' + localName + " directly to stream ");
        String prefix = getPrefix(qName);

        // it is only the prefix we want to learn from the QName! so we can get rid of the
        // spliting QName
        if (prefix == null) {
            writer.writeStartElement(namespaceURI, localName);
        } else {
            writer.writeStartElement(prefix, localName, namespaceURI);
        }
        if (atts != null) {
            int attCount = atts.getLength();
            for (int i = 0; i < attCount; i++) {
                writer.writeAttribute(atts.getURI(i), localName, atts.getValue(i));
            }
        }
    } catch (XMLStreamException e) {
        throw new SAXException(e);
    }
}

From source file:org.apache.axis.encoding.DeserializationContext.java

/**
 * Create a QName for the type of the element defined by localName and
 * namespace with the specified attributes.
 * @param namespace of the element//from ww  w .j  a v  a 2 s . co  m
 * @param localName is the local name of the element
 * @param attrs are the attributes on the element
 */
public QName getTypeFromAttributes(String namespace, String localName, Attributes attrs) {
    QName typeQName = getTypeFromXSITypeAttr(namespace, localName, attrs);
    if ((typeQName == null) && Constants.isSOAP_ENC(namespace)) {

        // If the element is a SOAP-ENC element, the name of the element is the type.
        // If the default type mapping accepts SOAP 1.2, then use then set
        // the typeQName to the SOAP-ENC type.
        // Else if the default type mapping accepts SOAP 1.1, then
        // convert the SOAP-ENC type to the appropriate XSD Schema Type.
        if (namespace.equals(Constants.URI_SOAP12_ENC)) {
            typeQName = new QName(namespace, localName);
        } else if (localName.equals(Constants.SOAP_ARRAY.getLocalPart())) {
            typeQName = Constants.SOAP_ARRAY;
        } else if (localName.equals(Constants.SOAP_STRING.getLocalPart())) {
            typeQName = Constants.SOAP_STRING;
        } else if (localName.equals(Constants.SOAP_BOOLEAN.getLocalPart())) {
            typeQName = Constants.SOAP_BOOLEAN;
        } else if (localName.equals(Constants.SOAP_DOUBLE.getLocalPart())) {
            typeQName = Constants.SOAP_DOUBLE;
        } else if (localName.equals(Constants.SOAP_FLOAT.getLocalPart())) {
            typeQName = Constants.SOAP_FLOAT;
        } else if (localName.equals(Constants.SOAP_INT.getLocalPart())) {
            typeQName = Constants.SOAP_INT;
        } else if (localName.equals(Constants.SOAP_LONG.getLocalPart())) {
            typeQName = Constants.SOAP_LONG;
        } else if (localName.equals(Constants.SOAP_SHORT.getLocalPart())) {
            typeQName = Constants.SOAP_SHORT;
        } else if (localName.equals(Constants.SOAP_BYTE.getLocalPart())) {
            typeQName = Constants.SOAP_BYTE;
        }
    }

    // If we still have no luck, check to see if there's an arrayType
    // (itemType for SOAP 1.2) attribute, in which case this is almost
    // certainly an array.

    if (typeQName == null && attrs != null) {
        String encURI = getSOAPConstants().getEncodingURI();
        String itemType = getSOAPConstants().getAttrItemType();
        for (int i = 0; i < attrs.getLength(); i++) {
            if (encURI.equals(attrs.getURI(i)) && itemType.equals(attrs.getLocalName(i))) {
                return new QName(encURI, "Array");
            }
        }
    }

    return typeQName;
}

From source file:org.apache.axis.encoding.ser.BeanDeserializer.java

/**
 * Set the bean properties that correspond to element attributes.
 * // w  w  w .j a  v a2 s .c  o 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());

                // The java type is an array, but the context didn't
                // know that we are an attribute.  Better stick with
                // simple types..
                if (dSer instanceof ArrayDeserializer) {
                    SimpleListDeserializerFactory factory = new SimpleListDeserializerFactory(bpd.getType(),
                            fieldDesc.getXmlType());
                    dSer = (Deserializer) factory.getDeserializerAs(dSer.getMechanismType());
                }
            }

            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:org.apache.axis.encoding.SerializationContext.java

/**
 * Writes (using the Writer) the start tag for element QName along with the
 * indicated attributes and namespace mappings.
 * @param qName is the name of the element
 * @param attributes are the attributes to write
 */// w  ww.  j ava  2  s .  c  o m
public void startElement(QName qName, Attributes attributes) throws IOException {
    java.util.ArrayList vecQNames = null;
    if (debugEnabled) {
        log.debug(Messages.getMessage("startElem00",
                "[" + qName.getNamespaceURI() + "]:" + qName.getLocalPart()));
    }

    if (startOfDocument && sendXMLDecl) {
        writeXMLDeclaration();
    }

    if (writingStartTag) {
        writer.write('>');
        if (pretty)
            writer.write('\n');
        indent++;
    }

    if (pretty)
        for (int i = 0; i < indent; i++)
            writer.write(' ');
    String elementQName = qName2String(qName, true);
    writer.write('<');

    writer.write(elementQName);

    if (writeXMLType != null) {
        attributes = setTypeAttribute(attributes, writeXMLType);
        writeXMLType = null;
    }

    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            String qname = attributes.getQName(i);
            writer.write(' ');

            String prefix = "";
            String uri = attributes.getURI(i);
            if (uri != null && uri.length() > 0) {
                if (qname.length() == 0) {
                    // If qname isn't set, generate one
                    prefix = getPrefixForURI(uri);
                } else {
                    // If it is, make sure the prefix looks reasonable.
                    int idx = qname.indexOf(':');
                    if (idx > -1) {
                        prefix = qname.substring(0, idx);
                        prefix = getPrefixForURI(uri, prefix, true);
                    }
                }
                if (prefix.length() > 0) {
                    qname = prefix + ':' + attributes.getLocalName(i);
                } else {
                    qname = attributes.getLocalName(i);
                }
            } else {
                qname = attributes.getQName(i);
                if (qname.length() == 0)
                    qname = attributes.getLocalName(i);
            }

            if (qname.startsWith("xmlns")) {
                if (vecQNames == null)
                    vecQNames = new ArrayList();
                vecQNames.add(qname);
            }
            writer.write(qname);
            writer.write("=\"");

            getEncoder().writeEncoded(writer, attributes.getValue(i));

            writer.write('"');
        }
    }

    if (noNamespaceMappings) {
        nsStack.push();
    } else {
        for (Mapping map = nsStack.topOfFrame(); map != null; map = nsStack.next()) {
            if (!(map.getNamespaceURI().equals(Constants.NS_URI_XMLNS) && map.getPrefix().equals("xmlns"))
                    && !(map.getNamespaceURI().equals(Constants.NS_URI_XML) && map.getPrefix().equals("xml"))) {
                StringBuffer sb = new StringBuffer("xmlns");
                if (map.getPrefix().length() > 0) {
                    sb.append(':');
                    sb.append(map.getPrefix());
                }
                if ((vecQNames == null) || (vecQNames.indexOf(sb.toString()) == -1)) {
                    writer.write(' ');
                    sb.append("=\"");
                    sb.append(map.getNamespaceURI());
                    sb.append('"');
                    writer.write(sb.toString());
                }
            }
        }

        noNamespaceMappings = true;
    }

    writingStartTag = true;

    elementStack.push(elementQName);

    onlyXML = true;
}

From source file:org.apache.axis.encoding.SerializationContextImpl.java

/**
 * Writes (using the Writer) the start tag for element QName along with the
 * indicated attributes and namespace mappings.
 * @param qName is the name of the element
 * @param attributes are the attributes to write
 *//*from  w w w .j av  a 2  s  .  c  o m*/
public void startElement(QName qName, Attributes attributes)
    throws IOException
{
    java.util.ArrayList vecQNames = null;
    if (log.isDebugEnabled()) {
        log.debug(Messages.getMessage("startElem00",
                "[" + qName.getNamespaceURI() + "]:" + qName.getLocalPart()));
    }

    if (startOfDocument && sendXMLDecl) {
        writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        startOfDocument = false;
    }

    if (writingStartTag) {
        writer.write('>');
        if (pretty) writer.write('\n');
        indent++;
    }

    if (pretty) for (int i=0; i<indent; i++) writer.write(' ');
    String elementQName = qName2String(qName, true);
    writer.write('<');

    writer.write(elementQName);

    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            String qname = attributes.getQName(i);
            writer.write(' ');

            String prefix = "";
            String uri = attributes.getURI(i);
            if (uri != null && uri.length() > 0) {
                if (qname.length() == 0) {
                    // If qname isn't set, generate one
                    prefix = getPrefixForURI(uri);
                } else {
                    // If it is, make sure the prefix looks reasonable.
                    int idx = qname.indexOf(':');
                    if (idx > -1) {
                        prefix = qname.substring(0, idx);
                        prefix = getPrefixForURI(uri,
                                                 prefix, true);
                    }
                }
                if (prefix.length() > 0) {
                    qname = prefix + ':' + attributes.getLocalName(i);
                } else {
                    qname = attributes.getLocalName(i);
                }
            } else {
               qname = attributes.getQName(i);
                if(qname.length() == 0)
                    qname = attributes.getLocalName(i);
            }

            if (qname.startsWith("xmlns")) {
              if (vecQNames == null) vecQNames = new ArrayList();
              vecQNames.add(qname);
            }
            writer.write(qname);
            writer.write("=\"");
            writer.write(XMLUtils.xmlEncodeString(attributes.getValue(i)));
            writer.write('"');
        }
    }

    if (noNamespaceMappings) {
        nsStack.push();
    } else {
        for (Mapping map=nsStack.topOfFrame(); map!=null; map=nsStack.next()) {
            StringBuffer sb = new StringBuffer("xmlns");
            if (map.getPrefix().length() > 0) {
                sb.append(':');
                sb.append(map.getPrefix());
            }
            if ((vecQNames==null) || (vecQNames.indexOf(sb.toString())==-1)) {
                writer.write(' ');
                sb.append("=\"");
                sb.append(map.getNamespaceURI());
                sb.append('"');
                writer.write(sb.toString());
            }
        }

        noNamespaceMappings = true;
    }

    writingStartTag = true;

    elementStack.push(elementQName);

    onlyXML=true;
}

From source file:org.apache.cocoon.components.language.markup.xsp.XSPExpressionFilter.java

/**
 * Start a new element. If attribute value templates are enabled and the element has attributes
 * with templates, these are replaced by xsp:attribute tags.
 *
 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String,
 *      java.lang.String, org.xml.sax.Attributes)
 */// w w  w.ja v  a  2s. c o  m
public void startElement(String namespaceURI, String localName, String qName, Attributes attribs)
        throws SAXException {
    expressionParser.flush(locator, "...<" + qName + ">");

    // Check template for interpolation flags
    attribs = pushInterpolationStack(attribs);

    if (getInterpolationSettings().attrInterpolation) {
        // Attribute value templates enabled => process attributes
        AttributesImpl staticAttribs = new AttributesImpl();
        AttributesImpl dynamicAttribs = new AttributesImpl();

        // Gather attributes with and without templates separately
        for (int i = 0; i < attribs.getLength(); ++i) {
            String value = attribs.getValue(i);

            if (value.indexOf("{#") != -1) {
                // The attribute contains templates
                dynamicAttribs.addAttribute(attribs.getURI(i), attribs.getLocalName(i), attribs.getQName(i),
                        attribs.getType(i), value);
            } else {
                // The attribute does not contain templates
                staticAttribs.addAttribute(attribs.getURI(i), attribs.getLocalName(i), attribs.getQName(i),
                        attribs.getType(i), value);
            }
        }

        // Start the element with template-free attributes
        super.startElement(namespaceURI, localName, qName, staticAttribs);

        // Generate xsp:attribute elements for the attributes containing templates
        for (int i = 0; i < dynamicAttribs.getLength(); ++i) {
            AttributesImpl elemAttribs = new AttributesImpl();
            addAttribute(elemAttribs, "uri", dynamicAttribs.getURI(i));

            String qname = dynamicAttribs.getQName(i);

            if (qname != null) {
                addAttribute(elemAttribs, "prefix", StringUtils.left(qname, qname.indexOf(':')));
            }

            String attrName = dynamicAttribs.getLocalName(i);
            addAttribute(elemAttribs, "name", attrName);

            super.startElement(markupURI, "attribute", markupPrefix + ":attribute", elemAttribs);
            expressionParser.consume(dynamicAttribs.getValue(i));
            expressionParser.flush(locator, "<" + qName + " " + attrName + "=\"...\">");
            super.endElement(markupURI, "attribute", markupPrefix + ":attribute");
        }
    } else {
        // Attribute value templates disabled => pass through element
        super.startElement(namespaceURI, localName, qName, attribs);
    }
}