Example usage for org.xml.sax Attributes getType

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

Introduction

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

Prototype

public abstract String getType(String qName);

Source Link

Document

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

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));
    }//  w  w  w .j  av a  2s. c o  m
    return impl;
}

From source file:MainClass.java

public void startElement(String uri, String localName, String qname, Attributes attr) {
    System.out.println("Start element: local name: " + localName + " qname: " + qname + " uri: " + uri);
    int attrCount = attr.getLength();
    if (attrCount > 0) {
        System.out.println("Attributes:");
        for (int i = 0; i < attrCount; i++) {
            System.out.println("  Name : " + attr.getQName(i));
            System.out.println("  Type : " + attr.getType(i));
            System.out.println("  Value: " + attr.getValue(i));
        }// ww w  .  j  a  v  a 2 s .co  m
    }
}

From source file:Writer.java

/** Returns a sorted list of attributes. */
protected Attributes sortAttributes(Attributes attrs) {

    AttributesImpl attributes = new AttributesImpl();

    int len = (attrs != null) ? attrs.getLength() : 0;
    for (int i = 0; i < len; i++) {
        String name = attrs.getQName(i);
        int count = attributes.getLength();
        int j = 0;
        while (j < count) {
            if (name.compareTo(attributes.getQName(j)) < 0) {
                break;
            }/*  w  w  w. j av a  2 s. c  om*/
            j++;
        }
        attributes.insertAttributeAt(j, name, attrs.getType(i), attrs.getValue(i));
    }

    return attributes;

}

From source file:DocumentTracer.java

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

    printIndent();//from ww  w.  jav a2 s .  c  om
    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.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.  java2  s  .c  om
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.eclipse.smila.connectivity.framework.crawler.web.parse.html.DOMBuilder.java

/**
 * Receive notification of the beginning of an element.
 * //from   ww w . j a v a  2s.c  o m
 * <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.
 * 
 * @throws SAXException
 *           the SAX exception
 * 
 * @see #endElement
 * @see org.xml.sax.Attributes
 */
