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.regenstrief.util.XMLUtil.java

/**
 * Returns a string of the Attributes/*from w ww  . j  a va  2s  . co  m*/
 * 
 * @param attrs the Attributes
 * @return the string of Attributes
 **/
public final static String attrsToString(final Attributes attrs) {
    if (attrs == null) {
        return "";
    }

    final StringBuilder txt = new StringBuilder();
    for (int i = 0; i < attrs.getLength(); i++) {
        txt.append(' ');
        txt.append(attrs.getQName(i));
        txt.append("=\"");
        txt.append(attrs.getValue(i));
        txt.append('\"');
    }

    return txt.toString();
}

From source file:org.sakaibrary.xserver.XMLCleanup.java

public void startElement(String namespaceURI, String sName, // simple name
        String qName, // qualified name
        Attributes attrs) throws SAXException {
    echoText();/*from   w  w  w  . j  a  v  a  2s  .  c  om*/
    indentLevel++;
    nl();

    String eName = sName; // element name

    if ("".equals(eName)) {
        eName = qName; // not namespaceAware
    }

    emit("<" + eName);

    if (eName.equals("error_code")) {
        // hit an error
        error = true;
        error_codeFlag = true;
    } else if (eName.equals("error_text")) {
        error_textFlag = true;
    }

    if (attrs != null) {
        for (int i = 0; i < attrs.getLength(); i++) {
            String aName = attrs.getLocalName(i); // Attr name 

            if ("".equals(aName)) {
                aName = attrs.getQName(i);
            }

            if (!aName.equals("xmlns") && !aName.equals("xmlns:xsi") && !aName.equals("xsi:schemaLocation")) {
                emit(" ");
                emit(aName);
                emit(" = \"");
                emit(attrs.getValue(i));
                emit("\"");
            }
        }
    }

    emit(">");
}

From source file:org.seasar.mayaa.impl.builder.SpecificationNodeHandler.java

public void startElement(String namespaceURI, String localName, String qName, Attributes attributes) {
    // TODO doctype???
    // workaround NekoHTMLParser?doctype?"\n"????????
    // ???xerces????????????
    if (_afterDocType) {
        _afterDocType = false;// workaround for doctype
        int length = _charactersBuffer.length();
        if (length > 0) {
            int firstCharIndex;
            for (firstCharIndex = 0; firstCharIndex < length; firstCharIndex++) {
                char currentChar = _charactersBuffer.charAt(firstCharIndex);
                if (currentChar != ' ' && currentChar != '\t') {
                    break;
                }/* ww w.j  a  v  a  2s. co m*/
            }
            if (_charactersBuffer.charAt(firstCharIndex) == '\n') {
                _charactersBuffer.insert(firstCharIndex, '\r');
            } else if (_charactersBuffer.charAt(firstCharIndex) != '\r') {
                _charactersBuffer.insert(firstCharIndex, "\r\n");
            }
        } else {
            _charactersBuffer.insert(0, "\r\n");
        }
    } // workaround

    addCharactersNode();

    Namespace elementNS = SpecificationUtil.createNamespace();
    Iterator it = getCurrentInternalNamespacePrefixMap().entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry entry = (Map.Entry) it.next();
        elementNS.addPrefixMapping((String) entry.getKey(), (URI) entry.getValue());
    }
    elementNS.setParentSpace(SpecificationUtil.getFixedNamespace(_namespace));

    PrefixAwareName parsedName = BuilderUtil.parseName(elementNS, qName);
    QName nodeQName = parsedName.getQName();
    URI nodeURI = nodeQName.getNamespaceURI();
    elementNS.setDefaultNamespaceURI(nodeURI);
    elementNS = SpecificationUtil.getFixedNamespace(elementNS);

    SpecificationNode node = addNode(nodeQName);
    it = getCurrentInternalNamespacePrefixMap().entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry entry = (Map.Entry) it.next();
        node.addPrefixMapping((String) entry.getKey(), (URI) entry.getValue());
    }
    node.setDefaultNamespaceURI(nodeURI);

    for (int i = 0; i < attributes.getLength(); i++) {
        String attrName = attributes.getQName(i);
        String attrValue = attributes.getValue(i);
        if (checkAttribute(attrName, attrValue)) {
            PrefixAwareName parsedAttrName = BuilderUtil.parseName(elementNS, attrName);
            QName attrQName = parsedAttrName.getQName();
            node.addAttribute(attrQName, attrName, attrValue);
        }
    }
    NodeTreeWalker parent = _current; // by kato
    _current = node;
    _current.setParentNode(parent);
    saveToCycle(_current);
    pushNamespace(elementNS);
}

