Search in sources :

Example 91 with DefaultHandler

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

the class MavenUtil method crcWithoutSpaces.

public static int crcWithoutSpaces(@NotNull InputStream in) throws IOException {
    try {
        final CRC32 crc = new CRC32();
        SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        parser.getXMLReader().setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
        parser.parse(in, new DefaultHandler() {

            boolean textContentOccur = false;

            int spacesCrc;

            private void putString(@Nullable String string) {
                if (string == null)
                    return;
                for (int i = 0, end = string.length(); i < end; i++) {
                    crc.update(string.charAt(i));
                }
            }

            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                textContentOccur = false;
                crc.update(1);
                putString(qName);
                for (int i = 0; i < attributes.getLength(); i++) {
                    putString(attributes.getQName(i));
                    putString(attributes.getValue(i));
                }
            }

            @Override
            public void endElement(String uri, String localName, String qName) throws SAXException {
                textContentOccur = false;
                crc.update(2);
                putString(qName);
            }

            private void processTextOrSpaces(char[] ch, int start, int length) {
                for (int i = start, end = start + length; i < end; i++) {
                    char a = ch[i];
                    if (Character.isWhitespace(a)) {
                        if (textContentOccur) {
                            spacesCrc = spacesCrc * 31 + a;
                        }
                    } else {
                        if (textContentOccur && spacesCrc != 0) {
                            crc.update(spacesCrc);
                            crc.update(spacesCrc >> 8);
                        }
                        crc.update(a);
                        textContentOccur = true;
                        spacesCrc = 0;
                    }
                }
            }

            @Override
            public void characters(char[] ch, int start, int length) throws SAXException {
                processTextOrSpaces(ch, start, length);
            }

            @Override
            public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
                processTextOrSpaces(ch, start, length);
            }

            @Override
            public void processingInstruction(String target, String data) throws SAXException {
                putString(target);
                putString(data);
            }

            @Override
            public void skippedEntity(String name) throws SAXException {
                putString(name);
            }

            @Override
            public void error(SAXParseException e) throws SAXException {
                crc.update(100);
            }
        });
        return (int) crc.getValue();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    } catch (SAXException e) {
        return -1;
    }
}
Also used : CRC32(java.util.zip.CRC32) Attributes(org.xml.sax.Attributes) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) SAXParseException(org.xml.sax.SAXParseException) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 92 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project zaproxy by zaproxy.

the class ZapXmlConfiguration method createDocumentBuilder.

@Override
protected DocumentBuilder createDocumentBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory factory = XmlUtils.newXxeDisabledDocumentBuilderFactory();
    // Same behaviour as base method:
    if (isValidating()) {
        factory.setValidating(true);
        if (isSchemaValidation()) {
            factory.setNamespaceAware(true);
            factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
        }
    }
    DocumentBuilder result = factory.newDocumentBuilder();
    result.setEntityResolver(getEntityResolver());
    if (isValidating()) {
        result.setErrorHandler(new DefaultHandler() {

            @Override
            public void error(SAXParseException ex) throws SAXException {
                throw ex;
            }
        });
    }
    return result;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException)

Example 93 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project newsrob by marianokamp.

the class GRAnsweredBadRequestException method discoverFeeds.

