Example usage for org.xml.sax Attributes getQName

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

Introduction

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

Prototype

public abstract String getQName(int index);

Source Link

Document

Look up an attribute's XML qualified (prefixed) name by index.

Usage

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
 *//*w w w .  jav a2  s. co 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.axis.message.MessageElement.java

/**
 * Advanced constructor used for deserialization.
 * <ol>//from   ww  w  . j a  va 2s . com
 * <li>The context provides the mappings and Sax event recorder
 * <li>The soap messaging style is determined from the current message context, defaulting
 * to SOAP1.1 if there is no current context.
 * <li>if there is an id attribute (any namespace), then the ID is registered
 * with {@link DeserializationContext#registerElementByID(String, MessageElement)} ;a  new recorder is
 * created if needed.
 * <li>If there is an attribute "root" in the default SOAP namespace, then it is examined
 * to see if it marks the element as root (value=="1" or not)
 * <li>If there is an arrayType attribute then we assume we are an array and set our
 * {@link #typeQName} field appropriately.
 * <li>The {@link #href} field is set if there is a relevant href value
 * </ol>
 *
 * @param namespace namespace namespace of element
 * @param localPart local name local name of element
 * @param prefix prefix prefix of element
 * @param attributes attributes to save as our attributes
 * @param context deserialization context for this message element
 * @throws AxisFault if the encoding style is not recognized/supported
 */
public MessageElement(String namespace, String localPart, String prefix, Attributes attributes,
        DeserializationContext context) throws AxisFault {
    if (log.isDebugEnabled()) {
        log.debug(Messages.getMessage("newElem00", super.toString(), "{" + prefix + "}" + localPart));
        for (int i = 0; attributes != null && i < attributes.getLength(); i++) {
            log.debug("  " + attributes.getQName(i) + " = '" + attributes.getValue(i) + "'");
        }
    }
    this.namespaceURI = namespace;
    this.name = localPart;
    this.prefix = prefix;

    this.context = context;
    this.startEventIndex = context.getStartOfMappingsPos();

    setNSMappings(context.getCurrentNSMappings());

    this.recorder = context.getRecorder();

    if (attributes != null && attributes.getLength() > 0) {
        this.attributes = attributes;

        this.typeQName = context.getTypeFromAttributes(namespace, localPart, attributes);

        String rootVal = attributes.getValue(Constants.URI_DEFAULT_SOAP_ENC, Constants.ATTR_ROOT);

        if (rootVal != null) {
            _isRoot = "1".equals(rootVal);
        }

        id = attributes.getValue(Constants.ATTR_ID);
        // Register this ID with the context.....
        if (id != null) {
            context.registerElementByID(id, this);
            if (recorder == null) {
                recorder = new SAX2EventRecorder();
                context.setRecorder(recorder);
            }
        }

        // Set the encoding style to the attribute value.  If null,
        // we just automatically use our parent's (see getEncodingStyle)
        MessageContext mc = context.getMessageContext();
        SOAPConstants sc = (mc != null) ? mc.getSOAPConstants() : SOAPConstants.SOAP11_CONSTANTS;

        href = attributes.getValue(sc.getAttrHref());

        // If there's an arrayType attribute, we can pretty well guess that we're an Array???
        if (attributes.getValue(Constants.URI_DEFAULT_SOAP_ENC, Constants.ATTR_ARRAY_TYPE) != null) {
            typeQName = Constants.SOAP_ARRAY;
        }

        encodingStyle = attributes.getValue(sc.getEncodingURI(), Constants.ATTR_ENCODING_STYLE);

        // if no-encoding style was defined, we don't define as well
        if (Constants.URI_SOAP12_NOENC.equals(encodingStyle))
            encodingStyle = null;

        // If we have an encoding style, and are not a MESSAGE style
        // operation (in other words - we're going to do some data
        // binding), AND we're SOAP 1.2, check the encoding style against
        // the ones we've got type mappings registered for.  If it isn't
        // registered, throw a DataEncodingUnknown fault as per the
        // SOAP 1.2 spec.
        if (encodingStyle != null && sc.equals(SOAPConstants.SOAP12_CONSTANTS)
                && (mc.getOperationStyle() != Style.MESSAGE)) {
            TypeMapping tm = mc.getTypeMappingRegistry().getTypeMapping(encodingStyle);
            if (tm == null || (tm.equals(mc.getTypeMappingRegistry().getDefaultTypeMapping()))) {
                AxisFault badEncodingFault = new AxisFault(Constants.FAULT_SOAP12_DATAENCODINGUNKNOWN,
                        "bad encoding style", null, null);
                throw badEncodingFault;
            }
        }

    }
}

