use of org.xml.sax.helpers.DefaultHandler in project tomee by apache.
the class ReadDescriptors method isEmpty.
private static boolean isEmpty(final InputStream is, final String rootElement) throws IOException, ParserConfigurationException, SAXException {
final LengthInputStream in = new LengthInputStream(is);
final InputSource inputSource = new InputSource(in);
final SAXParser parser;
final Thread thread = Thread.currentThread();
final ClassLoader original = thread.getContextClassLoader();
thread.setContextClassLoader(Saxs.class.getClassLoader());
try {
parser = Saxs.namespaceAwareFactory().newSAXParser();
} finally {
thread.setContextClassLoader(original);
}
try {
parser.parse(inputSource, new DefaultHandler() {
public void startElement(final String uri, final String localName, final String qName, final Attributes att) throws SAXException {
if (!localName.equals(rootElement)) {
throw new SAXException(localName);
}
}
public InputSource resolveEntity(final String publicId, final String systemId) throws IOException, SAXException {
return new InputSource(new ByteArrayInputStream(new byte[0]));
}
});
return true;
} catch (final SAXException e) {
return in.getLength() == 0;
}
}
use of org.xml.sax.helpers.DefaultHandler in project tomee by apache.
the class ReadDescriptors method getId.
private static String getId(final InputStream is) {
final String[] id = { null };
try {
final LengthInputStream in = new LengthInputStream(is);
final InputSource inputSource = new InputSource(in);
final SAXParser parser = Saxs.namespaceAwareFactory().newSAXParser();
parser.parse(inputSource, new DefaultHandler() {
public void startElement(final String uri, final String localName, final String qName, final Attributes att) throws SAXException {
id[0] = att.getValue("id");
}
public InputSource resolveEntity(final String publicId, final String systemId) throws IOException, SAXException {
return new InputSource(new ByteArrayInputStream(new byte[0]));
}
});
} catch (final Exception e) {
// no-op
}
return id[0];
}
use of org.xml.sax.helpers.DefaultHandler in project tomee by apache.
the class ReadDescriptors method readOpenejbJar.
private void readOpenejbJar(final EjbModule ejbModule) throws OpenEJBException {
final Source source = getSource(ejbModule.getAltDDs().get("openejb-jar.xml"));
if (source != null) {
try {
// Attempt to parse it first as a v3 descriptor
final OpenejbJar openejbJar = JaxbOpenejbJar3.unmarshal(OpenejbJar.class, source.get()).postRead();
ejbModule.setOpenejbJar(openejbJar);
} catch (final Exception v3ParsingException) {
// Attempt to parse it second as a v2 descriptor
final OpenejbJar openejbJar = new OpenejbJar();
ejbModule.setOpenejbJar(openejbJar);
try {
final JAXBElement element = (JAXBElement) JaxbOpenejbJar2.unmarshal(OpenejbJarType.class, source.get());
final OpenejbJarType o2 = (OpenejbJarType) element.getValue();
ejbModule.getAltDDs().put("openejb-jar.xml", o2);
final GeronimoEjbJarType g2 = OpenEjb2Conversion.convertToGeronimoOpenejbXml(o2);
ejbModule.getAltDDs().put("geronimo-openejb.xml", g2);
} catch (final Exception v2ParsingException) {
// Now we have to determine which error to throw; the v3 file exception or the fallback v2 file exception.
final Exception[] realIssue = { v3ParsingException };
try {
final SAXParserFactory factory = Saxs.namespaceAwareFactory();
final SAXParser parser = factory.newSAXParser();
parser.parse(source.get(), new DefaultHandler() {
public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException {
if (localName.equals("environment")) {
realIssue[0] = v2ParsingException;
throw new SAXException("Throw exception to stop parsing");
}
if (uri == null) {
return;
}
if (uri.contains("openejb-jar-2.") || uri.contains("geronimo.apache.org/xml/ns")) {
realIssue[0] = v2ParsingException;
throw new SAXException("Throw exception to stop parsing");
}
}
});
} catch (final Exception dontCare) {
// no-op
}
String filePath = "<error: could not be written>";
try {
File tempFile;
try {
tempFile = File.createTempFile("openejb-jar-", ".xml");
} catch (final Throwable e) {
final File tmp = new File("tmp");
if (!tmp.exists() && !tmp.mkdirs()) {
throw new IOException("Failed to create local tmp directory: " + tmp.getAbsolutePath());
}
tempFile = File.createTempFile("openejb-jar-", ".xml", tmp);
}
try {
IO.copy(source.get(), tempFile);
} catch (final IOException e) {
// no-op
}
filePath = tempFile.getAbsolutePath();
} catch (final IOException e) {
// no-op
}
final Exception e = realIssue[0];
if (e instanceof SAXException) {
throw new OpenEJBException("Cannot parse the openejb-jar.xml. Xml content written to: " + filePath, e);
} else if (e instanceof JAXBException) {
throw new OpenEJBException("Cannot unmarshall the openejb-jar.xml. Xml content written to: " + filePath, e);
} else if (e instanceof IOException) {
throw new OpenEJBException("Cannot read the openejb-jar.xml.", e);
} else {
throw new OpenEJBException("Encountered unknown error parsing the openejb-jar.xml.", e);
}
}
}
}
final Source source1 = getSource(ejbModule.getAltDDs().get("geronimo-openejb.xml"));
if (source1 != null) {
try {
GeronimoEjbJarType geronimoEjbJarType = null;
final Object o = JaxbOpenejbJar2.unmarshal(GeronimoEjbJarType.class, source1.get());
if (o instanceof GeronimoEjbJarType) {
geronimoEjbJarType = (GeronimoEjbJarType) o;
} else if (o instanceof JAXBElement) {
final JAXBElement element = (JAXBElement) o;
geronimoEjbJarType = (GeronimoEjbJarType) element.getValue();
}
if (geronimoEjbJarType != null) {
final Object nested = geronimoEjbJarType.getOpenejbJar();
if (nested != null && nested instanceof OpenejbJar) {
final OpenejbJar existingOpenejbJar = ejbModule.getOpenejbJar();
if (existingOpenejbJar == null || existingOpenejbJar.getEjbDeploymentCount() <= 0) {
final OpenejbJar openejbJar = (OpenejbJar) nested;
ejbModule.getAltDDs().put("openejb-jar.xml", openejbJar);
ejbModule.setOpenejbJar(openejbJar);
}
}
ejbModule.getAltDDs().put("geronimo-openejb.xml", geronimoEjbJarType);
}
} catch (final Exception e) {
throw new OpenEJBException("Failed parsing geronimo-openejb.xml", e);
}
}
}
use of org.xml.sax.helpers.DefaultHandler in project tomee by apache.
the class Report method main.
private void main() throws Exception {
// final File file = new File("/Users/dblevins/work/uber/geronimo-tck-public-trunk/jcdi-tck-runner/target/surefire-reports/testng-results.xml");
final File file = new File("/Users/dblevins/work/all/trunk/openejb/tck/cdi-tomee/target/failsafe-reports/testng-results.xml");
// final File file = new File("/Users/dblevins/work/uber/testng-results.xml");
final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
parser.parse(file, new DefaultHandler() {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
final String name = qName;
if ("class".equals(name)) {
classes.add(new TestClass(attributes.getValue("name")));
}
if ("test-method".equals(name)) {
classes.getLast().addStatus(attributes.getValue("status"), attributes.getValue("name"));
}
}
});
Collections.sort(classes);
textReport(file);
passingXml(file);
failingXml(file);
}
use of org.xml.sax.helpers.DefaultHandler in project android_frameworks_base by crdroidandroid.
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");
}
Aggregations