public void startElement(final String ns, final String localName, final String name, final 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 {
        final int nAtts = atts.getLength();

        if (0 != nAtts) {
            for (int i = 0; i < nAtts; 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.
                final 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:")) {
                    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 (final java.lang.Exception de) {
        if (LOG.isErrorEnabled()) {
            LOG.error(de);
        }
        throw new org.xml.sax.SAXException(de);
    }

}

From source file:org.xchain.framework.jsl.SaxTemplateHandler.java

/**
 * Handles start element events for all JSL elements.
 *///from w  w w .  java  2  s .c  o  m
protected void startJslElement(String uri, String localName, String qName, Attributes attributes)
        throws SAXException {
    // scan for jsl elements that come out of order.
    if (TEMPLATE_LOCAL_NAME.equals(localName)) {
        templateElementDepth++;
    } else if (templateElementDepth == 0) {
        throw new SAXException("The element {" + uri + "}" + localName + "was found outside of a {"
                + JSL_NAMESPACE + "}template element.");
    }

    boolean startSource = elementInfoStack.size() == 1
            || elementInfoStack.get(1).getElementType() == ElementType.COMMAND_ELEMENT_TYPE;
    // if the parent element is null or a command element, then we need to start a new template source.
    if (startSource) {
        sourceBuilder.startSource(elementInfoStack.getFirst().getSourcePrefixMapping(),
                elementInfoStack.getFirst().getSourceExcludeResultPrefixSet(),
                TEMPLATE_LOCAL_NAME.equals(localName));
    }

    if (TEMPLATE_LOCAL_NAME.equals(localName)) {
        startJslTemplateElement(uri, localName, qName, attributes);
    } else if (VALUE_OF_LOCAL_NAME.equals(localName)) {
        startJslValueOfElement(uri, localName, qName, attributes);
    } else if (TEXT_LOCAL_NAME.equals(localName)) {
        startJslTextElement(uri, localName, qName, attributes);
    } else if (COMMENT_LOCAL_NAME.equals(localName)) {
        startJslCommentElement(uri, localName, qName, attributes);
    } else if (ELEMENT_LOCAL_NAME.equals(localName)) {
        startJslDynamicElement(uri, localName, qName, attributes);
    } else if (ATTRIBUTE_LOCAL_NAME.equals(localName)) {
        startJslDynamicAttribute(uri, localName, qName, attributes);
    } else {
        throw new SAXException("Unknown template element '{" + uri + "}" + localName + "'.");
    }

    if (startSource) {
        // send start prefix mapping events to the digester.
        for (Map.Entry<String, String> mapping : elementInfoStack.getFirst().getHandlerPrefixMapping()
                .entrySet()) {
            getContentHandler().startPrefixMapping(mapping.getKey(), mapping.getValue());
        }

        // create a new attributes object with any attributes that are in an xchain namespace.
        AttributesImpl digesterAttributes = new AttributesImpl();
        for (int i = 0; i < attributes.getLength(); i++) {
            if (commandNamespaceSet.contains(attributes.getURI(i))) {
                digesterAttributes.addAttribute(attributes.getURI(i), attributes.getLocalName(i),
                        attributes.getQName(i), attributes.getType(i), attributes.getValue(i));
            }
        }

        // send the start template element to the digester.
        getContentHandler().startElement(JSL_NAMESPACE, TEMPORARY_CHAIN_LOCAL_NAME, temporaryChainQName(),
                digesterAttributes);
    }
}

From source file:org.xchain.framework.jsl.SaxTemplateHandler.java

/**
 * Handles start element events for template elements.
 *///from  w w w  .j  ava  2 s  . c o  m
protected void startTemplateElement(String uri, String localName, String qName, Attributes attributes)
        throws SAXException {
    if (templateElementDepth == 0) {
        throw new SAXException(
                "The element {" + uri + "}" + localName + " was found outside of a template element.");
    }

    // push characters target.
    charactersTargetStack.addFirst(CharacterTarget.SOURCE_BUILDER);

    // push ignorable whitespace target.
    ignorableWhitespaceTargetStack.addFirst(CharacterTarget.SOURCE_BUILDER);

    boolean startSource = elementInfoStack.size() == 1
            || elementInfoStack.get(1).getElementType() == ElementType.COMMAND_ELEMENT_TYPE;
    // if the parent element is null or a command element, then we need to start a new template source.
    if (startSource) {
        sourceBuilder.startSource(elementInfoStack.getFirst().getSourcePrefixMapping(),
                elementInfoStack.getFirst().getSourceExcludeResultPrefixSet(), false);
    }

    try {
        // get the element info for the head of the stack.
        ElementInfo elementInfo = elementInfoStack.getFirst();

        // let the source builder know that we are starting a start element.
        sourceBuilder.startStartElement();

        // send the prefix mappings to the source builder.
        for (Map.Entry<String, String> prefixMapping : elementInfo.getPrefixMapping().entrySet()) {
            sourceBuilder.appendStartPrefixMapping(prefixMapping.getKey(), prefixMapping.getValue());
        }

        // send any exclude result prefix events that need to go out.
        for (String excludeResultPrefix : elementInfo.getExcludeResultPrefixSet()) {
            sourceBuilder.appendStartExcludeResultPrefix(excludeResultPrefix);
        }

        // send the attributes to the source builder.
        for (int i = 0; i < attributes.getLength(); i++) {
            sourceBuilder.appendAttributeValueTemplate(attributes.getURI(i), attributes.getLocalName(i),
                    attributes.getQName(i), attributes.getValue(i));
        }

        // append the start element.
        sourceBuilder.appendStartElement(uri, localName, qName);

        // let the source builder know that we are ending a start element.
        sourceBuilder.endStartElement();
    } catch (SAXException e) {
        throw e;
    } catch (Exception e) {
        throw new SAXException(e);
    }

    // we need to do this just before we stop templating, so this kind of code should be in start command, or end jsl template and end template when there was not
    // a nested command.  We need to delay this, since the passed through template must provide the namespaces state from all of the consumed elements.
    if (startSource) {
        // send start prefix mapping events to the digester.
        for (Map.Entry<String, String> mapping : elementInfoStack.getFirst().getHandlerPrefixMapping()
                .entrySet()) {
            getContentHandler().startPrefixMapping(mapping.getKey(), mapping.getValue());
        }

        // create a new attributes object with any attributes that are in an xchain namespace.
        AttributesImpl digesterAttributes = new AttributesImpl();
        for (int i = 0; i < attributes.getLength(); i++) {
            if (commandNamespaceSet.contains(attributes.getURI(i))) {
                digesterAttributes.addAttribute(attributes.getURI(i), attributes.getLocalName(i),
                        attributes.getQName(i), attributes.getType(i), attributes.getValue(i));
            }
        }

        // send the start template element to the digester.
        getContentHandler().startElement(JSL_NAMESPACE, TEMPORARY_CHAIN_LOCAL_NAME, temporaryChainQName(),
                digesterAttributes);
    }
}