From source file:org.apache.catalina.startup.SetAllPropertiesRule.java

/**
 * Handle the beginning of an XML element.
 *
 * @param attributes The attributes of this element
 *
 * @exception Exception if a processing error occurs
 *///w w w  . j av  a 2 s  .  co  m
public void begin(Attributes attributes) throws Exception {

    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);
        IntrospectionUtils.setProperty(digester.peek(), name, value);
    }

}

From source file:org.apache.cayenne.map.MapLoader.java

/** Prints the attributes. Used for error reporting purposes. */
private StringBuffer printAttributes(Attributes atts) {
    StringBuffer sb = new StringBuffer();
    String name, value;/*from  w  w  w  . j  av a 2s. c o m*/
    for (int i = 0; i < atts.getLength(); i++) {
        value = atts.getQName(i);
        name = atts.getValue(i);
        sb.append("Name: ").append(name).append("\tValue: ").append(value).append("\n");
    }
    return sb;
}

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)
 *//*from  w w  w .j  a va 2  s .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);
    }
}

From source file:org.apache.cocoon.components.serializers.EncodingSerializer.java

/**
 * Receive notification of the beginning of an element.
 *///from  ww w . j  av a2 s.  com
public void startElement(String nsuri, String local, String qual, Attributes attributes) throws SAXException {
    if (indentPerLevel > 0) {
        this.writeIndent(indentPerLevel * level);
        level++;
    }

    String name = this.namespaces.qualify(nsuri, local, qual);

    if (this.prolog) {
        this.body(nsuri, local, name);
        this.prolog = false;
    }

    String ns[][] = this.namespaces.commit();

    String at[][] = new String[attributes.getLength()][4];
    for (int x = 0; x < at.length; x++) {
        at[x][ATTRIBUTE_NSURI] = attributes.getURI(x);
        at[x][ATTRIBUTE_LOCAL] = attributes.getLocalName(x);
        at[x][ATTRIBUTE_QNAME] = namespaces.qualify(attributes.getURI(x), attributes.getLocalName(x),
                attributes.getQName(x));
        at[x][ATTRIBUTE_VALUE] = attributes.getValue(x);
    }

    this.startElementImpl(nsuri, local, name, ns, at);
}

From source file:org.apache.ddlutils.io.SetColumnPropertyFromSubElementRule.java

/**
 * {@inheritDoc}/*from w  w w .  ja v a 2  s.  c o  m*/
 */
public void begin(Attributes attributes) throws Exception {
    for (int idx = 0; idx < attributes.getLength(); idx++) {
        String attrName = attributes.getLocalName(idx);

        if ("".equals(attrName)) {
            attrName = attributes.getQName(idx);
        }
        if (DatabaseIO.BASE64_ATTR_NAME.equals(attrName) && "true".equalsIgnoreCase(attributes.getValue(idx))) {
            _usesBase64 = true;
            break;
        }
    }
}

From source file:org.apache.ddlutils.io.SetColumnPropertyRule.java

/**
 * {@inheritDoc}/*from www . j a  va 2 s . c  o m*/
 */