From source file:org.wso2.carbon.mediator.datamapper.engine.input.readers.XMLReader.java

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
        throws SAXException {
    try {//from  w w w  .  jav  a 2 s  .  com
        if ("true".equals(attributes.getValue("xsi" + ":nil"))) {
            sendObjectStartEvent(localName);
            schemaElementList.add(new SchemaElement(localName, uri));
            return;
        }
        localName = getNamespacesAndIdentifiersAddedFieldName(uri, localName, attributes);
        schemaElementList.add(new SchemaElement(localName, uri));
        if (!getEventStack().isEmpty()) {
            ReaderEvent stackElement = getEventStack().peek();
            if (ReaderEventType.ARRAY_START.equals(stackElement.getEventType()) && !(getInputSchema()
                    .isChildElement(schemaElementList.subList(0, schemaElementList.size() - 2), localName)
                    || getModifiedFieldName(localName).equals(stackElement.getName()))) {
                sendArrayEndEvent(localName);
                schemaElementList.add(new SchemaElement(localName, uri));
            }
        }
        String elementType = getInputSchema().getElementTypeByName(schemaElementList);
        String schemaTitle = getInputSchema().getName();
        if (localName.equals(schemaTitle)) {
            sendAnonymousObjectStartEvent(schemaTitle);
            for (int attributeCount = 0; attributeCount < attributes.getLength(); attributeCount++) {
                if (!attributes.getQName(attributeCount).contains(XMLNS)) {
                    schemaElementList.add(new SchemaElement(attributes.getQName(attributeCount),
                            attributes.getURI(attributeCount)));
                    String attributeType = getInputSchema().getElementTypeByName(schemaElementList);
                    schemaElementList.remove(schemaElementList.size() - 1);
                    String attributeFieldName = getAttributeFieldName(attributes.getQName(attributeCount),
                            attributes.getURI(attributeCount));
                    sendFieldEvent(attributeFieldName, attributes.getValue(attributeCount), attributeType);
                }
            }
        } else if (ARRAY_ELEMENT_TYPE.equals(elementType)) {
            //first element of a array should fire array start element
            if (!getEventStack().isEmpty()) {
                ReaderEvent stackElement = getEventStack().peek();
                if (!(ReaderEventType.ARRAY_START.equals(stackElement.getEventType())
                        && getModifiedFieldName(localName).equals(stackElement.getName()))) {
                    sendArrayStartEvent(localName);
                }
            } else {
                sendArrayStartEvent(localName);
            }
            sendAnonymousObjectStartEvent(localName);
            for (int attributeCount = 0; attributeCount < attributes.getLength(); attributeCount++) {
                if (!attributes.getQName(attributeCount).contains(XMLNS)) {
                    schemaElementList.add(new SchemaElement(attributes.getQName(attributeCount),
                            attributes.getURI(attributeCount)));
                    String attributeType = getInputSchema().getElementTypeByName(schemaElementList);
                    schemaElementList.remove(schemaElementList.size() - 1);
                    String attributeFieldName = getAttributeFieldName(attributes.getQName(attributeCount),
                            attributes.getURI(attributeCount));
                    sendFieldEvent(attributeFieldName, attributes.getValue(attributeCount), attributeType);
                }
            }
        } else if (OBJECT_ELEMENT_TYPE.equals(elementType)) {
            sendObjectStartEvent(localName);
            for (int attributeCount = 0; attributeCount < attributes.getLength(); attributeCount++) {
                if (!attributes.getQName(attributeCount).contains(XMLNS)) {
                    schemaElementList.add(new SchemaElement(attributes.getQName(attributeCount),
                            attributes.getURI(attributeCount)));
                    String attributeType = getInputSchema().getElementTypeByName(schemaElementList);
                    schemaElementList.remove(schemaElementList.size() - 1);
                    String attributeFieldName = getAttributeFieldName(attributes.getQName(attributeCount),
                            attributes.getURI(attributeCount));
                    sendFieldEvent(attributeFieldName, attributes.getValue(attributeCount), attributeType);
                }
            }
        } else if ((STRING_ELEMENT_TYPE.equals(elementType) || BOOLEAN_ELEMENT_TYPE.equals(elementType)
                || NUMBER_ELEMENT_TYPE.equals(elementType) || INTEGER_ELEMENT_TYPE.equals(elementType))
                && attributes.getLength() > 0) {
            sendObjectStartEvent(localName + SCHEMA_ATTRIBUTE_PARENT_ELEMENT_POSTFIX);
            for (int attributeCount = 0; attributeCount < attributes.getLength(); attributeCount++) {
                if (!attributes.getQName(attributeCount).contains(XMLNS)) {
                    schemaElementList.add(new SchemaElement(attributes.getQName(attributeCount),
                            attributes.getURI(attributeCount)));
                    String attributeType = getInputSchema().getElementTypeByName(schemaElementList);
                    schemaElementList.remove(schemaElementList.size() - 1);
                    String attributeFieldName = getAttributeFieldName(attributes.getQName(attributeCount),
                            attributes.getURI(attributeCount));
                    sendFieldEvent(attributeFieldName, attributes.getValue(attributeCount), attributeType);
                }
            }
            sendObjectEndEvent(localName + SCHEMA_ATTRIBUTE_PARENT_ELEMENT_POSTFIX);
        }
    } catch (IOException | JSException | InvalidPayloadException | SchemaException | ReaderException e) {
        throw new SAXException("Error occurred while processing start element event", e);
    }
}

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

