Example usage for org.xml.sax.helpers AttributesImpl getLocalName

List of usage examples for org.xml.sax.helpers AttributesImpl getLocalName

Introduction

In this page you can find the example usage for org.xml.sax.helpers AttributesImpl getLocalName.

Prototype

public String getLocalName(int index) 

Source Link

Document

Return an attribute's local name.

Usage

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

protected Attributes rebuildAttributes(final SlingHttpServletRequest slingRequest, final String elementName,
        final Attributes attrs) {
    if (slingRequest == null || !attributes.containsKey(elementName)) {
        // element is not defined as a candidate to rewrite
        return attrs;
    }//  w w w  . j  a  v a2  s .  c  om
    final String[] modifiableAttributes = attributes.get(elementName);

    // clone the attributes
    final AttributesImpl newAttrs = new AttributesImpl(attrs);
    final int len = newAttrs.getLength();

    for (int i = 0; i < len; i++) {
        final String attrName = newAttrs.getLocalName(i);
        if (ArrayUtils.contains(modifiableAttributes, attrName)) {
            final String attrValue = newAttrs.getValue(i);
            if (StringUtils.startsWith(attrValue, "/") && !StringUtils.startsWith(attrValue, "//")) {
                // Only map absolute paths (starting w /), avoid relative-scheme URLs starting w //
                newAttrs.setValue(i, slingRequest.getResourceResolver().map(slingRequest, attrValue));
            }
        }
    }
    return newAttrs;
}

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

@SuppressWarnings("squid:S3776")
private Attributes rebuildAttributes(String elementName, Attributes attrs, String[] modifyableAttributes) {
    // clone the attributes
    final AttributesImpl newAttrs = new AttributesImpl(attrs);

    for (int i = 0; i < newAttrs.getLength(); i++) {
        final String attrName = newAttrs.getLocalName(i);
        if (ArrayUtils.contains(modifyableAttributes, attrName)) {
            final String attrValue = newAttrs.getValue(i);

            String key = elementName + ":" + attrName;
            if (matchingPatterns.containsKey(key)) {
                // Find value based on matching pattern
                Pattern matchingPattern = matchingPatterns.get(key);
                try {
                    newAttrs.setValue(i, handleMatchingPatternAttribute(matchingPattern, attrValue));
                } catch (Exception e) {
                    log.error("Could not perform replacement based on matching pattern", e);
                }/*from ww  w .j ava 2 s  .c o m*/
            } else {
                for (String prefix : prefixes) {
                    if (attrValue.startsWith(prefix)) {
                        newAttrs.setValue(i, prependHostName(attrValue));
                    }
                }
            }
        }
    }

    return newAttrs;
}

From source file:org.apache.axis.message.MessageElement.java

/**
 * remove an element/* ww w . java  2 s.c  o m*/
 * @param attrName name of the element
 * @return true if the attribute was found and removed.
 * @see javax.xml.soap.SOAPElement#removeAttribute(javax.xml.soap.Name)
 */
public boolean removeAttribute(Name attrName) {
    AttributesImpl attributes = makeAttributesEditable();
    boolean removed = false;

    for (int i = 0; i < attributes.getLength() && !removed; i++) {
        if (attributes.getURI(i).equals(attrName.getURI())
                && attributes.getLocalName(i).equals(attrName.getLocalName())) {
            attributes.removeAttribute(i);
            removed = true;
        }
    }
    return removed;
}

From source file:org.apache.axis.message.MessageElement.java

/**
 * remove a named attribute.//ww w.  j a va  2  s  .  c om
 * @see org.w3c.dom.Element#removeAttribute(String)
 * @param attrName name of the attributes
 * @throws DOMException
 */
public void removeAttribute(String attrName) throws DOMException {
    AttributesImpl impl = (AttributesImpl) attributes;
    int index = impl.getIndex(attrName);
    if (index >= 0) {
        AttributesImpl newAttrs = new AttributesImpl();
        // copy except the removed attribute
        for (int i = 0; i < impl.getLength(); i++) { // shift after removal
            if (i != index) {
                String uri = impl.getURI(i);
                String local = impl.getLocalName(i);
                String qname = impl.getQName(i);
                String type = impl.getType(i);
                String value = impl.getValue(i);
                newAttrs.addAttribute(uri, local, qname, type, value);
            }
        }
        // replace it
        attributes = newAttrs;
    }
}

