Search in sources :

Example 16 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project spring-security-oauth by spring-projects.

the class SparklrServiceImpl method getSparklrPhotoIds.

public List<String> getSparklrPhotoIds() throws SparklrException {
    try {
        InputStream photosXML = new ByteArrayInputStream(sparklrRestTemplate.getForObject(URI.create(sparklrPhotoListURL), byte[].class));
        final List<String> photoIds = new ArrayList<String>();
        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
        parserFactory.setValidating(false);
        parserFactory.setXIncludeAware(false);
        parserFactory.setNamespaceAware(false);
        SAXParser parser = parserFactory.newSAXParser();
        parser.parse(photosXML, new DefaultHandler() {

            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                if ("photo".equals(qName)) {
                    photoIds.add(attributes.getValue("id"));
                }
            }
        });
        return photoIds;
    } catch (IOException e) {
        throw new IllegalStateException(e);
    } catch (SAXException e) {
        throw new IllegalStateException(e);
    } catch (ParserConfigurationException e) {
        throw new IllegalStateException(e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Attributes(org.xml.sax.Attributes) IOException(java.io.IOException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) ByteArrayInputStream(java.io.ByteArrayInputStream) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 17 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project intellij-community by JetBrains.

the class RngSchemaValidator method collectInformation.

@Nullable
@Override
public MyValidationMessageConsumer collectInformation(@NotNull final PsiFile file) {
    final FileType type = file.getFileType();
    if (type != StdFileTypes.XML && type != RncFileType.getInstance()) {
        return null;
    }
    final XmlFile xmlfile = (XmlFile) file;
    final XmlDocument document = xmlfile.getDocument();
    if (document == null) {
        return null;
    }
    if (type == StdFileTypes.XML) {
        final XmlTag rootTag = document.getRootTag();
        if (rootTag == null) {
            return null;
        }
        if (!ApplicationLoader.RNG_NAMESPACE.equals(rootTag.getNamespace())) {
            return null;
        }
    } else {
        if (!ApplicationManager.getApplication().isUnitTestMode() && MyErrorFinder.hasError(xmlfile)) {
            return null;
        }
    }
    final Document doc = PsiDocumentManager.getInstance(file.getProject()).getDocument(file);
    final MyValidationMessageConsumer consumer = new MyValidationMessageConsumer();
    ErrorHandler eh = new DefaultHandler() {

        @Override
        public void warning(SAXParseException e) {
            handleError(e, file, doc, consumer.warning());
        }

        @Override
        public void error(SAXParseException e) {
            handleError(e, file, doc, consumer.error());
        }
    };
    RngParser.parsePattern(file, eh, true);
    return consumer;
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) RncFileType(org.intellij.plugins.relaxNG.compact.RncFileType) FileType(com.intellij.openapi.fileTypes.FileType) SAXParseException(org.xml.sax.SAXParseException) Document(com.intellij.openapi.editor.Document) DefaultHandler(org.xml.sax.helpers.DefaultHandler) Nullable(org.jetbrains.annotations.Nullable)

Example 18 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project smali by JesusFreke.

the class BaksmaliOptions method loadResourceIds.

/**
     * Load the resource ids from a set of public.xml files.
     *
     * @param resourceFiles A map of resource prefixes -> public.xml files
     */
public void loadResourceIds(Map<String, File> resourceFiles) throws SAXException, IOException {
    for (Map.Entry<String, File> entry : resourceFiles.entrySet()) {
        try {
            SAXParser saxp = SAXParserFactory.newInstance().newSAXParser();
            final String prefix = entry.getKey();
            saxp.parse(entry.getValue(), new DefaultHandler() {

                @Override
                public void startElement(String uri, String localName, String qName, Attributes attr) throws SAXException {
                    if (qName.equals("public")) {
                        String resourceType = attr.getValue("type");
                        String resourceName = attr.getValue("name").replace('.', '_');
                        Integer resourceId = Integer.decode(attr.getValue("id"));
                        String qualifiedResourceName = String.format("%s.%s.%s", prefix, resourceType, resourceName);
                        resourceIds.put(resourceId, qualifiedResourceName);
                    }
                }
            });
        } catch (ParserConfigurationException ex) {
            throw new RuntimeException(ex);
        }
    }
}
Also used : Attributes(org.xml.sax.Attributes) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException)

Example 19 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project android_frameworks_base by DirtyUnicorns.

the class ExpatPerformanceTest method runSax.

private void runSax() throws IOException, SAXException {
    long start = System.currentTimeMillis();
    Xml.parse(newInputStream(), Xml.Encoding.UTF_8, new DefaultHandler());
    long elapsed = System.currentTimeMillis() - start;
    Log.i(TAG, "expat SAX: " + elapsed + "ms");
}
Also used : DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Example 20 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project jdk8u_jdk by JetBrains.

the class SupplementaryChars method test.

@Test(dataProvider = "supported")
public void test(String xml) throws Exception {
    ByteArrayInputStream stream = new ByteArrayInputStream(xml.getBytes("UTF-8"));
    getParser().parse(stream, new DefaultHandler());
    stream.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultHandler(org.xml.sax.helpers.DefaultHandler) Test(org.testng.annotations.Test)

Aggregations

DefaultHandler (org.xml.sax.helpers.DefaultHandler)148 InputStream (java.io.InputStream)65 Metadata (org.apache.tika.metadata.Metadata)59 ParseContext (org.apache.tika.parser.ParseContext)52 Test (org.junit.Test)44 Attributes (org.xml.sax.Attributes)41 SAXParser (javax.xml.parsers.SAXParser)40 SAXException (org.xml.sax.SAXException)39 ByteArrayInputStream (java.io.ByteArrayInputStream)32 SAXParserFactory (javax.xml.parsers.SAXParserFactory)29 IOException (java.io.IOException)26 InputSource (org.xml.sax.InputSource)23 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)22 Parser (org.apache.tika.parser.Parser)22 TikaInputStream (org.apache.tika.io.TikaInputStream)20 ContentHandler (org.xml.sax.ContentHandler)20 File (java.io.File)19 AutoDetectParser (org.apache.tika.parser.AutoDetectParser)17 BodyContentHandler (org.apache.tika.sax.BodyContentHandler)16 FileInputStream (java.io.FileInputStream)15