/**
 * Handles start element events for all JSL elements.
 *//*w ww .ja  v  a  2 s  . c  om*/
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 .  ja  v a2s . c  om
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.xchain.namespaces.jsl.BaseTestSaxEvents.java

public void assertStartElement(Iterator<SaxEventRecorder.SaxEvent> eventIterator, String uri, String localName,
        Attributes attributes) throws Exception {
    assertTrue("There is not a start element event.", eventIterator.hasNext());
    SaxEventRecorder.SaxEvent event = eventIterator.next();
    assertEquals("There was not a start element event.", SaxEventRecorder.EventType.START_ELEMENT,
            event.getType());//from  www.  j a  v  a 2 s .  c  o m
    assertEquals("The element local name is not correct.", localName, event.getLocalName());
    assertEquals("The element uri is not correct.", uri, event.getUri());

    // only check the attributes if they are not null.
    if (attributes != null) {
        assertEquals("The element has the wrong number of attributes.", attributes.getLength(),
                event.getAttributes().getLength());
        for (int i = 0; i < event.getAttributes().getLength(); i++) {
            int attributesIndex = attributes.getIndex(event.getAttributes().getURI(i),
                    event.getAttributes().getLocalName(i));
            assertTrue("The attribute does not contain the attribute {" + event.getAttributes().getURI(i) + "}"
                    + event.getAttributes().getLocalName(i) + ".", -1 != attributesIndex);
            //if( attributes.getQName(attributesIndex) != null ) {
            assertEquals("The qName for the attributes do not match.", attributes.getQName(attributesIndex),
                    event.getAttributes().getQName(i));
            //}
            assertEquals(
                    "The attribute {" + event.getAttributes().getURI(i) + "}"
                            + event.getAttributes().getLocalName(i) + " has the wrong value.",
                    attributes.getValue(attributesIndex), event.getAttributes().getValue(i));
        }
    }
}

From source file:org.xulux.guidriver.XuluxGuiDriver.java

/**
 * @param atts the attributes to get a map from
 * @return A map of the current attributes
 *//*from www.  j av a2 s .c  om*/
