Search in sources :

Example 26 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project jackrabbit by apache.

the class SerializationTest method createXMLReader.

/**
     * Creates an XMLReader for the given content handler.
     *
     * @param handler the content handler.
     * @return an XMLReader for the given content handler.
     * @throws SAXException if the reader cannot be created.
     */
private XMLReader createXMLReader(ContentHandler handler) throws SAXException {
    XMLReader reader = XMLReaderFactory.createXMLReader();
    reader.setFeature("http://xml.org/sax/features/namespaces", true);
    reader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
    reader.setContentHandler(handler);
    reader.setErrorHandler(new DefaultHandler());
    return reader;
}
Also used : XMLReader(org.xml.sax.XMLReader) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Example 27 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project jackrabbit by apache.

the class ParsingContentHandlerTest method testExternalEntities.

/**
     * Test case for JCR-1355.
     * 
     * @see https://issues.apache.org/jira/browse/JCR-1355
     */
public void testExternalEntities() {
    try {
        String source = "<!DOCTYPE foo SYSTEM \"http://invalid.address/\"><foo/>";
        new ParsingContentHandler(new DefaultHandler()).parse(new ByteArrayInputStream(source.getBytes("UTF-8")));
    } catch (Exception e) {
        fail("JCR-1355: XML import should not access external entities");
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Example 28 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project maven-plugins by apache.

the class AbstractEarPluginIT method assertDeploymentDescriptors.

// Generated application.xml stuff
/**
     * Asserts that the deployment descriptors have been generated successfully.
     * <p/>
     * This test assumes that deployment descriptors are located in the <tt>expected-META-INF</tt> directory of the
     * project. Note that the <tt>MANIFEST.mf</tt> file is ignored and is not tested.
     * 
     * @param baseDir the directory of the tested project
     * @param projectName the name of the project
     */
protected void assertDeploymentDescriptors(final File baseDir, final String projectName) throws IOException {
    final File earDirectory = getEarDirectory(baseDir, projectName);
    final File[] actualDeploymentDescriptors = getDeploymentDescriptors(new File(earDirectory, "META-INF"));
    final File[] expectedDeploymentDescriptors = getDeploymentDescriptors(new File(baseDir, "expected-META-INF"));
    if (expectedDeploymentDescriptors == null) {
        assertNull("No deployment descriptor was expected", actualDeploymentDescriptors);
    } else {
        assertNotNull("Missing deployment descriptor", actualDeploymentDescriptors);
        // Make sure we have the same number of files
        assertEquals("Number of Deployment descriptor(s) mismatch", expectedDeploymentDescriptors.length, actualDeploymentDescriptors.length);
        // Sort the files so that we have the same behavior here
        Arrays.sort(expectedDeploymentDescriptors);
        Arrays.sort(actualDeploymentDescriptors);
        for (int i = 0; i < expectedDeploymentDescriptors.length; i++) {
            File expectedDeploymentDescriptor = expectedDeploymentDescriptors[i];
            File actualDeploymentDescriptor = actualDeploymentDescriptors[i];
            assertEquals("File name mismatch", expectedDeploymentDescriptor.getName(), actualDeploymentDescriptor.getName());
            try {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                dbf.setValidating(true);
                DocumentBuilder docBuilder = dbf.newDocumentBuilder();
                docBuilder.setEntityResolver(new ResourceEntityResolver());
                docBuilder.setErrorHandler(new DefaultHandler());
                final Diff myDiff = new Diff(docBuilder.parse(expectedDeploymentDescriptor), docBuilder.parse(actualDeploymentDescriptor));
                XMLAssert.assertXMLEqual("Wrong deployment descriptor generated for[" + expectedDeploymentDescriptor.getName() + "]", myDiff, true);
            } catch (Exception e) {
                e.printStackTrace();
                fail("Could not assert deployment descriptor " + e.getMessage());
            }
        }
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Diff(org.custommonkey.xmlunit.Diff) ResourceEntityResolver(org.apache.maven.plugins.ear.util.ResourceEntityResolver) File(java.io.File) VerificationException(org.apache.maven.it.VerificationException) IOException(java.io.IOException) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Example 29 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project lucene-solr by apache.

the class TikaEntityProcessor method nextRow.

@Override
public Map<String, Object> nextRow() {
    if (done)
        return null;
    Map<String, Object> row = new HashMap<>();
    DataSource<InputStream> dataSource = context.getDataSource();
    InputStream is = dataSource.getData(context.getResolvedEntityAttribute(URL));
    ContentHandler contentHandler = null;
    Metadata metadata = new Metadata();
    StringWriter sw = new StringWriter();
    try {
        if ("html".equals(format)) {
            contentHandler = getHtmlHandler(sw);
        } else if ("xml".equals(format)) {
            contentHandler = getXmlContentHandler(sw);
        } else if ("text".equals(format)) {
            contentHandler = getTextContentHandler(sw);
        } else if ("none".equals(format)) {
            contentHandler = new DefaultHandler();
        }
    } catch (TransformerConfigurationException e) {
        wrapAndThrow(SEVERE, e, "Unable to create content handler");
    }
    Parser tikaParser = null;
    if (parser.equals(AUTO_PARSER)) {
        tikaParser = new AutoDetectParser(tikaConfig);
    } else {
        tikaParser = context.getSolrCore().getResourceLoader().newInstance(parser, Parser.class);
    }
    try {
        ParseContext context = new ParseContext();
        if ("identity".equals(htmlMapper)) {
            context.set(HtmlMapper.class, IdentityHtmlMapper.INSTANCE);
        }
        if (extractEmbedded) {
            context.set(Parser.class, tikaParser);
        }
        tikaParser.parse(is, contentHandler, metadata, context);
    } catch (Exception e) {
        if (SKIP.equals(onError)) {
            throw new DataImportHandlerException(DataImportHandlerException.SKIP_ROW, "Document skipped :" + e.getMessage());
        }
        wrapAndThrow(SEVERE, e, "Unable to read content");
    }
    IOUtils.closeQuietly(is);
    for (Map<String, String> field : context.getAllEntityFields()) {
        if (!"true".equals(field.get("meta")))
            continue;
        String col = field.get(COLUMN);
        String s = metadata.get(col);
        if (s != null)
            row.put(col, s);
    }
    if (!"none".equals(format))
        row.put("text", sw.toString());
    tryToAddLatLon(metadata, row);
    done = true;
    return row;
}
Also used : TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) HashMap(java.util.HashMap) InputStream(java.io.InputStream) Metadata(org.apache.tika.metadata.Metadata) BodyContentHandler(org.apache.tika.sax.BodyContentHandler) ContentHandler(org.xml.sax.ContentHandler) XHTMLContentHandler(org.apache.tika.sax.XHTMLContentHandler) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SAXException(org.xml.sax.SAXException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) Parser(org.apache.tika.parser.Parser) AutoDetectParser(org.apache.tika.parser.AutoDetectParser) StringWriter(java.io.StringWriter) ParseContext(org.apache.tika.parser.ParseContext) AutoDetectParser(org.apache.tika.parser.AutoDetectParser)

Example 30 with DefaultHandler

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

the class Template method processXml.

private void processXml(@NotNull final RenderingContext context, @NotNull String xml) throws TemplateProcessingException {
    try {
        xml = XmlUtils.stripBom(xml);
        InputSource inputSource = new InputSource(new StringReader(xml));
        SAXParserFactory.newInstance().newSAXParser().parse(inputSource, new DefaultHandler() {

            @Override
            public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
                try {
                    Map<String, Object> paramMap = context.getParamMap();
                    if (TAG_PARAMETER.equals(name)) {
                        String id = attributes.getValue(ATTR_ID);
                        if (!paramMap.containsKey(id)) {
                            String value = attributes.getValue(ATTR_DEFAULT);
                            Object mapValue = value;
                            if (value != null && !value.isEmpty()) {
                                String type = attributes.getValue(ATTR_TYPE);
                                if ("boolean".equals(type)) {
                                    mapValue = Boolean.valueOf(value);
                                }
                            }
                            paramMap.put(id, mapValue);
                        }
                    } else if (TAG_GLOBAL.equals(name)) {
                        String id = attributes.getValue(ATTR_ID);
                        if (!paramMap.containsKey(id)) {
                            paramMap.put(id, TypedVariable.parseGlobal(attributes));
                        }
                    } else if (TAG_GLOBALS.equals(name)) {
                        // Handle evaluation of variables
                        File globalsFile = getPath(attributes, ATTR_FILE);
                        if (globalsFile != null) {
                            processFile(context, globalsFile);
                        }
                    // else: <globals> root element
                    } else if (TAG_EXECUTE.equals(name)) {
                        File recipeFile = getPath(attributes, ATTR_FILE);
                        if (recipeFile != null) {
                            executeRecipeFile(context, recipeFile);
                        }
                    } else if (!name.equals("template") && !name.equals("category") && !name.equals("option") && !name.equals(TAG_THUMBS) && !name.equals(TAG_THUMB) && !name.equals(TAG_ICONS) && !name.equals(TAG_DEPENDENCY) && !name.equals(TAG_FORMFACTOR)) {
                        LOG.error("WARNING: Unknown template directive " + name);
                    }
                } catch (TemplateProcessingException e) {
                    throw new SAXException(e);
                }
            }
        });
    } catch (SAXException ex) {
        if (ex.getCause() instanceof TemplateProcessingException) {
            throw (TemplateProcessingException) ex.getCause();
        }
        throw new TemplateProcessingException(ex);
    } catch (ParserConfigurationException ex) {
        throw new TemplateProcessingException(ex);
    } catch (IOException ex) {
        throw new TemplateProcessingException(ex);
    }
}
Also used : InputSource(org.xml.sax.InputSource) Attributes(org.xml.sax.Attributes) IOException(java.io.IOException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) StringReader(java.io.StringReader) TemplateProcessingException(com.android.tools.idea.templates.FreemarkerUtils.TemplateProcessingException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Map(java.util.Map) File(java.io.File)

Aggregations

DefaultHandler (org.xml.sax.helpers.DefaultHandler)139 InputStream (java.io.InputStream)61 Metadata (org.apache.tika.metadata.Metadata)59 ParseContext (org.apache.tika.parser.ParseContext)52 Test (org.junit.Test)43 Attributes (org.xml.sax.Attributes)39 SAXParser (javax.xml.parsers.SAXParser)35 SAXException (org.xml.sax.SAXException)34 ByteArrayInputStream (java.io.ByteArrayInputStream)30 SAXParserFactory (javax.xml.parsers.SAXParserFactory)25 IOException (java.io.IOException)24 Parser (org.apache.tika.parser.Parser)22 InputSource (org.xml.sax.InputSource)21 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)20 TikaInputStream (org.apache.tika.io.TikaInputStream)20 ContentHandler (org.xml.sax.ContentHandler)20 File (java.io.File)17 AutoDetectParser (org.apache.tika.parser.AutoDetectParser)17 BodyContentHandler (org.apache.tika.sax.BodyContentHandler)16 FileInputStream (java.io.FileInputStream)14