public void begin(Attributes attributes) throws Exception {
    Object bean = digester.peek();

    for (int idx = 0; idx < attributes.getLength(); idx++) {
        String attrName = attributes.getLocalName(idx);

        if ("".equals(attrName)) {
            attrName = attributes.getQName(idx);
        }
        if ((_caseSensitive && attrName.equals(_column.getName()))
                || (!_caseSensitive && attrName.equalsIgnoreCase(_column.getName()))) {
            String attrValue = attributes.getValue(idx);
            Object propValue = (_converter != null
                    ? _converter.convertFromString(attrValue, _column.getTypeCode())
                    : attrValue);

            if (digester.getLogger().isDebugEnabled()) {
                digester.getLogger().debug("[SetColumnPropertyRule]{" + digester.getMatch()
                        + "} Setting property '" + _column.getName() + "' to '" + propValue + "'");
            }

            PropertyUtils.setProperty(bean, _column.getName(), propValue);
        }
    }
}

From source file:org.apache.fop.fo.PropertyList.java

/**
 * <p>Adds the attributes, passed in by the parser to the PropertyList.</p>
 * <p>Note that certain attributes are given priority in terms of order of
 * processing due to conversion dependencies, where the order is as follows:</p>
 * <ol>/*  ww w .ja  v  a2  s. c o m*/
 * <li>writing-mode</li>
 * <li>column-number</li>
 * <li>number-columns-spanned</li>
 * <li>font</li>
 * <li>font-size</li>
 * <li><emph>all others in order of appearance</emph></li>
 * </ol>
 *
 * @param attributes Collection of attributes passed to us from the parser.
 * @throws ValidationException if there is an attribute that does not
 *          map to a property id (strict validation only)
 */
public void addAttributesToList(Attributes attributes) throws ValidationException {
    /*
     * Give writing-mode highest conversion priority.
     */
    addAttributeToList(attributes, "writing-mode");

    /*
     * If column-number/number-columns-spanned are specified, then we
     * need them before all others (possible from-table-column() on any
     * other property further in the list...
     */
    addAttributeToList(attributes, "column-number");
    addAttributeToList(attributes, "number-columns-spanned");

    /*
     * If font-size is set on this FO, must set it first, since
     * other attributes specified in terms of "ems" depend on it.
     */
    String checkValue = addAttributeToList(attributes, "font");
    if (checkValue == null || "".equals(checkValue)) {
        /*
         * font shorthand wasn't specified, so still need to process
         * explicit font-size
         */
        addAttributeToList(attributes, "font-size");
    }

    String attributeNS;
    String attributeName;
    String attributeValue;
    FopFactory factory = getFObj().getUserAgent().getFactory();
    for (int i = 0; i < attributes.getLength(); i++) {
        /* convert all attributes with the same namespace as the fo element
         * the "xml:lang" and "xml:base" properties are special cases */
        attributeNS = attributes.getURI(i);
        attributeName = attributes.getQName(i);
        attributeValue = attributes.getValue(i);
        if (attributeNS == null || attributeNS.length() == 0 || "xml:lang".equals(attributeName)
                || "xml:base".equals(attributeName)) {
            convertAttributeToProperty(attributes, attributeName, attributeValue);
        } else if (!factory.isNamespaceIgnored(attributeNS)) {
            ElementMapping mapping = factory.getElementMappingRegistry().getElementMapping(attributeNS);
            QName attr = new QName(attributeNS, attributeName);
            if (mapping != null) {
                if (mapping.isAttributeProperty(attr) && mapping.getStandardPrefix() != null) {
                    convertAttributeToProperty(attributes,
                            mapping.getStandardPrefix() + ":" + attr.getLocalName(), attributeValue);
                } else {
                    getFObj().addForeignAttribute(attr, attributeValue);
                }
            } else {
                handleInvalidProperty(attr);
            }
        }
    }
}

From source file:org.apache.geode.internal.cache.xmlcache.CacheXmlParser.java

private void mapJNDI(Attributes atts, Map gfSpecific) {
    int attsLen = atts.getLength();
    String key = "";
    String value = "";
    // put attributes into a Map
    for (int i = 0; i < attsLen; i++) {
        key = atts.getQName(i);
        value = atts.getValue(key);/*from   www  .j a  va  2 s.  c  o  m*/
        gfSpecific.put(key, value);
    }
}