From source file:org.apache.cocoon.template.script.Parser.java

public void startElement(String namespaceURI, String localName, String qname, Attributes attrs)
        throws SAXException {
    Event newEvent = null;//from   w ww  . j  a  va  2  s  .c  om
    AttributesImpl elementAttributes = new AttributesImpl(attrs);
    int attributeCount = elementAttributes.getLength();
    for (int i = 0; i < attributeCount; i++) {
        String attributeURI = elementAttributes.getURI(i);
        if (StringUtils.equals(attributeURI, JXTemplateGenerator.NS)) {
            // TODO: template properties should be allowed only on template
            // root
            getStartEvent().getTemplateProperties().put(elementAttributes.getLocalName(i), this.parsingContext
                    .getStringTemplateParser().compileExpr(elementAttributes.getValue(i), null, locator));
            elementAttributes.removeAttribute(i--);
        }
    }
    StartElement startElement = new StartElement(this.parsingContext, locator, namespaceURI, localName, qname,
            elementAttributes);
    InstructionFactory instructionFactory = this.parsingContext.getInstructionFactory();
    if (instructionFactory.isInstruction(startElement))
        newEvent = instructionFactory.createInstruction(this.parsingContext, startElement, attrs, stack);
    else
        newEvent = startElement;
    stack.push(newEvent);
    addEvent(newEvent);
}

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

protected void startAttributeElement(Attributes attributes) {
    switch (current_state) {
    case STATE_INSIDE_EXECUTE_QUERY:
    case STATE_INSIDE_EXECUTE_INCREMENT:
        current_state = LDAPTransformer.STATE_INSIDE_ATTRIBUTE_ELEMENT;
        current_value.setLength(0);/*from ww w .j  a  v  a2s  .  c  o m*/
        break;
    case STATE_INSIDE_EXECUTE_REPLACE:
        boolean is_name_present = false;
        String mode = MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE_DEFAULT;
        if (attributes != null && attributes.getLength() > 0) {
            AttributesImpl new_attributes = new AttributesImpl(attributes);
            for (int i = 0; i < new_attributes.getLength(); i++) {
                String attr_name = new_attributes.getLocalName(i);
                if (attr_name.equals(MAGIC_ATTRIBUTE_ELEMENT_ATTRIBUTE)) {
                    String value = new_attributes.getValue(i);
                    getCurrentQuery().addAttrList(value);
                    is_name_present = true;
                } else if (attr_name.equals(MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE)) {
                    if (new_attributes.getValue(i).equals(MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE_VALUE_A))
                        mode = MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE_VALUE_A;
                } else {
                    this.getLogger().debug("Invalid attribute match: " + attr_name);
                    throwIllegalStateException("Invalid attribute match in start attribute element");
                }
            }
        }
        if (!is_name_present) {
            this.getLogger().debug("Do not match 'value' attribute");
            throwIllegalStateException("Do not match 'value' attribute in start attribute element");
        }
        getCurrentQuery().addAttrModeVal(mode);
        current_state = LDAPTransformer.STATE_INSIDE_ATTRIBUTE_ELEMENT;
        current_value.setLength(0);
        break;
    case STATE_INSIDE_EXECUTE_ADD:
        if (attributes != null && attributes.getLength() > 0) {
            AttributesImpl new_attributes = new AttributesImpl(attributes);
            for (int i = 0; i < new_attributes.getLength(); i++) {
                String attr_name = new_attributes.getLocalName(i);
                if (attr_name.equals(MAGIC_ATTRIBUTE_ELEMENT_ATTRIBUTE)) {
                    String value = new_attributes.getValue(i);
                    getCurrentQuery().addAttrList(value);
                } else {
                    this.getLogger().debug("Invalid attribute match: " + attr_name);
                    throwIllegalStateException("Invalid attribute match in start attribute element");
                }
            }
        } else {
            this.getLogger().debug("Do not match 'value' attribute");
            throwIllegalStateException("Do not match 'value' attribute in start attribute element");
        }
        current_state = LDAPTransformer.STATE_INSIDE_ATTRIBUTE_ELEMENT;
        current_value.setLength(0);
        break;
    default:
        throwIllegalStateException("Not expecting a start attribute element");
    }
}

