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

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

Introduction

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

Prototype

public void setValue(int index, String value) 

Source Link

Document

Set the value of a specific attribute.

Usage

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

/**
 * Remove extra information from element's attributes. Currently only removes
 * the repeater variable from the element's name attribute if present.
 *
 * @param attr/*from   ww w .  jav  a2 s. c  o  m*/
 * @return modified attributes
 */
private Attributes normalizeAttributes(Attributes attr) {
    Attributes result = attr;
    if (this.stripNumber && this.repeater.size() > 0) {
        String name = attr.getValue("name");
        if (name != null) {
            for (Iterator i = this.repeater.iterator(); i.hasNext();) {
                RepeaterStatus status = (RepeaterStatus) i.next();
                int pos = name.indexOf(status.var);
                if (pos >= 0) {
                    AttributesImpl attributes;
                    if (result instanceof AttributesImpl) {
                        attributes = (AttributesImpl) result;
                    } else {
                        attributes = new AttributesImpl(result);
                    }
                    name = name.substring(0, pos - this.decorationSize)
                            + name.substring(pos + status.var.length() + this.decorationSize);
                    attributes.setValue(attributes.getIndex("name"), name);
                    result = attributes;
                }
            }
        }
    }
    return result;
}

From source file:org.apache.cocoon.woody.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));
            newAtts.setValue(position, newValue);
        }/*from  w w  w. j  a  v  a 2  s .c  o m*/
    }
    return newAtts;
}

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  www.  j a  v a2  s .co  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.xwiki.portlet.view.HTMLIdAttributeXMLFilter.java

/**
 * Rewrites URL fragments in anchor URLs relative to the current page.
 * //www  .  j  av  a 2s .  co m
 * @param atts the lists of element attributes
 * @return the given list of attributes where the value of the {@link #HREF} attribute has been changed
 */
private Attributes rewriteURLFragment(Attributes atts) {
    String href = atts.getValue(HREF);
    if (href != null && href.startsWith("#")) {
        AttributesImpl newAtts = atts instanceof AttributesImpl ? (AttributesImpl) atts
                : new AttributesImpl(atts);
        newAtts.setValue(atts.getIndex(HREF), String.format("#%s", namespace(href.substring(1))));
        return newAtts;
    }
    return atts;
}