List of usage examples for org.xml.sax SAXException printStackTrace
public void printStackTrace()
From source file:Main.java
public static boolean creteEntity(Object entity, String fileName) { boolean flag = false; try {/*w ww. j av a 2 s. c o m*/ DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(fileName); Class clazz = entity.getClass(); Field[] fields = clazz.getDeclaredFields(); Node EntityElement = document.getElementsByTagName(clazz.getSimpleName() + "s").item(0); Element newEntity = document.createElement(clazz.getSimpleName()); EntityElement.appendChild(newEntity); for (Field field : fields) { field.setAccessible(true); Element element = document.createElement(field.getName()); element.appendChild(document.createTextNode(field.get(entity).toString())); newEntity.appendChild(element); } TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource domSource = new DOMSource(document); StreamResult streamResult = new StreamResult(new File(fileName)); transformer.transform(domSource, streamResult); flag = true; } catch (ParserConfigurationException pce) { System.out.println(pce.getLocalizedMessage()); pce.printStackTrace(); } catch (TransformerException te) { System.out.println(te.getLocalizedMessage()); te.printStackTrace(); } catch (IOException ioe) { System.out.println(ioe.getLocalizedMessage()); ioe.printStackTrace(); } catch (SAXException sae) { System.out.println(sae.getLocalizedMessage()); sae.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return flag; }
From source file:Main.java
public static Document parseXmlData(String xmlData, boolean validating) { try {// ww w . j a v a 2 s . c o m // Create a builder factory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(validating); // Create the builder and parse the file Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(xmlData))); return doc; } catch (SAXException e) { // A parsing error occurred; the xml input is not valid System.err.println("SAXException in parsing XML data, will return null doc"); e.printStackTrace(); } catch (ParserConfigurationException e) { System.err.println("ParserConfigurationException in parsing XML data, will return null doc"); e.printStackTrace(); } catch (IOException e) { System.err.println("IOException in parsing XML data, will return null doc"); e.printStackTrace(); } return null; }
From source file:de.unisb.cs.st.javalanche.rhino.coverage.CoberturaParser.java
public static void parseXmlFile(File file, DefaultHandler handler, boolean validating) { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(validating);//w w w. ja va 2 s . c o m try { factory.newSAXParser().parse(file, handler); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } }
From source file:Main.java
public static Document parseXmlFile(String filename, boolean validating) { try {//from w w w . j av a 2 s . c o m // Create a builder factory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(validating); // Create the builder and parse the file Document doc = factory.newDocumentBuilder().parse(new File(filename)); return doc; } catch (SAXException e) { // A parsing error occurred; the xml input is not valid System.err.println("SAXException in parsing XML File:" + filename + ", will return null doc"); e.printStackTrace(); } catch (ParserConfigurationException e) { System.err.println( "ParserConfigurationException in parsing XML File:" + filename + ", will return null doc"); e.printStackTrace(); } catch (IOException e) { System.err.println("IOException in parsing XML File:" + filename + ", will return null doc"); e.printStackTrace(); } return null; }
From source file:com.isa.ws.utiles.UtilesSWHelper.java
public static String getNodeValue(String xml, String node) { //get the factory DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try {/*from w ww. j a va 2s .c o m*/ //Using factory get an instance of document builder DocumentBuilder db = dbf.newDocumentBuilder(); //parse using builder to get DOM representation of the XML file InputStream is = new ByteArrayInputStream(xml.getBytes()); Document dom = db.parse(is); NodeList nodelist = dom.getElementsByTagName(node); Node node1 = nodelist.item(0); String value = null; if (node1.getFirstChild() != null) { if (node.equals("css:validity")) { value = ""; value += node1.getChildNodes().item(0).getFirstChild().getNodeValue(); value += ","; value += node1.getChildNodes().item(1).getFirstChild().getNodeValue(); } else value = node1.getFirstChild().getNodeValue(); } return value; } catch (ParserConfigurationException pce) { pce.printStackTrace(); return null; } catch (SAXException se) { se.printStackTrace(); return null; } catch (IOException ioe) { ioe.printStackTrace(); return null; } }
From source file:edu.ku.brc.util.thumbnails.Thumbnailer.java
/** * //from w ww . j av a 2 s . co m */ private static void readIconMap() { availableIcons = new HashMap<String, String>(); fallbackIcons = new HashMap<String, String>(); File mimeTypeFile = XMLHelper.getConfigDir("mime_icons.xml"); if (mimeTypeFile.exists()) { try { Document registry = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(mimeTypeFile); NodeList mimeNodes = registry.getElementsByTagName("mimetype"); for (int i = 0; i < mimeNodes.getLength(); ++i) { Node mimeNode = mimeNodes.item(i); Node iconNameNode = mimeNode.getAttributes().getNamedItem("icon"); Node fallbackNode = mimeNode.getAttributes().getNamedItem("fallback"); String iconName = iconNameNode.getNodeValue(); String fbStr = fallbackNode != null ? fallbackNode.getNodeValue() : null; boolean isFallBack = StringUtils.isNotEmpty(fbStr) && fbStr.equals("true"); NodeList extNodes = mimeNode.getChildNodes(); for (int j = 0; j < extNodes.getLength(); ++j) { Node extNode = extNodes.item(j); String ext = extNode.getTextContent().trim(); if (StringUtils.isNotEmpty(ext)) { if (isFallBack) { fallbackIcons.put(ext, iconName); } else { availableIcons.put(ext, iconName); } } } } } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } } }
From source file:de.fraunhofer.iosb.ivct.IVCTcommander.java
private static Document parseXmlFile(final String fileName) { //get the factory DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); Document dom;// w ww .j a va 2s . com try { //Using factory get an instance of document builder DocumentBuilder db = dbf.newDocumentBuilder(); //parse using builder to get DOM representation of the XML file dom = db.parse(fileName); return dom; } catch (ParserConfigurationException pce) { pce.printStackTrace(); } catch (SAXException se) { se.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } return null; }
From source file:main.java.refinement_class.Useful.java
public static boolean validation(String schema_file, String xml_file) { Object obj = null;//from ww w.j a v a 2 s . c om // create a JAXBContext capable of handling classes generated into // JAXBContext jc = JAXBContext.newInstance(ObjectFactory.class ); JAXBContext jc; try { jc = JAXBContext.newInstance(); // create an Unmarshaller Unmarshaller u = jc.createUnmarshaller(); SchemaFactory sf = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI); try { javax.xml.validation.Schema schema = sf.newSchema(new File(schema_file)); u.setSchema((javax.xml.validation.Schema) schema); u.setEventHandler(new ValidationEventHandler() { // allow unmarshalling to continue even if there are errors public boolean handleEvent(ValidationEvent ve) { // ignore warnings if (ve.getSeverity() != ValidationEvent.WARNING) { ValidationEventLocator vel = ve.getLocator(); System.out.println("Line:Col[" + vel.getLineNumber() + ":" + vel.getColumnNumber() + "]:" + ve.getMessage()); } return true; } }); } catch (org.xml.sax.SAXException se) { System.out.println("Unable to validate due to following error."); se.printStackTrace(); LOG.error(se.toString()); } } catch (JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); LOG.error(e.toString()); } return true; }
From source file:main.java.refinement_class.Useful.java
public static Object unmashal2(String schema_file, String xml_file, Class c) { Object obj = null;//from w ww. j a v a2 s . c om try { // create a JAXBContext capable of handling classes generated into // JAXBContext jc = JAXBContext.newInstance(ObjectFactory.class ); JAXBContext jc = JAXBContext.newInstance(c); // create an Unmarshaller Unmarshaller u = jc.createUnmarshaller(); SchemaFactory sf = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI); try { javax.xml.validation.Schema schema = sf.newSchema(new File(schema_file)); u.setSchema((javax.xml.validation.Schema) schema); u.setEventHandler(new ValidationEventHandler() { // allow unmarshalling to continue even if there are errors public boolean handleEvent(ValidationEvent ve) { // ignore warnings if (ve.getSeverity() != ValidationEvent.WARNING) { ValidationEventLocator vel = ve.getLocator(); System.out.println("Line:Col[" + vel.getLineNumber() + ":" + vel.getColumnNumber() + "]:" + ve.getMessage()); } return true; } }); } catch (org.xml.sax.SAXException se) { System.out.println("Unable to validate due to following error."); se.printStackTrace(); LOG.error(se.toString()); } obj = u.unmarshal(new File(xml_file)); // even though document was determined to be invalid unmarshalling, // marshal out result. // System.out.println(""); // System.out.println("Still able to marshal invalid document"); // javax.xml.bind.Marshaller m = jc.createMarshaller(); // m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE ); // m.marshal(poe, System.out); } catch (UnmarshalException ue) { // The JAXB specification does not mandate how the JAXB provider // must behave when attempting to unmarshal invalid XML data. // those cases, the JAXB provider is allowed to terminate the // call to unmarshal with an UnmarshalException. System.out.println("Caught UnmarshalException"); } catch (JAXBException je) { je.printStackTrace(); LOG.error(je.toString()); } return obj; }
From source file:main.java.refinement_class.Useful.java
public static Object unmashal(String schema_file, String xml_file, Class c) { Object obj = null;/*from w ww .j a va 2 s . c o m*/ try { // create a JAXBContext capable of handling classes generated into // JAXBContext jc = JAXBContext.newInstance(ObjectFactory.class ); JAXBContext jc = JAXBContext.newInstance(c); // create an Unmarshaller Unmarshaller u = jc.createUnmarshaller(); SchemaFactory sf = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI); try { // LOG.info("\n\n XX -->> Schema file: " + schema_file); // LOG.info("\n\n XX -->> xml_file file: " + xml_file); //++ javax.xml.validation.Schema schema; if (schema_file.contains("/tmp/")) { schema = sf.newSchema(new File(schema_file)); } else { URL urlSchema = getUrl(H2mserviceImpl.class, schema_file); // LOG.info("\n\n XX -->> urlSchema: " + urlSchema.getPath()); schema = sf.newSchema(urlSchema); } //-- //javax.xml.validation.Schema schema = sf.newSchema(new File(schema_file)); // ++ u.setSchema((javax.xml.validation.Schema) schema); u.setEventHandler(new ValidationEventHandler() { // allow unmarshalling to continue even if there are errors public boolean handleEvent(ValidationEvent ve) { // ignore warnings if (ve.getSeverity() != ValidationEvent.WARNING) { ValidationEventLocator vel = ve.getLocator(); System.out.println("Line:Col[" + vel.getLineNumber() + ":" + vel.getColumnNumber() + "]:" + ve.getMessage()); } return true; } }); } catch (org.xml.sax.SAXException se) { System.out.println("Unable to validate due to following error."); se.printStackTrace(); LOG.error("===>[1]ERROR Unmashaling \n\n" + se.toString()); LOG.error(Useful.getStackTrace(se)); } catch (Exception e) { LOG.error("===>[2]ERROR Unmashaling \n\n" + e.toString()); LOG.error(Useful.getStackTrace(e)); } if (xml_file.contains("/tmp/")) { obj = u.unmarshal(new File(xml_file)); } else { URL url_xml_file = getUrl(H2mserviceImpl.class, xml_file); LOG.info("\n\n XX -->> url_xml_file: " + url_xml_file.getPath()); obj = u.unmarshal(url_xml_file); } //-- //obj = u.unmarshal( new File( xml_file)); //++ // even though document was determined to be invalid unmarshalling, // marshal out result. // System.out.println(""); // System.out.println("Still able to marshal invalid document"); // javax.xml.bind.Marshaller m = jc.createMarshaller(); // m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE ); // m.marshal(poe, System.out); } catch (UnmarshalException ue) { // The JAXB specification does not mandate how the JAXB provider // must behave when attempting to unmarshal invalid XML data. // those cases, the JAXB provider is allowed to terminate the // call to unmarshal with an UnmarshalException. System.out.println("Caught UnmarshalException"); LOG.error("===>[3]ERROR Unmashaling \n\n" + ue.toString()); } catch (JAXBException je) { je.printStackTrace(); LOG.error("===>[4]ERROR Unmashaling \n\n" + je.toString()); LOG.error(Useful.getStackTrace(je)); } catch (Exception e) { LOG.error("===>[5]ERROR Unmashaling \n\n" + e.toString()); LOG.error(Useful.getStackTrace(e)); } if (obj == null) { LOG.error("===>[6]ERROR Unmashaling Object NULL"); } return obj; }