List of usage examples for javax.xml.bind JAXBException printStackTrace
public void printStackTrace()
From source file:Main.java
public static <T> Element marshal(JAXBElement<T> jaxbElement, Class<T> cls) { try {//from ww w. j a v a2s .c om DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); JAXBContext jaxbContext = JAXBContext.newInstance(cls); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.marshal(jaxbElement, doc); return doc.getDocumentElement(); } catch (JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
From source file:org.pmedv.jake.JakeUtil.java
/** * Updates the {@link RecentFileList} with a new file * // ww w.j a v a 2 s . c o m * @param filename the name to append to the list */ public static void updateRecentFiles(String filename) { RecentFileList fileList = null; try { String inputDir = System.getProperty("user.home") + "/." + AppContext.getName() + "/"; String inputFileName = "recentFiles.xml"; File inputFile = new File(inputDir + inputFileName); if (inputFile.exists()) { Unmarshaller u = JAXBContext.newInstance(RecentFileList.class).createUnmarshaller(); fileList = (RecentFileList) u.unmarshal(inputFile); } if (fileList == null) fileList = new RecentFileList(); } catch (JAXBException e) { e.printStackTrace(); } if (fileList.getRecentFiles().size() >= 5) { fileList.getRecentFiles().remove(0); } if (!fileList.getRecentFiles().contains(filename)) fileList.getRecentFiles().add(filename); Marshaller m; try { String outputDir = System.getProperty("user.home") + "/." + AppContext.getName() + "/"; String outputFileName = "recentFiles.xml"; m = JAXBContext.newInstance(RecentFileList.class).createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); File output = new File(outputDir + outputFileName); m.marshal(fileList, output); } catch (JAXBException e) { e.printStackTrace(); } }
From source file:com.common.util.mapper.JaxbMapper.java
/** * Java Object->Xml with encoding./*from w w w .j a v a 2s . c om*/ */ public static String toXml(Object root, Class clazz, String encoding) { StringWriter writer = new StringWriter(); try { createMarshaller(clazz, encoding).marshal(root, writer); } catch (JAXBException e) { e.printStackTrace(); } return writer.toString(); }
From source file:com.common.util.mapper.JaxbMapper.java
/** * Xml->Java Object./*from ww w. j av a 2 s . com*/ */ public static <T> T fromXml(String xml, Class<T> clazz) { StringReader reader = new StringReader(xml); try { return (T) createUnmarshaller(clazz).unmarshal(reader); } catch (JAXBException e) { e.printStackTrace(); } return null; }
From source file:com.common.util.mapper.JaxbMapper.java
/** * Java Collection->Xml with encoding, ?Root ElementCollection. *//* w ww . ja va 2s . c o m*/ public static String toXml(Collection<?> root, String rootName, Class clazz, String encoding) { CollectionWrapper wrapper = new CollectionWrapper(); wrapper.collection = root; JAXBElement<CollectionWrapper> wrapperElement = new JAXBElement<CollectionWrapper>(new QName(rootName), CollectionWrapper.class, wrapper); StringWriter writer = new StringWriter(); try { createMarshaller(clazz, encoding).marshal(wrapperElement, writer); } catch (JAXBException e) { e.printStackTrace(); } return writer.toString(); }
From source file:eu.prestoprime.p4gui.connection.WorkflowConnection.java
public static WfDescriptor getWfDescriptor(P4Service service) { String path = service.getURL() + "/wf/descriptor"; try {//www. j a v a2s . co m P4HttpClient client = new P4HttpClient(service.getUserID()); HttpRequestBase request = new HttpGet(path); HttpResponse response = client.executeRequest(request); HttpEntity entity = response.getEntity(); if (entity != null) { WfDescriptor descriptor = (WfDescriptor) ModelUtils.getUnmarshaller(P4JAXBPackage.CONF) .unmarshal(entity.getContent()); return descriptor; } } catch (JAXBException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return new WfDescriptor(); }
From source file:com.tera.common.vcontext.service.ContextConfigurationParser.java
/** * @param bundle bundle of the loading resource * @param xsdSchema schema location of vcontext.xml * @param fileSystemXml resource to be loaded */// w w w. ja v a 2s . c o m public static final void parseVirtualConfiguration(Bundle bundle, URL xsdSchema, URL fileSystemXml) { Unmarshaller un = JaxbUtil.create(VContextTemplate.class, xsdSchema); try { VContextTemplate context = (VContextTemplate) un.unmarshal(fileSystemXml.openStream()); List<VElementTemplate> elements = context.getTemplates(); for (VElementTemplate template : elements) { String name = template.getName(); String path = template.getPath(); String target = template.getTarget(); switch (template.getType()) { case DM: ContextRegisterService.registerDM(bundle.getSymbolicName(), name); break; case CONFIG: ContextRegisterService.registerConfig(bundle.getSymbolicName(), name); break; case FILE: ContextRegisterService.registerFile(bundle.getResource(path), name, target); break; case PROPERTIES: ContextRegisterService.registerProperties(bundle.getResource(name), name); break; case COMMAND: ContextRegisterService.registerCommands(bundle.getSymbolicName(), bundle.getEntryPaths(path)); break; case SCHEMA_VERSION: ContextRegisterService.registerSchemaVersions(bundle.getResource(path)); break; } } } catch (JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:com.cloudera.api.model.JaxbAnnotationBehavior.java
private static <T> void testObject(String test, Class<? extends T> type, T object) { System.out.println(test);// www . j ava 2 s .c o m String objXML = null; String objJSON = null; try { objXML = ApiModelTest.objectToXml(object); objJSON = ApiModelTest.objectToJson(object); } catch (JAXBException e) { e.printStackTrace(); return; } catch (UnsupportedEncodingException e) { e.printStackTrace(); return; } catch (IOException e) { e.printStackTrace(); return; } if (objXML.contains(TEST_STRING)) { System.out.println("\tObject->XML contained the value."); } else { System.out.println("\tObject->XML DID NOT contain the value."); } if (objJSON.contains(TEST_STRING)) { System.out.println("\tObject->JSON contained the value."); } else { System.out.println("\tObject->JSON DID NOT contain the value."); } T objFromXML = null; T objFromJSON = null; try { objFromXML = ApiModelTest.xmlToObject(objXML, type); objFromJSON = ApiModelTest.jsonToObject(objJSON, type); } catch (JAXBException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (UnrecognizedPropertyException e) { System.out.println("\tJSON->Obj threw an UnrecognizedPropertyException"); } catch (IOException e) { e.printStackTrace(); } if (objFromXML == null) { System.out.println("\tXML->Obj returned a null object"); } else { String xmlValue = ((Valuable) objFromXML).getValue(); if (xmlValue == null) { System.out.println("\tXML->Obj has a NULL value"); } else if (xmlValue.equals(TEST_STRING)) { System.out.println("\tXML->Obj has the value."); } else { fail("XML->Obj has an unexpected value=" + xmlValue); } } if (objFromJSON == null) { System.out.println("\tJSON->Obj returned a null object"); } else { String jsonValue = ((Valuable) objFromJSON).getValue(); if (jsonValue == null) { System.out.println("\tJSON->Obj has a NULL value"); } else if (jsonValue.equals(TEST_STRING)) { System.out.println("\tJSON->Obj has the value."); } else { fail("JSON->Obj has an unexpected value=" + jsonValue); } } }
From source file:Main.java
public static String toXxml(Object bean) { StringWriter stringWriter = null; try {/* ww w . ja v a2s . com*/ JAXBContext jaxbContext = JAXBContext.newInstance(bean.getClass()); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); stringWriter = new StringWriter(); marshaller.marshal(bean, stringWriter); String result = stringWriter.toString(); // remove xml declaration result = result.replaceFirst(".*\n", ""); return result; } catch (JAXBException e) { throw new RuntimeException(e); } finally { if (stringWriter != null) try { stringWriter.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:com.cws.esolutions.core.listeners.CoreServiceInitializer.java
/** * Initializes the core service in a standalone mode - used for applications outside of a container or when * run as a standalone jar.//from w w w. j a v a 2 s .c om * * @param configFile - The service configuration file to utilize * @param logConfig - The logging configuration file to utilize * @param loadSecurity - Flag to start security * @param startConnections - Flag to start connections * @throws CoreServiceException @{link com.cws.esolutions.core.exception.CoreServiceException} * if an exception occurs during initialization */ public static void initializeService(final String configFile, final String logConfig, final boolean loadSecurity, final boolean startConnections) throws CoreServiceException { URL xmlURL = null; JAXBContext context = null; Unmarshaller marshaller = null; SecurityConfig secConfig = null; CoreConfigurationData configData = null; SecurityConfigurationData secConfigData = null; if (loadSecurity) { secConfigData = SecurityServiceBean.getInstance().getConfigData(); secConfig = secConfigData.getSecurityConfig(); } final String serviceConfig = (StringUtils.isBlank(configFile)) ? System.getProperty("coreConfigFile") : configFile; final String loggingConfig = (StringUtils.isBlank(logConfig)) ? System.getProperty("coreLogConfig") : logConfig; try { try { DOMConfigurator.configure(Loader.getResource(loggingConfig)); } catch (NullPointerException npx) { try { DOMConfigurator.configure(FileUtils.getFile(loggingConfig).toURI().toURL()); } catch (NullPointerException npx1) { System.err.println("Unable to load logging configuration. No logging enabled!"); System.err.println(""); npx1.printStackTrace(); } } xmlURL = CoreServiceInitializer.class.getClassLoader().getResource(serviceConfig); if (xmlURL == null) { // try loading from the filesystem xmlURL = FileUtils.getFile(configFile).toURI().toURL(); } context = JAXBContext.newInstance(CoreConfigurationData.class); marshaller = context.createUnmarshaller(); configData = (CoreConfigurationData) marshaller.unmarshal(xmlURL); CoreServiceInitializer.appBean.setConfigData(configData); if (startConnections) { Map<String, DataSource> dsMap = CoreServiceInitializer.appBean.getDataSources(); if (DEBUG) { DEBUGGER.debug("dsMap: {}", dsMap); } if (dsMap == null) { dsMap = new HashMap<String, DataSource>(); } for (DataSourceManager mgr : configData.getResourceConfig().getDsManager()) { if (!(dsMap.containsKey(mgr.getDsName()))) { StringBuilder sBuilder = new StringBuilder() .append("connectTimeout=" + mgr.getConnectTimeout() + ";") .append("socketTimeout=" + mgr.getConnectTimeout() + ";") .append("autoReconnect=" + mgr.getAutoReconnect() + ";") .append("zeroDateTimeBehavior=convertToNull"); if (DEBUG) { DEBUGGER.debug("StringBuilder: {}", sBuilder); } BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(mgr.getDriver()); dataSource.setUrl(mgr.getDataSource()); dataSource.setUsername(mgr.getDsUser()); dataSource.setConnectionProperties(sBuilder.toString()); dataSource.setPassword(PasswordUtils.decryptText(mgr.getDsPass(), mgr.getSalt(), secConfig.getSecretAlgorithm(), secConfig.getIterations(), secConfig.getKeyBits(), secConfig.getEncryptionAlgorithm(), secConfig.getEncryptionInstance(), configData.getAppConfig().getEncoding())); if (DEBUG) { DEBUGGER.debug("BasicDataSource: {}", dataSource); } dsMap.put(mgr.getDsName(), dataSource); } } if (DEBUG) { DEBUGGER.debug("dsMap: {}", dsMap); } CoreServiceInitializer.appBean.setDataSources(dsMap); } } catch (JAXBException jx) { jx.printStackTrace(); throw new CoreServiceException(jx.getMessage(), jx); } catch (MalformedURLException mux) { mux.printStackTrace(); throw new CoreServiceException(mux.getMessage(), mux); } }