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

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

Introduction

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

Prototype

public String getValue(String qName) 

Source Link

Document

Look up an attribute's value by qualified (prefixed) 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;
    }//from w ww.j av a 2  s .  c o  m
    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   w  ww .j  a  v a2  s .  co m*/
            } else {
                for (String prefix : prefixes) {
                    if (attrValue.startsWith(prefix)) {
                        newAttrs.setValue(i, prependHostName(attrValue));
                    }
                }
            }
        }
    }

    return newAttrs;
}

From source file:com.rapidminer.gui.properties.OperatorPropertyPanel.java

/**
 * Starts a progress thread which parses the parameter descriptions for the provided operator ,
 * cleans the {@link #parameterDescriptionCache}, and stores parsed descriptions in the
 * {@link #parameterDescriptionCache}.//from   ww  w . j av  a  2s  .  c om
 */
private void parseParameterDescriptions(final Operator operator) {
    parameterDescriptionCache.clear();
    URL documentationURL = OperatorDocumentationBrowser.getDocResourcePath(operator);
    if (documentationURL != null) {
        try (InputStream documentationStream = documentationURL.openStream()) {
            XMLStreamReader reader = XML_STREAM_FACTORY.createXMLStreamReader(documentationStream);
            String parameterKey = null;

            // The builder that stores the parameter description text
            StringBuilder parameterTextBuilder = null;
            boolean inParameters = false;
            while (reader.hasNext()) {
                switch (reader.next()) {
                case XMLStreamReader.START_ELEMENT:
                    if (!inParameters && reader.getLocalName().equals(TAG_PARAMETERS)) {
                        inParameters = true;
                    } else {
                        AttributesImpl attributes = new AttributesImpl();
                        for (int i = 0; i < reader.getAttributeCount(); i++) {
                            attributes.addAttribute("", reader.getAttributeLocalName(i),
                                    reader.getAttributeName(i).toString(), reader.getAttributeType(i),
                                    reader.getAttributeValue(i));
                        }

                        // Check if no parameter was found
                        if (reader.getLocalName().equals(TAG_PARAMETER)) {
                            parameterKey = attributes.getValue(ATTRIBUTE_PARAMETER_KEY);

                            // In case a parameter key was found, create a new string
                            // builder
                            if (parameterKey != null) {
                                parameterTextBuilder = new StringBuilder();
                            }
                        }

                        if (parameterTextBuilder != null) {
                            appendParameterStartTag(reader.getLocalName(), attributes, parameterTextBuilder);
                        }
                    }
                    break;
                case XMLStreamReader.END_ELEMENT:
                    // end parsing when end of parameters element is reached
                    if (reader.getLocalName().equals(TAG_PARAMETERS)) {
                        return;
                    }

                    if (parameterTextBuilder != null) {

                        // otherwise add element to description text
                        parameterTextBuilder.append("</");
                        parameterTextBuilder.append(reader.getLocalName());
                        parameterTextBuilder.append(">");

                        // Store description when parameter element ends
                        if (reader.getLocalName().equals(TAG_PARAMETER)) {
                            final String parameterDescription = parameterTextBuilder.toString();
                            final String key = parameterKey;
                            if (!parameterDescriptionCache.containsKey(parameterKey)) {
                                Source xmlSource = new StreamSource(new StringReader(parameterDescription));
                                try {
                                    String desc = OperatorDocToHtmlConverter.applyXSLTTransformation(xmlSource);
                                    parameterDescriptionCache.put(key, StringEscapeUtils.unescapeHtml(desc));
                                } catch (TransformerException e) {
                                    // ignore
                                }
                            }
                        }
                    }
                    break;
                case XMLStreamReader.CHARACTERS:
                    if (parameterTextBuilder != null) {
                        parameterTextBuilder.append(StringEscapeUtils.escapeHtml(reader.getText()));
                    }
                    break;
                default:
                    // ignore other events
                    break;
                }
            }
        } catch (IOException | XMLStreamException e) {
            // ignore
        }
    }
}

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

/**
 * remove a named attribute./*from  w  w w.  j  a va 2 s  .co m*/
 * @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.axis.message.NodeImpl.java

