/** Utility method to serialize an exception and its stack trace to simple HTML. */ private final void serializeException(PrintWriter osw, Throwable t) { osw.write("<b>Exception</b>: " + t.toString() + "\n<br/><br/>"); osw.write("<b>Stack trace:</b>"); osw.write( "<pre style=\"margin: 1px solid red; padding: 3px; font-family: sans-serif; font-size: small;\">"); t.printStackTrace(osw); osw.write("</pre>"); }
/** * This creates a new <code>{@link Document}</code> from an existing <code>InputStream</code> by * letting a DOM parser handle parsing using the supplied stream. * * @param in <code>InputStream</code> to parse. * @param validate <code>boolean</code> to indicate if validation should occur. * @return <code>Document</code> - instance ready for use. * @throws IOException when I/O error occurs. * @throws JDOMException when errors occur in parsing. */ public Document getDocument(InputStream in, boolean validate) throws IOException, JDOMException { try { // Load the parser class Class parserClass = Class.forName("org.apache.xerces.parsers.DOMParser"); Object parser = parserClass.newInstance(); // Set validation Method setFeature = parserClass.getMethod("setFeature", new Class[] {java.lang.String.class, boolean.class}); setFeature.invoke( parser, new Object[] {"http://xml.org/sax/features/validation", new Boolean(validate)}); // Set namespaces true setFeature.invoke( parser, new Object[] {"http://xml.org/sax/features/namespaces", new Boolean(true)}); // Set the error handler if (validate) { Method setErrorHandler = parserClass.getMethod("setErrorHandler", new Class[] {ErrorHandler.class}); setErrorHandler.invoke(parser, new Object[] {new BuilderErrorHandler()}); } // Parse the document Method parse = parserClass.getMethod("parse", new Class[] {org.xml.sax.InputSource.class}); parse.invoke(parser, new Object[] {new InputSource(in)}); // Get the Document object Method getDocument = parserClass.getMethod("getDocument", null); Document doc = (Document) getDocument.invoke(parser, null); return doc; } catch (InvocationTargetException e) { Throwable targetException = e.getTargetException(); if (targetException instanceof org.xml.sax.SAXParseException) { SAXParseException parseException = (SAXParseException) targetException; throw new JDOMException( "Error on line " + parseException.getLineNumber() + " of XML document: " + parseException.getMessage(), e); } else if (targetException instanceof IOException) { IOException ioException = (IOException) targetException; throw ioException; } else { throw new JDOMException(targetException.getMessage(), e); } } catch (Exception e) { throw new JDOMException(e.getClass().getName() + ": " + e.getMessage(), e); } }
/** * Constructor. * * @param ex exception * @param level type * @param schema schema url */ ErrorInfo(final SAXException ex, final String level, final IO schema) { this.level = level; String m = ex.getMessage(); Throwable e = ex; if (m.contains("Exception:")) { // may be recursively called if external validator (e.g. Saxon) is used e = Util.rootException(e); if (e instanceof SAXException) m = e.getLocalizedMessage(); } if (ex instanceof SAXParseException) { final SAXParseException se = (SAXParseException) ex; final String id = se.getSystemId(); if (id != null) { final IO io = IO.get(id); if (schema == null || !io.isDir() && !schema.equals(io)) url = id; } line = se.getLineNumber(); column = se.getColumnNumber(); } message = m; }
public static void main(String[] args) throws IOException { String filename = "z.xml"; // NOT LOCALIZABLE, main try { String uri = "file:" + new File(filename).getAbsolutePath(); // NOT LOCALIZABLE, main // // turn it into an in-memory object. // Parser parser = getParser(); parser.setDocumentHandler(new XmlParser()); parser.setErrorHandler(new MyErrorHandler()); parser.parse(uri); } catch (SAXParseException err) { Debug.trace( "** Parsing error" // NOT LOCALIZABLE, main + ", line " + err.getLineNumber() // NOT LOCALIZABLE + ", uri " + err.getSystemId()); // NOT LOCALIZABLE Debug.trace(" " + err.getMessage()); } catch (SAXException e) { Exception x = e; if (e.getException() != null) x = e.getException(); x.printStackTrace(); } catch (Throwable t) { t.printStackTrace(); } byte[] buf = new byte[256]; Debug.trace("Press ENTER to exit."); // NOT LOCALIZABLE System.in.read(buf, 0, 256); System.exit(0); }
/** Unwraps original throwable from the transformer/ SAX stack. */ private Throwable unwrapCause(TransformerException e) { Throwable t; if (e.getException() != null) { t = e.getException(); } else if (e.getCause() != null) { t = e.getCause(); } else { return e; } do { if (t instanceof IOException) { // break early on IOException return t; } else if (t.getCause() != null) { t = t.getCause(); } else if (t instanceof SAXException && ((SAXException) t).getException() != null) { t = ((SAXException) t).getException(); } else { return t; } } while (true); }
public static MiningResult importFile(InputStream input) throws IOException { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); Document doc; // NodeList netNodes; dbf.setValidating(false); dbf.setIgnoringComments(true); dbf.setIgnoringElementContentWhitespace(true); // dbf.setExpandEntityReferences(false); // dbf.setNamespaceAware(false); DocumentBuilder db = dbf.newDocumentBuilder(); db.setEntityResolver( new EntityResolver() { public InputSource resolveEntity(String publicId, String systemId) { if (systemId.indexOf("ARIS-Export") != -1) { return new InputSource("file:" + About.EXTLIBLOCATION() + "ARIS-Export101.dtd"); } else { return null; } } }); InputSource inpStream = new InputSource(input); inpStream.setSystemId("file:" + System.getProperty("user.dir", "")); doc = db.parse(inpStream); // check if root element is a aml tag Message.add("parsing done" + doc, Message.DEBUG); if (!(doc.getDocumentElement().getNodeName().equals("AML"))) { Message.add("aml tag not found", Message.ERROR); throw new Exception("aml tag not found"); } else { Message.add("aml root element found"); } EPCResult result = new EPCResult(null, (EPC) null); HashMap ObjDef_LinkId = new HashMap(); HashMap modelid_net = new HashMap(); HashMap ObjDef_Name = new HashMap(); HashMap function_LinkId = new HashMap(); HashMap ModelId_ModelType = new HashMap(); traverseAMLforObjectNames( ObjDef_Name, doc.getDocumentElement(), ObjDef_LinkId, ModelId_ModelType); Iterator findLinkToEpc = ObjDef_LinkId.keySet().iterator(); while (findLinkToEpc.hasNext()) { String currentObjDef = (String) findLinkToEpc.next(); String Links = (String) ObjDef_LinkId.get(currentObjDef); StringTokenizer linkSet = new StringTokenizer(Links); String realEpcLink = ""; while (linkSet.hasMoreTokens()) { String currentLink = linkSet.nextToken(); if (ModelId_ModelType.get(currentLink).equals("MT_EEPC")) { realEpcLink = currentLink; break; } } if (realEpcLink.equals(" ")) { ObjDef_LinkId.remove(currentObjDef); } else { ObjDef_LinkId.put(currentObjDef, realEpcLink); } } result = traverseAML( result, doc.getDocumentElement(), null, ObjDef_Name, ObjDef_LinkId, modelid_net, function_LinkId); Iterator hierarchicalFunctions = function_LinkId.keySet().iterator(); while (hierarchicalFunctions.hasNext()) { EPCSubstFunction f = (EPCSubstFunction) hierarchicalFunctions.next(); f.setSubstitutedEPC((EPC) modelid_net.get(function_LinkId.get(f))); // Message.add(f.getSubstitutedEPC().getName()); } return result; } catch (Throwable x) { Message.add(x.toString()); throw new IOException(x.getMessage()); } }
public static EPCResult traverseAML( EPCResult partialResult, Node currentNode, Object parent, HashMap ObjDef_Name, HashMap ObjDef_LinkId, HashMap modelid_net, HashMap function_LinkId) throws Exception { if (currentNode.hasChildNodes()) { for (int i = 0; i < currentNode.getChildNodes().getLength(); i++) { Node currentChild = currentNode.getChildNodes().item(i); if (currentChild.getNodeName().equals("Group")) { String id = currentChild.getAttributes().getNamedItem("Group.ID").getNodeValue(); String GroupName = ""; if (currentChild.hasChildNodes()) { NodeList currentChildren = currentChild.getChildNodes(); for (int j = 0; j < currentChildren.getLength(); j++) { Node Child = currentChildren.item(j); if (!(Child.getNodeName().equals("AttrDef"))) { continue; } if (Child.getAttributes() .getNamedItem("AttrDef.Type") .getNodeValue() .equals("AT_NAME")) { if (Child.hasChildNodes()) { for (int l = 0; l < Child.getChildNodes().getLength(); l++) { if (!(Child.getChildNodes().item(l).getNodeName().equals("AttrValue"))) { continue; } else { GroupName = getTextContent(Child.getChildNodes().item(l)); } } break; } } } if (GroupName.equals("")) { GroupName = id; } } ModelHierarchyDirectory dir = new ModelHierarchyDirectory(id, GroupName); partialResult.addInHierarchy(dir, parent, GroupName); partialResult = traverseAML( partialResult, currentChild, dir, ObjDef_Name, ObjDef_LinkId, modelid_net, function_LinkId); } if (currentChild.getNodeName().equals("Model") && currentChild .getAttributes() .getNamedItem("Model.Type") .getNodeValue() .equals("MT_EEPC")) { String ModelName = "gaga"; if (currentChild.hasChildNodes()) { NodeList currentChildren = currentChild.getChildNodes(); for (int j = 0; j < currentChildren.getLength(); j++) { Node Child = currentChildren.item(j); if (!(Child.getNodeName().equals("AttrDef"))) { continue; } if (Child.getAttributes() .getNamedItem("AttrDef.Type") .getNodeValue() .equals("AT_NAME")) { if (Child.hasChildNodes()) { for (int l = 0; l < Child.getChildNodes().getLength(); l++) { if (!(Child.getChildNodes().item(l).getNodeName().equals("AttrValue"))) { continue; } else { ModelName = getTextContent(Child.getChildNodes().item(l)); } } } break; } } } try { ModelName = ModelName.replaceAll("\n", " "); EPC net = read(currentChild, ObjDef_Name, ObjDef_LinkId, function_LinkId, ModelName); partialResult.addInHierarchy(net, parent, ModelName); modelid_net.put( currentChild.getAttributes().getNamedItem("Model.ID").getNodeValue(), net); } catch (Throwable x) { Message.add(x.getClass().getName()); // throw new IOException(x.getMessage()); } } } } return partialResult; }