Example usage for org.xml.sax Attributes getIndex

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

Introduction

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

Prototype

public int getIndex(String uri, String localName);

Source Link

Document

Look up the index of an attribute by Namespace name.

Usage

From source file:com.adobe.acs.commons.rewriter.impl.VersionedClientlibsTransformerFactory.java

private Attributes versionClientLibs(final String elementName, final Attributes attrs,
        final SlingHttpServletRequest request) {
    if (SaxElementUtils.isCss(elementName, attrs)) {
        return this.rebuildAttributes(new AttributesImpl(attrs), attrs.getIndex("", ATTR_CSS_PATH),
                attrs.getValue("", ATTR_CSS_PATH), LibraryType.CSS, request);

    } else if (SaxElementUtils.isJavaScript(elementName, attrs)) {
        return this.rebuildAttributes(new AttributesImpl(attrs), attrs.getIndex("", ATTR_JS_PATH),
                attrs.getValue("", ATTR_JS_PATH), LibraryType.JS, request);

    } else {//from   w  w w  .  j a v  a  2  s.c om
        return attrs;
    }
}

From source file:nl.nn.adapterframework.align.XmlTo.java

@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
    boolean xmlArrayContainer = aligner.isParentOfSingleMultipleOccurringChildElement();
    boolean repeatedElement = aligner.isMultipleOccurringChildInParentElement(localName);
    XSTypeDefinition typeDefinition = aligner.getTypeDefinition();
    if (!localName.equals(topElement)) {
        if (topElement != null) {
            if (DEBUG)
                log.debug("endElementGroup [" + topElement + "]");
            documentContainer.endElementGroup(topElement);
        }//w w  w .  j a va 2  s. c o m
        if (DEBUG)
            log.debug("startElementGroup [" + localName + "]");
        documentContainer.startElementGroup(localName, xmlArrayContainer, repeatedElement, typeDefinition);
        topElement = localName;
    }
    element.push(topElement);
    topElement = null;
    if (DEBUG)
        log.debug("startElement [" + localName + "] xml array container ["
                + aligner.isParentOfSingleMultipleOccurringChildElement() + "] repeated element ["
                + aligner.isMultipleOccurringChildInParentElement(localName) + "]");
    documentContainer.startElement(localName, xmlArrayContainer, repeatedElement, typeDefinition);
    super.startElement(uri, localName, qName, atts);
    if (aligner.isNil(atts)) {
        documentContainer.setNull();
    } else {
        if (writeAttributes) {
            XSObjectList attributeUses = aligner.getAttributeUses();
            if (attributeUses == null) {
                if (atts.getLength() > 0) {
                    log.warn("found [" + atts.getLength() + "] attributes, but no declared AttributeUses");
                }
            } else {
                for (int i = 0; i < attributeUses.getLength(); i++) {
                    XSAttributeUse attributeUse = (XSAttributeUse) attributeUses.item(i);
                    XSAttributeDeclaration attributeDeclaration = attributeUse.getAttrDeclaration();
                    XSSimpleTypeDefinition attTypeDefinition = attributeDeclaration.getTypeDefinition();
                    String attName = attributeDeclaration.getName();
                    String attNS = attributeDeclaration.getNamespace();
                    if (DEBUG)
                        log.debug("startElement [" + localName + "] searching attribute [" + attNS + ":"
                                + attName + "]");
                    int attIndex = attNS != null ? atts.getIndex(attNS, attName) : atts.getIndex(attName);
                    if (attIndex >= 0) {
                        String value = atts.getValue(attIndex);
                        if (DEBUG)
                            log.debug("startElement [" + localName + "] attribute [" + attNS + ":" + attName
                                    + "] value [" + value + "]");
                        if (StringUtils.isNotEmpty(value)) {
                            documentContainer.setAttribute(attName, value, attTypeDefinition);
                        }
                    }
                }
            }
        }
    }
}

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

