Search in sources :

Example 46 with Attributes

use of org.xml.sax.Attributes in project WordPress-Android by wordpress-mobile.

the class AztecEditorFragment method overlayFailedMedia.

private void overlayFailedMedia() {
    for (String localMediaId : mFailedMediaIds) {
        Attributes attributes = content.getMediaAttributes(ImagePredicate.localMediaIdPredicate(localMediaId));
        overlayFailedMedia(localMediaId, attributes);
    }
}
Also used : Attributes(org.xml.sax.Attributes)

Example 47 with Attributes

use of org.xml.sax.Attributes in project groovy by apache.

the class AntBuilderLocator method createNode.

protected Object createNode(final Object name, final Map attributes) {
    final Attributes attrs = buildAttributes(attributes);
    String tagName = name.toString();
    String ns = "";
    if (name instanceof QName) {
        QName q = (QName) name;
        tagName = q.getLocalPart();
        ns = q.getNamespaceURI();
    }
    // import can be used only as top level element
    if ("import".equals(name)) {
        antXmlContext.setCurrentTarget(implicitTarget);
    } else if ("target".equals(name) && !insideTask) {
        return onStartTarget(attrs, tagName, ns);
    } else if ("defineTarget".equals(name) && !insideTask) {
        return onDefineTarget(attrs, "target", ns);
    }
    try {
        antElementHandler.onStartElement(ns, tagName, tagName, attrs, antXmlContext);
    } catch (final SAXParseException e) {
        log.log(Level.SEVERE, "Caught: " + e, e);
    }
    insideTask = true;
    final RuntimeConfigurable wrapper = antXmlContext.getWrapperStack().lastElement();
    return wrapper.getProxy();
}
Also used : QName(groovy.xml.QName) SAXParseException(org.xml.sax.SAXParseException) Attributes(org.xml.sax.Attributes)

Example 48 with Attributes

use of org.xml.sax.Attributes in project tika by apache.

the class HtmlParserTest method assertRelativeLink.

private void assertRelativeLink(String url, String base, String relative) throws Exception {
    String test = "<html><head><base href=\"" + base + "\"></head>" + "<body><a href=\"" + relative + "\">test</a></body></html>";
    final List<String> links = new ArrayList<String>();
    new HtmlParser().parse(new ByteArrayInputStream(test.getBytes(UTF_8)), new DefaultHandler() {

        @Override
        public void startElement(String u, String l, String name, Attributes atts) {
            if (name.equals("a") && atts.getValue("", "href") != null) {
                links.add(atts.getValue("", "href"));
            }
        }
    }, new Metadata(), new ParseContext());
    assertEquals(1, links.size());
    assertEquals(url, links.get(0));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) Attributes(org.xml.sax.Attributes) Metadata(org.apache.tika.metadata.Metadata) ParseContext(org.apache.tika.parser.ParseContext) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Example 49 with Attributes

use of org.xml.sax.Attributes in project bnd by bndtools.

the class TestSAXFilters method testSelectionFilter.

public void testSelectionFilter() throws Exception {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    ElementSelectionFilter filter = new ElementSelectionFilter() {

        @Override
        protected boolean select(int depth, String uri, String localName, String qName, Attributes attribs) {
            return !"e".equals(qName);
        }
    };
    XMLReader reader = SAXUtil.buildPipeline(new StreamResult(output), filter);
    reader.parse(new InputSource(new ByteArrayInputStream(SAMPLE4.getBytes())));
    assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root c=\"3\"><a><b><c><d/></c></b></a></root>", output.toString());
}
Also used : InputSource(org.xml.sax.InputSource) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) Attributes(org.xml.sax.Attributes) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ElementSelectionFilter(aQute.libg.sax.filters.ElementSelectionFilter) XMLReader(org.xml.sax.XMLReader)

Example 50 with Attributes

use of org.xml.sax.Attributes in project tika by apache.

the class BoilerpipeContentHandler method endDocument.