private HashMap getAttributeMap(Attributes atts) {
    if (atts == null || atts.getLength() == 0) {
        return null;
    }
    HashMap map = new HashMap();
    for (int i = 0; i < atts.getLength(); i++) {
        map.put(currentqName + "." + atts.getQName(i), atts.getValue(i));
    }
    return map;
}

From source file:org.xwiki.filter.xml.internal.parser.DefaultXMLParser.java

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
        throws SAXException {
    Block currentBlock = this.blockStack.isEmpty() ? null : this.blockStack.peek();

    if (onBlockElement(qName)) {
        if (currentBlock != null) {
            // send previous event
            if (!currentBlock.beginSent) {
                currentBlock.fireBeginEvent(this.filter);
            }/*from  www. ja v  a  2s.c  om*/
        }

        // push new event
        Block block = getBlock(qName, attributes);

        this.blockStack.push(block);

        if (!block.isContainer() && block.filterElement != null
                && block.filterElement.getParameters().length > 0) {
            this.content = new StringBuilder();
        }

        // Extract simple parameters from attributes
        for (int i = 0; i < attributes.getLength(); ++i) {
            String attributeName = attributes.getQName(i);

            setParameter(block, attributeName, attributes.getValue(i), true);
        }
    } else {
        if (onParametersElement(qName)) {
            // starting a new block parameter
            if (currentBlock.filterElement != null) {
                try {
                    currentBlock.parametersDOMBuilder = new Sax2Dom();
                } catch (ParserConfigurationException e) {
                    throw new SAXException("Failed to create new Sax2Dom handler", e);
                }
                currentBlock.parametersDOMBuilder.startDocument();
            }
        }

        if (currentBlock.parametersDOMBuilder != null) {
            currentBlock.parametersDOMBuilder.startElement(uri, localName, qName, attributes);
        }
    }

    ++this.elementDepth;
}

From source file:org.xwiki.rendering.xml.internal.parser.DefaultXMLParser.java

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
        throws SAXException {
    Block currentBlock = this.blockStack.isEmpty() ? null : this.blockStack.peek();

    if (onBlockElement(qName)) {
        if (currentBlock != null) {
            // send previous event
            if (currentBlock.listenerElement != null && !currentBlock.beginSent) {
                currentBlock.fireBeginEvent(this.listener, currentBlock.getParametersTable());
            }/*from   ww w.  j  a  v  a2  s  . c  o m*/
        }

        // push new event
        Block block = getBlock(qName, attributes);

        currentBlock = this.blockStack.push(block);

        if (!block.isContainer() && block.listenerElement.getParameters().size() == 1
                && XMLUtils.isSimpleType(block.listenerElement.getParameters().get(0))) {
            this.content = new StringBuilder();
        }

        // Extract simple parameters from attributes
        for (int i = 0; i < attributes.getLength(); ++i) {
            String attributeName = attributes.getQName(i);

            if (this.configuration.getElementParameterPattern().matcher(attributeName).matches()) {
                int parameterIndex = extractParameterIndex(attributeName);

                Type type = block.listenerElement.getParameters().get(parameterIndex);
                Class<?> typeClass = ReflectionUtils.getTypeClass(type);

                if (XMLUtils.isSimpleType(typeClass)) {
                    block.setParameter(parameterIndex,
                            this.stringConverter.convert(type, attributes.getValue(i)));
                } else {
                    block.setParameter(parameterIndex, XMLUtils.defaultValue(typeClass));
                }
            }
        }
    } else {
        if (onParameterElement(qName)) {
            // starting a new block parameter
            if (currentBlock.listenerElement != null) {
                try {
                    currentBlock.parameterDOMBuilder = new Sax2Dom();
                } catch (ParserConfigurationException e) {
                    throw new SAXException("Failed to create new Sax2Dom handler", e);
                }
                currentBlock.parameterDOMBuilder.startDocument();
            }
        }

        if (currentBlock.parameterDOMBuilder != null) {
            currentBlock.parameterDOMBuilder.startElement(uri, localName, qName, attributes);
        }
    }

    ++this.elementDepth;
}