From source file:org.apache.tika.gui.TikaGUI.java

/**
 * Creates and returns a content handler that turns XHTML input to
 * simplified HTML output that can be correctly parsed and displayed
 * by {@link JEditorPane}.//from   w ww .j a v a2s  . c o m
 * <p>
 * The returned content handler is set to output <code>html</code>
 * to the given writer. The XHTML namespace is removed from the output
 * to prevent the serializer from using the &lt;tag/&gt; empty element
 * syntax that causes extra "&gt;" characters to be displayed.
 * The &lt;head&gt; tags are dropped to prevent the serializer from
 * generating a &lt;META&gt; content type tag that makes
 * {@link JEditorPane} fail thinking that the document character set
 * is inconsistent.
 * <p>
 * Additionally, it will use ImageSavingParser to re-write embedded:(image) 
 * image links to be file:///(temporary file) so that they can be loaded.
 *
 * @param writer output writer
 * @return HTML content handler
 * @throws TransformerConfigurationException if an error occurs
 */
private ContentHandler getHtmlHandler(Writer writer) throws TransformerConfigurationException {
    SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
    TransformerHandler handler = factory.newTransformerHandler();
    handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "html");
    handler.setResult(new StreamResult(writer));
    return new ContentHandlerDecorator(handler) {
        @Override
        public void startElement(String uri, String localName, String name, Attributes atts)
                throws SAXException {
            if (XHTMLContentHandler.XHTML.equals(uri)) {
                uri = null;
            }
            if (!"head".equals(localName)) {
                if ("img".equals(localName)) {
                    AttributesImpl newAttrs;
                    if (atts instanceof AttributesImpl) {
                        newAttrs = (AttributesImpl) atts;
                    } else {
                        newAttrs = new AttributesImpl(atts);
                    }

                    for (int i = 0; i < newAttrs.getLength(); i++) {
                        if ("src".equals(newAttrs.getLocalName(i))) {
                            String src = newAttrs.getValue(i);
                            if (src.startsWith("embedded:")) {
                                String filename = src.substring(src.indexOf(':') + 1);
                                try {
                                    File img = imageParser.requestSave(filename);
                                    String newSrc = img.toURI().toString();
                                    newAttrs.setValue(i, newSrc);
                                } catch (IOException e) {
                                    System.err.println("Error creating temp image file " + filename);
                                    // The html viewer will show a broken image too to alert them
                                }
                            }
                        }
                    }
                    super.startElement(uri, localName, name, newAttrs);
                } else {
                    super.startElement(uri, localName, name, atts);
                }
            }
        }

        @Override
        public void endElement(String uri, String localName, String name) throws SAXException {
            if (XHTMLContentHandler.XHTML.equals(uri)) {
                uri = null;
            }
            if (!"head".equals(localName)) {
                super.endElement(uri, localName, name);
            }
        }

        @Override
        public void startPrefixMapping(String prefix, String uri) {
        }

        @Override
        public void endPrefixMapping(String prefix) {
        }
    };
}

From source file:org.betaconceptframework.astroboa.engine.jcr.io.Serializer.java

public void openEntity(String elementQName, AttributesImpl atts) throws Exception {

    if (rootElementAttributes != null) {
        for (int i = 0; i < atts.getLength(); i++) {
            IOUtils.addAttribute(rootElementAttributes, atts.getLocalName(i), atts.getValue(i));
        }/*from   w w w  .j  a  v  a 2s .  c om*/
        exportContentHandler.startElement(elementQName, rootElementAttributes);
        rootElementAttributes = null;
    } else {
        exportContentHandler.startElement(elementQName, atts);
    }
}