List of usage examples for javax.xml.bind JAXBException toString
public String toString()
From source file:cz.lbenda.dataman.rc.DbConfigFactory.java
public static String storeStructureCache(DbConfig dbConfig) { cz.lbenda.dataman.schema.dbstructure.ObjectFactory of = new cz.lbenda.dataman.schema.dbstructure.ObjectFactory(); try {//from ww w.java2s . c om JAXBContext jc = JAXBContext.newInstance(cz.lbenda.dataman.schema.dbstructure.ObjectFactory.class); Marshaller m = jc.createMarshaller(); StringWriter sw = new StringWriter(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); DatabaseStructureType dst = DbStructureFactory.createXMLDatabaseStructure(dbConfig.getCatalogs()); JAXBElement<DatabaseStructureType> element = of.createDatabaseType(dst); m.marshal(element, sw); return sw.toString(); } catch (JAXBException e) { LOG.error("Problem with write configuration: " + e.toString(), e); throw new RuntimeException("Problem with write configuration: " + e.toString(), e); } }
From source file:cz.lbenda.dataman.rc.DbConfigFactory.java
public static String storeToString(Callback<DbConfig, String> cacheDbStructureWriteFactory) { cz.lbenda.dataman.schema.dataman.ObjectFactory of = new cz.lbenda.dataman.schema.dataman.ObjectFactory(); DatamanType config = of.createDatamanType(); config.setSessions(of.createSessionsType()); for (DbConfig sc : getConfigurations()) { config.getSessions().getSession() .add(sc.storeToSessionType(null, cacheDbStructureWriteFactory.call(sc))); }/*from ww w. j ava 2s. com*/ try { JAXBContext jc = JAXBContext.newInstance(cz.lbenda.dataman.schema.dataman.ObjectFactory.class); Marshaller m = jc.createMarshaller(); StringWriter sw = new StringWriter(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); m.marshal(of.createDataman(config), sw); return sw.toString(); } catch (JAXBException e) { LOG.error("Problem with write configuration: " + e.toString(), e); throw new RuntimeException("Problem with write configuration: " + e.toString(), e); } }
From source file:cz.lbenda.dataman.rc.DbConfigFactory.java
public static void load(final String document) { if (StringUtils.isBlank(document)) { return;//from w w w . j a va 2 s . c om } try { JAXBContext jc = JAXBContext.newInstance(cz.lbenda.dataman.schema.dataman.ObjectFactory.class); Unmarshaller u = jc.createUnmarshaller(); JAXBElement o = (JAXBElement) u.unmarshal(new StringReader(document)); if (o.getValue() instanceof DatamanType) { DatamanType dc = (DatamanType) o.getValue(); if (dc.getSessions() == null || dc.getSessions().getSession().isEmpty()) { LOG.info("No configuration for loading"); } else { configurations.clear(); for (SessionType session : dc.getSessions().getSession()) { DbConfig sc = new DbConfig(); configurations.add(sc); sc.fromSessionType(session, true); } } } else { LOG.error("The string didn't contains dataman configuration: " + o.getClass().getName()); } } catch (JAXBException e) { LOG.error("Problem with reading dataman configuration\nXML:" + document + "\n" + e.toString(), e); } }
From source file:main.java.refinement_class.Useful.java
public static void mashal(Object o, String xml_file, Class c) { Marshaller marshaller;/*from w ww. j a va2 s . co m*/ // create a JAXBContext JAXBContext jaxbContext; try { jaxbContext = JAXBContext.newInstance(c); marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true)); marshaller.marshal(o, new FileOutputStream(xml_file)); } catch (JAXBException e) { e.printStackTrace(); LOG.error(e.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); LOG.error(e.toString()); } }
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 ww w .ja v a 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 { 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 boolean validation(String schema_file, String xml_file) { Object obj = null;/*from ww w . j a va2s.c o m*/ // 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 unmashal(String schema_file, String xml_file, Class c) { Object obj = null;//from w w w.ja v a 2 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 { // 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; }
From source file:cz.lbenda.dataman.db.DbStructureFactory.java
public static void loadDatabaseStructureFromXML(InputStream dbStructure, DbConfig dbConfig) { try {/*from w ww . jav a2 s .c o m*/ JAXBContext jc = JAXBContext.newInstance(cz.lbenda.dataman.schema.dbstructure.ObjectFactory.class); Unmarshaller um = jc.createUnmarshaller(); Object ob = um.unmarshal(dbStructure); if (ob instanceof JAXBElement && ((JAXBElement) ob).getValue() instanceof DatabaseStructureType) { //noinspection unchecked loadDatabaseStructureFromXML(((JAXBElement<DatabaseStructureType>) ob).getValue(), dbConfig); } else { throw new RuntimeException( "The file with database structure not contains XML with cached database structure."); } } catch (JAXBException e) { LOG.error("Problem with read cached database structure configuration: " + e.toString(), e); throw new RuntimeException("Problem with read cached database structure configuration: " + e.toString(), e); } }
From source file:cz.lbenda.dataman.db.ExportTableData.java
/** Write rows from sql query to output stream * @param sqlQueryRows rows/*w w w . ja v a 2 s. c o m*/ * @param outputStream stream to which are data write */ public static void writeSqlQueryRowsToXMLv1(SQLQueryRows sqlQueryRows, OutputStream outputStream) { ObjectFactory of = new ObjectFactory(); ExportType export = of.createExportType(); export.setSql(sqlQueryRows.getSQL()); export.setVersion("1"); ColumnsType columnsType = of.createColumnsType(); export.setColumns(columnsType); Map<ColumnDesc, ColumnType> ctsMap = new HashMap<>(); sqlQueryRows.getMetaData().getColumns().forEach(cd -> { ColumnType ct = of.createColumnType(); ctsMap.put(cd, ct); ct.setId(columnId(cd)); ct.setCatalog(cd.getCatalog()); ct.setSchema(cd.getSchema()); ct.setTable(cd.getTable()); ct.setColumn(cd.getName()); ct.setDataType(DbStructureFactory.columnTypeToDataTypeType(cd.getDataType())); ct.setLength(cd.getSize()); ct.setScale(cd.getScale()); ct.setValue(cd.getLabel()); columnsType.getColumn().add(ct); }); sqlQueryRows.getRows().forEach(row -> { RowType rowType = of.createRowType(); sqlQueryRows.getMetaData().getColumns().forEach(cd -> { FieldType field = of.createFieldType(); field.setColumn(ctsMap.get(cd)); if (row.isColumnNull(cd)) { field.setNull(true); } else { field.setValue(row.getColumnValueStr(cd)); } rowType.getField().add(field); }); export.getRow().add(rowType); }); try { JAXBContext jc = JAXBContext.newInstance(cz.lbenda.dataman.schema.export.ObjectFactory.class); Marshaller m = jc.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE); m.marshal(of.createExport(export), outputStream); } catch (JAXBException e) { LOG.error("Problem with write exporting data: " + e.toString(), e); throw new RuntimeException("Problem with write exporting data: " + e.toString(), e); } }
From source file:cz.lbenda.dataman.db.ExtConfFactory.java
private void loadExConfType(Reader reader) { if (reader == null) { loadExConfType((ExConfType) null); } else {/*from w ww . ja va2 s. c o m*/ try { JAXBContext jc = JAXBContext.newInstance(cz.lbenda.dataman.schema.exconf.ObjectFactory.class); Unmarshaller u = jc.createUnmarshaller(); JAXBElement o = (JAXBElement) u.unmarshal(reader); if (o.getValue() instanceof ExConfType) { loadExConfType((ExConfType) o.getValue()); } else { LOG.error("The file didn't contains expected configuration: " + o.getClass().getName()); } } catch (JAXBException e) { LOG.error("Problem with reading extended configuration: " + e.toString(), e); } } }