@Override
public void endDocument() throws SAXException {
    super.endDocument();
    td = toTextDocument();
    try {
        extractor.process(td);
    } catch (BoilerpipeProcessingException e) {
        throw new SAXException(e);
    }
    Attributes emptyAttrs = new AttributesImpl();
    // and only emit character runs that passed the boilerpipe filters.
    if (includeMarkup) {
        BitSet validCharacterRuns = new BitSet();
        for (TextBlock block : td.getTextBlocks()) {
            if (block.isContent()) {
                BitSet bs = block.getContainedTextElements();
                if (bs != null) {
                    validCharacterRuns.or(bs);
                }
            }
        }
        // Now have bits set for all valid character runs. Replay our recorded elements,
        // but only emit character runs flagged as valid.
        int curCharsIndex = headerCharOffset;
        for (RecordedElement element : elements) {
            switch(element.getElementType()) {
                case START:
                    delegate.startElement(element.getUri(), element.getLocalName(), element.getQName(), element.getAttrs());
                case CONTINUE:
                    // we have to follow suit.
                    for (char[] chars : element.getCharacters()) {
                        curCharsIndex++;
                        if (validCharacterRuns.get(curCharsIndex)) {
                            delegate.characters(chars, 0, chars.length);
                            // https://issues.apache.org/jira/browse/TIKA-961
                            if (!Character.isWhitespace(chars[chars.length - 1])) {
                                // Only add whitespace for certain elements
                                if (XHTMLContentHandler.ENDLINE.contains(element.getLocalName())) {
                                    delegate.ignorableWhitespace(NL, 0, NL.length);
                                }
                            }
                        }
                    }
                    break;
                case END:
                    delegate.endElement(element.getUri(), element.getLocalName(), element.getQName());
                    break;
                default:
                    throw new RuntimeException("Unhandled element type: " + element.getElementType());
            }
        }
    } else {
        for (TextBlock block : td.getTextBlocks()) {
            if (block.isContent()) {
                delegate.startElement(XHTMLContentHandler.XHTML, "p", "p", emptyAttrs);
                char[] chars = block.getText().toCharArray();
                delegate.characters(chars, 0, chars.length);
                delegate.endElement(XHTMLContentHandler.XHTML, "p", "p");
                delegate.ignorableWhitespace(NL, 0, NL.length);
            }
        }
    }
    delegate.endElement(XHTMLContentHandler.XHTML, "body", "body");
    delegate.endElement(XHTMLContentHandler.XHTML, "html", "html");
    // We defer ending any prefix mapping until here, which is why we don't pass this
    // through to the delegate in an overridden method.
    delegate.endPrefixMapping("");
    delegate.endDocument();
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) Attributes(org.xml.sax.Attributes) BitSet(java.util.BitSet) TextBlock(de.l3s.boilerpipe.document.TextBlock) BoilerpipeProcessingException(de.l3s.boilerpipe.BoilerpipeProcessingException) SAXException(org.xml.sax.SAXException)

Aggregations

Attributes (org.xml.sax.Attributes)279 DefaultHandler (org.xml.sax.helpers.DefaultHandler)74 SAXException (org.xml.sax.SAXException)66 AttributesImpl (org.xml.sax.helpers.AttributesImpl)50 SAXParser (javax.xml.parsers.SAXParser)48 Test (org.junit.Test)46 POSaveFailedException (org.adempiere.pipo.exception.POSaveFailedException)37 InputSource (org.xml.sax.InputSource)33 SAXParserFactory (javax.xml.parsers.SAXParserFactory)30 IOException (java.io.IOException)29 File (java.io.File)22 ByteArrayInputStream (java.io.ByteArrayInputStream)19 InputStream (java.io.InputStream)17 ArrayList (java.util.ArrayList)17 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)17 Test (org.junit.jupiter.api.Test)17 XMLReader (org.xml.sax.XMLReader)17 ContentHandler (org.xml.sax.ContentHandler)15 StringReader (java.io.StringReader)12 Transformer (org.apache.sling.rewriter.Transformer)10