/**
 * Obtains the type attribute that should be serialized and returns the new list of Attributes
 * @param attributes of the qname//from   w  ww .j a v  a 2s  .c  o m
 * @param type is the qname of the type
 * @return new list of Attributes
 */
public Attributes setTypeAttribute(Attributes attributes, QName type) {
    SchemaVersion schema = SchemaVersion.SCHEMA_2001;
    if (msgContext != null) {
        schema = msgContext.getSchemaVersion();
    }

    if (type == null || type.getLocalPart().indexOf(SymbolTable.ANON_TOKEN) >= 0
            || ((attributes != null) && (attributes.getIndex(schema.getXsiURI(), "type") != -1)))
        return attributes;

    AttributesImpl attrs = new AttributesImpl();
    if (attributes != null && 0 < attributes.getLength())
        attrs.setAttributes(attributes);

    String prefix = getPrefixForURI(schema.getXsiURI(), "xsi");

    attrs.addAttribute(schema.getXsiURI(), "type", prefix + ":type", "CDATA", attributeQName2String(type));
    return attrs;
}

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

/**
 * Obtains the type attribute that should be serialized and returns the new list of Attributes
 * @param attributes of the qname//  w w  w.ja  v  a2 s  .  c o  m
 * @param type is the qname of the type
 * @return new list of Attributes
 */
public Attributes setTypeAttribute(Attributes attributes, QName type)
{
    if (type == null ||
         type.getLocalPart().indexOf(SymbolTable.ANON_TOKEN) >= 0 ||
        ((attributes != null) &&
         (attributes.getIndex(Constants.URI_DEFAULT_SCHEMA_XSI,
                            "type") != -1)))
        return attributes;

    AttributesImpl attrs = new AttributesImpl();
    if (attributes != null && 0 < attributes.getLength() )
        attrs.setAttributes(attributes);

    String prefix = getPrefixForURI(Constants.URI_DEFAULT_SCHEMA_XSI,
                                       "xsi");

    attrs.addAttribute(Constants.URI_DEFAULT_SCHEMA_XSI,
                       "type",
                       prefix + ":type",
                       "CDATA", attributeQName2String(type));
    return attrs;
}

From source file:org.apache.cocoon.transformation.LuceneIndexTransformer.java