/**
 * The internal representation of Attributes cannot help being changed
 * It is because Attribute is not immutible Type, so if we keep out value and
 * just return it in another form, the application may chnae it, which we cannot
 * detect without some kind back track method (call back notifying the chnage.)
 * I am not sure which approach is better.
 *//*  w  w  w.jav a 2s . c o m*/
protected NamedNodeMap convertAttrSAXtoDOM(Attributes saxAttr) {
    try {
        org.w3c.dom.Document doc = org.apache.axis.utils.XMLUtils.newDocument();
        AttributesImpl saxAttrs = (AttributesImpl) saxAttr;
        NamedNodeMap domAttributes = new NamedNodeMapImpl();
        for (int i = 0; i < saxAttrs.getLength(); i++) {
            String uri = saxAttrs.getURI(i);
            String qname = saxAttrs.getQName(i);
            String value = saxAttrs.getValue(i);
            if (uri != null && uri.trim().length() > 0) {
                // filterring out the tricky method to differentiate the null namespace
                // -ware case
                if (NULL_URI_NAME.equals(uri)) {
                    uri = null;
                }
                Attr attr = doc.createAttributeNS(uri, qname);
                attr.setValue(value);
                domAttributes.setNamedItemNS(attr);
            } else {
                Attr attr = doc.createAttribute(qname);
                attr.setValue(value);
                domAttributes.setNamedItem(attr);
            }
        }
        return domAttributes;
    } catch (Exception ex) {
        log.error(Messages.getMessage("saxToDomFailed00"), ex);

        return null;
    }
}

From source file:org.apache.catalina.util.CatalinaDigester.java

/**
 * Returns an attributes list which contains all the attributes
 * passed in, with any text of form "${xxx}" in an attribute value
 * replaced by the appropriate value from the system property.
 *//*from   www  .  j  av a2s. co  m*/
private Attributes updateAttributes(Attributes list) {

    if (list.getLength() == 0) {
        return list;
    }

    AttributesImpl newAttrs = new AttributesImpl(list);
    int nAttributes = newAttrs.getLength();
    for (int i = 0; i < nAttributes; ++i) {
        String value = newAttrs.getValue(i);
        try {
            String newValue = IntrospectionUtils.replaceProperties(value, null, source);
            if (value != newValue) {
                newAttrs.setValue(i, newValue);
            }
        } catch (Exception e) {
            // ignore - let the attribute have its original value
        }
    }

    return newAttrs;

}

From source file:org.apache.cocoon.forms.transformation.EffectWidgetReplacingPipe.java

private Attributes translateAttributes(Attributes attributes, String[] names) {
    AttributesImpl newAtts = new AttributesImpl(attributes);
    if (names != null) {
        for (int i = 0; i < names.length; i++) {
            String name = names[i];
            int position = newAtts.getIndex(name);
            String newValue = pipeContext.translateText(newAtts.getValue(position));
            if (position > -1)
                newAtts.setValue(position, newValue);
            else//from   w  w  w  .j  a  va  2 s.c om
                throw new RuntimeException("Attribute \"" + name + "\" not present!");
        }
    }
    return newAtts;
}

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  w  w.j a v  a  2s  .  c o m
    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.JPathTransformer.java

/**
 * Helper method to check for the existance of an attribute named
 * jpath:action. If existing the string 'id' is replaced with the
 * continuation id./*from ww w  .j ava  2 s  .  c  om*/
 *
 * @param a an {@link AttributesImpl} instance
 */
private void checkJPathAction(final AttributesImpl a) {

    // check for jpath:action attribute
    int idx = a.getIndex(JPATH_ACTION);

    if (idx != -1 && JPATH_NAMESPACE_URI.equals(a.getURI(idx))) {
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("found jpath:action, adjusting");
        }

        String value = a.getValue(idx);

        // REVISIT(MC): support for continuation level
        String id = m_kont.getContinuation(0).getId();

        a.removeAttribute(idx);
        a.addAttribute("", "action", "action", "CDATA", m_re.subst(value, id));
    }
}

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 www .jav a 2s.c om
        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");
    }
}