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;
}
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");
}
}
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());
}
}
}
}
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;
}
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);
}
}
Aggregations