use of org.xml.sax.ContentHandler in project j2objc by google.
the class SerializerSwitcher method switchSerializerIfHTML.
/**
* Switch to HTML serializer if element is HTML
*
*
* @param transformer Non-null transformer instance
* @param ns Namespace URI of the element
* @param localName Local part of name of element
*
* @throws TransformerException
*/
public static void switchSerializerIfHTML(TransformerImpl transformer, String ns, String localName) throws TransformerException {
if (null == transformer)
return;
if (((null == ns) || (ns.length() == 0)) && localName.equalsIgnoreCase("html")) {
// Access at level of hashtable to see if the method has been set.
if (null != transformer.getOutputPropertyNoDefault(OutputKeys.METHOD))
return;
// Getting the output properties this way won't cause a clone of
// the properties.
Properties prevProperties = transformer.getOutputFormat().getProperties();
// We have to make sure we get an output properties with the proper
// defaults for the HTML method. The easiest way to do this is to
// have the OutputProperties class do it.
OutputProperties htmlOutputProperties = new OutputProperties(Method.HTML);
htmlOutputProperties.copyFrom(prevProperties, true);
Properties htmlProperties = htmlOutputProperties.getProperties();
try {
// Serializer oldSerializer = transformer.getSerializer();
Serializer oldSerializer = null;
if (null != oldSerializer) {
Serializer serializer = SerializerFactory.getSerializer(htmlProperties);
Writer writer = oldSerializer.getWriter();
if (null != writer)
serializer.setWriter(writer);
else {
OutputStream os = oldSerializer.getOutputStream();
if (null != os)
serializer.setOutputStream(os);
}
// transformer.setSerializer(serializer);
ContentHandler ch = serializer.asContentHandler();
transformer.setContentHandler(ch);
}
} catch (java.io.IOException e) {
throw new TransformerException(e);
}
}
}
use of org.xml.sax.ContentHandler in project translationstudio8 by heartsome.
the class Xlsx2TmxHelper method parse.
public void parse(InputStream sheetInputStream, ReadOnlySharedStringsTable sharedStringsTable, AbstractWriter tmxWriter) throws ParserConfigurationException, SAXException, IOException {
InputSource sheetSource = new InputSource(sheetInputStream);
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
SAXParser saxParser = saxFactory.newSAXParser();
XMLReader sheetParser = saxParser.getXMLReader();
ContentHandler handler = new XSSFHander(sharedStringsTable);
sheetParser.setContentHandler(handler);
sheetParser.parse(sheetSource);
if (langCodes.isEmpty()) {
throw new SAXException("EMPTY-LANG-CODE");
}
writeEnd();
}
use of org.xml.sax.ContentHandler in project jdk8u_jdk by JetBrains.
the class XMLKit method readFrom.
public static Element readFrom(Reader in, boolean tokenizing, boolean makeFrozen) throws IOException {
Element sink = new Element();
ContentHandler b = makeBuilder(sink.asList(), tokenizing, makeFrozen);
XMLReader parser;
try {
parser = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
} catch (SAXException ee) {
throw new Error(ee);
}
//parser.setFastStandalone(true);
parser.setContentHandler(b);
try {
parser.setProperty("http://xml.org/sax/properties/lexical-handler", (LexicalHandler) b);
} catch (SAXException ee) {
// Ignore. We will miss the comments and whitespace.
}
try {
parser.parse(new InputSource(in));
} catch (SAXParseException ee) {
throw new RuntimeException("line " + ee.getLineNumber() + " col " + ee.getColumnNumber() + ": ", ee);
} catch (SAXException ee) {
throw new RuntimeException(ee);
}
switch(sink.size()) {
case 0:
return null;
case 1:
if (sink.get(0) instanceof Element) {
return (Element) sink.get(0);
}
// fall through
default:
if (makeFrozen) {
sink.shallowFreeze();
}
return sink;
}
}
use of org.xml.sax.ContentHandler in project android_frameworks_base by AOSPA.
the class SafeSaxTest method testPerformance.
@LargeTest
public void testPerformance() throws Exception {
InputStream in = mContext.getResources().openRawResource(R.raw.youtube);
byte[] xmlBytes;
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) != -1) {
out.write(buffer, 0, length);
}
xmlBytes = out.toByteArray();
} finally {
in.close();
}
Log.i("***", "File size: " + (xmlBytes.length / 1024) + "k");
VideoAdapter videoAdapter = new VideoAdapter();
ContentHandler handler = newContentHandler(videoAdapter);
for (int i = 0; i < 2; i++) {
pureSaxTest(new ByteArrayInputStream(xmlBytes));
saxyModelTest(new ByteArrayInputStream(xmlBytes));
saxyModelTest(new ByteArrayInputStream(xmlBytes), handler);
}
}
use of org.xml.sax.ContentHandler in project android_frameworks_base by ResurrectionRemix.
the class SafeSaxTest method testPerformance.
@LargeTest
public void testPerformance() throws Exception {
InputStream in = mContext.getResources().openRawResource(R.raw.youtube);
byte[] xmlBytes;
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) != -1) {
out.write(buffer, 0, length);
}
xmlBytes = out.toByteArray();
} finally {
in.close();
}
Log.i("***", "File size: " + (xmlBytes.length / 1024) + "k");
VideoAdapter videoAdapter = new VideoAdapter();
ContentHandler handler = newContentHandler(videoAdapter);
for (int i = 0; i < 2; i++) {
pureSaxTest(new ByteArrayInputStream(xmlBytes));
saxyModelTest(new ByteArrayInputStream(xmlBytes));
saxyModelTest(new ByteArrayInputStream(xmlBytes), handler);
}
}
Aggregations