public void endElement(String namespaceURI, String localName, String qName) throws SAXException {

    if (processing == STATE_QUERY) {
        if (LUCENE_URI.equals(namespaceURI) && LUCENE_QUERY_ELEMENT.equals(localName)) {
            // End query processing
            try {
                if (this.writer == null) {
                    openWriter();//from   w w  w .j a v a 2 s.  c  o m
                }
                this.writer.optimize();
                this.writer.close();
                this.writer = null;
            } catch (IOException e) {
                throw new SAXException(e);
            }
            // propagate the query element to the next stage in the pipeline
            super.endElement(namespaceURI, localName, qName);
            this.processing = STATE_GROUND;
        } else {
            throw new SAXException("</lucene:index> was expected!");
        }
    } else if (processing == STATE_DOCUMENT) {
        if (LUCENE_URI.equals(namespaceURI) && LUCENE_DOCUMENT_ELEMENT.equals(localName)) {
            // End document processing
            this.bodyDocument.add(Field.UnStored(LuceneXMLIndexer.BODY_FIELD, this.bodyText.toString()));
            this.bodyText = null;

            this.bodyDocument.add(Field.UnIndexed(LuceneXMLIndexer.URL_FIELD, this.bodyDocumentURL));
            // store: false, index: true, tokenize: false
            this.bodyDocument
                    .add(new Field(LuceneXMLIndexer.UID_FIELD, uid(this.bodyDocumentURL), false, true, false));
            try {
                reindexDocument();
            } catch (IOException e) {
                throw new SAXException(e);
            }
            this.bodyDocumentURL = null;

            // propagate the lucene:document element to the next stage in the pipeline
            long elapsedTime = System.currentTimeMillis() - this.documentStartTime;
            //documentAttributes = new AttributesImpl();
            this.documentAttributes.addAttribute("", LUCENE_ELAPSED_TIME_ATTRIBUTE,
                    LUCENE_ELAPSED_TIME_ATTRIBUTE, CDATA, String.valueOf(elapsedTime));
            super.startElement(namespaceURI, localName, qName, this.documentAttributes);
            super.endElement(namespaceURI, localName, qName);
            this.processing = STATE_QUERY;
        } else {
            // End element processing
            IndexHelperField tos = (IndexHelperField) elementStack.pop();
            StringBuffer text = tos.getText();

            Attributes atts = tos.getAttributes();
            boolean attributesToText = atts.getIndex(LUCENE_URI, LUCENE_ELEMENT_ATTR_TO_TEXT_ATTRIBUTE) != -1;
            for (int i = 0; i < atts.getLength(); i++) {
                // Ignore Lucene attributes
                if (LUCENE_URI.equals(atts.getURI(i)))
                    continue;

                String atts_lname = atts.getLocalName(i);
                String atts_value = atts.getValue(i);
                bodyDocument.add(Field.UnStored(localName + "@" + atts_lname, atts_value));
                if (attributesToText) {
                    text.append(atts_value);
                    text.append(' ');
                    bodyText.append(atts_value);
                    bodyText.append(' ');
                }
            }

            boolean store = atts.getIndex(LUCENE_URI, LUCENE_ELEMENT_ATTR_STORE_VALUE) != -1;
            if (text != null && text.length() > 0) {
                if (store) {
                    bodyDocument.add(Field.Text(localName, text.toString()));
                } else {
                    bodyDocument.add(Field.UnStored(localName, text.toString()));
                }
            }
        }
    } else {
        // All other tags
        super.endElement(namespaceURI, localName, qName);
    }
}

From source file:org.xchain.framework.digester.AnnotationRuleSet.java

public static String getAttribute(Attributes attributes, String namespace, String localName,
        String defaultValue) {//from   w w  w. ja v a  2s .  c  om
    String value = defaultValue;

    int index = attributes.getIndex(namespace, localName);
    if (index != -1) {
        value = attributes.getValue(index);
    }

    return value;
}

From source file:org.xchain.framework.digester.RuleSetRegistryConfigurator.java

public static String getValue(Attributes attributes, String namespace, String name) {
    String value = null;/*from w  w  w  .jav  a2 s.co  m*/

    int index = attributes.getIndex(namespace, name);

    if (index >= 0) {
        value = attributes.getValue(index);
    }

    return value;
}

From source file:org.xchain.framework.util.AttributesUtil.java

/**
 * Get the attribute value./* w  w  w  .  ja  v  a  2 s .  c om*/
 * 
 * @param attributes The list of attributes to check.
 * @param namespaceUri The namespace of the attribute.
 * @param localName The local name of the attribute.
 * @param defaultValue The default value to use if the attribute could not be found.
 * 
 * @return The value of the attribute if found.  The given defaultValue if not found.
 */
public static String getAttribute(Attributes attributes, String namespaceUri, String localName,
        String defaultValue) {
    String value = defaultValue;

    // get the index of the target attribute.
    int index = attributes.getIndex(namespaceUri, localName);

    // if the index is 0 or greater, then return the value.
    if (index >= 0) {
        value = attributes.getValue(index);
    }

    return value;
}

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 ww  w  .  j  ava2  s . c om
    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.xchain.namespaces.sax.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   w w  w .  j av  a2s .  co  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);
            assertEquals(
                    "The attribute {" + event.getAttributes().getURI(i) + "}"
                            + event.getAttributes().getLocalName(i) + " has the wrong value.",
                    attributes.getValue(attributesIndex), event.getAttributes().getValue(i));
        }
    }
}