public List<DiscoveredFeed> discoverFeeds(final String query) throws ReaderAPIException, IOException, GRTokenExpiredException, ParserConfigurationException, SAXException, GRAnsweredBadRequestException {
    Timing t = new Timing("discoverFeeds()", context);
    final List<DiscoveredFeed> results = new ArrayList<DiscoveredFeed>();
    if (query == null || query.length() == 0)
        return results;
    NewsRobHttpClient httpClient = NewsRobHttpClient.newInstance(false, context);
    try {
        final String queryPath = "/reader/api/0/feed-finder?q=";
        HttpRequestBase req = createGRRequest(httpClient, getGoogleHost() + queryPath + query);
        HttpResponse response = executeGRRequest(httpClient, req, true);
        throwExceptionWhenNotStatusOK(response);
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        SAXParser parser = saxParserFactory.newSAXParser();
        DefaultHandler handler = new SimpleStringExtractorHandler() {

            private DiscoveredFeed discoveredFeed;

            @Override
            public void endElement(String uri, String localName, String name) throws SAXException {
                super.endElement(uri, localName, name);
                if (discoveredFeed != null && "entry".equals(localName)) {
                    // System.out.println("Added discovered feed " +
                    // discoveredFeed);
                    results.add(discoveredFeed);
                    discoveredFeed = null;
                }
            }

            @Override
            public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
                super.startElement(uri, localName, name, attributes);
                // System.out.println("startElement=" + localName);
                if ("entry".equals(localName)) {
                    discoveredFeed = new DiscoveredFeed();
                    // System.out.println("Created new Discovered Feed");
                    return;
                }
                if ("link".equals(localName)) {
                    String rel = attributes.getValue("rel");
                    String href = attributes.getValue("href");
                    if ("self".equals(rel))
                        return;
                    if (discoveredFeed != null) {
                        if (rel != null) {
                            if ("alternate".equals(rel))
                                discoveredFeed.alternateUrl = href;
                            else if ("http://www.google.com/reader/atom/relation/feed".equals(rel))
                                discoveredFeed.feedUrl = href;
                        }
                    } else {
                        DiscoveredFeed df = new DiscoveredFeed();
                        df.title = query;
                        df.feedUrl = href;
                        results.add(df);
                    }
                }
            // System.out.println("startElement2=" + localName);
            }

            @Override
            public void receivedString(String localName, String fqn, String s) {
                if (discoveredFeed == null)
                    return;
                if ("title".equals(localName)) {
                    discoveredFeed.title = s;
                } else if ("content".equals(localName)) {
                    discoveredFeed.snippet = s;
                }
            }
        };
        parser.parse(NewsRobHttpClient.getUngzippedContent(response.getEntity(), context), handler);
        return results;
    } finally {
        httpClient.close();
        t.stop();
    }
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) SimpleStringExtractorHandler(com.newsrob.util.SimpleStringExtractorHandler) ArrayList(java.util.ArrayList) Attributes(org.xml.sax.Attributes) HttpResponse(org.apache.http.HttpResponse) DefaultHandler(org.xml.sax.helpers.DefaultHandler) NewsRobHttpClient(com.newsrob.download.NewsRobHttpClient) SAXParser(javax.xml.parsers.SAXParser) Timing(com.newsrob.util.Timing) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 94 with DefaultHandler

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

the class ModuleXmlParser method parse.

private ModuleScriptData parse(@NotNull InputStream xml) {
    try {
        setCurrentState(initial);
        SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
        saxParser.parse(xml, new DelegatedSaxHandler() {

            @NotNull
            @Override
            protected DefaultHandler getDelegate() {
                return currentState;
            }
        });
        return new ModuleScriptData(modules);
    } catch (ParserConfigurationException e) {
        MessageCollectorUtil.reportException(messageCollector, e);
    } catch (SAXException e) {
        messageCollector.report(ERROR, OutputMessageUtil.renderException(e), NO_LOCATION);
    } catch (IOException e) {
        MessageCollectorUtil.reportException(messageCollector, e);
    }
    return ModuleScriptData.EMPTY;
}
Also used : SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) NotNull(org.jetbrains.annotations.NotNull) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException)

Example 95 with DefaultHandler

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

the class ValidateXmlActionHandler method doParse.

public void doParse() {
    try {
        myParser.parse(new InputSource(new StringReader(myFile.getText())), new DefaultHandler() {

            @Override
            public void warning(SAXParseException e) throws SAXException {
                if (myErrorReporter.isUniqueProblem(e))
                    myErrorReporter.processError(e, ProblemType.WARNING);
            }

            @Override
            public void error(SAXParseException e) throws SAXException {
                if (myErrorReporter.isUniqueProblem(e))
                    myErrorReporter.processError(e, ProblemType.ERROR);
            }

            @Override
            public void fatalError(SAXParseException e) throws SAXException {
                if (myErrorReporter.isUniqueProblem(e))
                    myErrorReporter.processError(e, ProblemType.FATAL);
            }

            @Override
            public InputSource resolveEntity(String publicId, String systemId) {
                final PsiFile psiFile = myXmlResourceResolver.resolve(null, systemId);
                if (psiFile == null)
                    return null;
                return new InputSource(new StringReader(psiFile.getText()));
            }

            @Override
            public void startDocument() throws SAXException {
                super.startDocument();
                myParser.setProperty(ENTITY_RESOLVER_PROPERTY_NAME, myXmlResourceResolver);
                configureEntityManager(myFile, myParser);
            }
        });
        final String[] resourcePaths = myXmlResourceResolver.getResourcePaths();
        if (resourcePaths.length > 0) {
            // if caches are used
            final VirtualFile[] files = new VirtualFile[resourcePaths.length];
            for (int i = 0; i < resourcePaths.length; ++i) {
                files[i] = UriUtil.findRelativeFile(resourcePaths[i], null);
            }
            myFile.putUserData(DEPENDENT_FILES_KEY, files);
            myFile.putUserData(GRAMMAR_POOL_TIME_STAMP_KEY, calculateTimeStamp(files, myProject));
        }
        myFile.putUserData(KNOWN_NAMESPACES_KEY, getNamespaces(myFile));
    } catch (SAXException e) {
        LOG.debug(e);
    } catch (Exception exception) {
        filterAppException(exception);
    } catch (StackOverflowError error) {
    // http://issues.apache.org/jira/browse/XERCESJ-589
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) StringReader(java.io.StringReader) PsiFile(com.intellij.psi.PsiFile)

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