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:org.xchain.framework.jsl.SaxTemplateHandler.java

/**
 * Handles start element events for template elements.
 *//*from w  ww  .j  av  a2 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);
    }
}

From source file:org.xwiki.xml.Sax2Dom.java

@Override
public void startElement(String namespace, String localName, String qName, Attributes atts) {
    final Element element = this.document.createElementNS(namespace, qName);

    // Add namespace declarations first
    if (this.namespaceDecls != null) {
        final int nDecls = this.namespaceDecls.size();
        for (int i = 0; i < nDecls; i += 2) {
            String prefix = this.namespaceDecls.get(i);
            String uri = this.namespaceDecls.get(i + 1);

            if (StringUtils.isEmpty(prefix)) {
                element.setAttributeNS(XMLNS_URI, XMLNS_PREFIX, uri);
            } else {
                element.setAttributeNS(XMLNS_URI, XMLNS_STRING + prefix, uri);
            }/*from   w  ww.  j av  a 2  s. c  om*/
        }
        this.namespaceDecls.clear();
    }

    // Add attributes to element
    final int nattrs = atts.getLength();
    for (int i = 0; i < nattrs; i++) {
        if (atts.getLocalName(i) == null) {
            element.setAttribute(atts.getQName(i), atts.getValue(i));
        } else {
            element.setAttributeNS(atts.getURI(i), atts.getQName(i), atts.getValue(i));
        }
    }

    // Append this new node onto current stack node
    this.nodes.peek().appendChild(element);

    // Push this node onto stack
    this.nodes.push(element);
}