List of usage examples for javax.xml.bind Marshaller setProperty
public void setProperty(String name, Object value) throws PropertyException;
From source file:edu.uci.ics.asterix.event.service.AsterixEventServiceUtil.java
private static void writeAsterixClusterConfigurationFile(AsterixInstance asterixInstance) throws IOException, EventException, JAXBException { String asterixInstanceName = asterixInstance.getName(); Cluster cluster = asterixInstance.getCluster(); JAXBContext ctx = JAXBContext.newInstance(Cluster.class); Marshaller marshaller = ctx.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(cluster, new FileOutputStream(AsterixEventService.getAsterixDir() + File.separator + asterixInstanceName + File.separator + "cluster.xml")); }
From source file:com.evolveum.midpoint.model.client.ModelClientUtil.java
public static <T> String marshallToSting(QName elementName, T object, boolean formatted) throws JAXBException { Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); if (formatted) { marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); }//from w w w .j a va 2s . c o m java.io.StringWriter sw = new StringWriter(); JAXBElement<T> element = new JAXBElement<T>(elementName, (Class<T>) object.getClass(), object); marshaller.marshal(element, sw); return sw.toString(); }
From source file:com.evolveum.midpoint.model.client.ModelClientUtil.java
public static <O extends ObjectType> String marshallToSting(O object, boolean formatted) throws JAXBException { Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); if (formatted) { marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); }//from ww w . j a v a 2 s . co m java.io.StringWriter sw = new StringWriter(); Class<O> type = (Class<O>) object.getClass(); JAXBElement<O> element = new JAXBElement<O>(getElementName(type), type, object); marshaller.marshal(element, sw); return sw.toString(); }
From source file:com.ikon.util.impexp.RepositoryExporter.java
/** * Performs a recursive repository content export with metadata *//*from w ww . j a v a 2 s.c om*/ private static ImpExpStats exportDocumentsHelper(String token, String fldPath, File fs, String metadata, boolean history, Writer out, InfoDecorator deco) throws FileNotFoundException, PathNotFoundException, AccessDeniedException, RepositoryException, IOException, DatabaseException, ParseException, NoSuchGroupException, MessagingException { log.debug("exportDocumentsHelper({}, {}, {}, {}, {}, {}, {})", new Object[] { token, fldPath, fs, metadata, history, out, deco }); ImpExpStats stats = new ImpExpStats(); DocumentModule dm = ModuleManager.getDocumentModule(); FolderModule fm = ModuleManager.getFolderModule(); MailModule mm = ModuleManager.getMailModule(); MetadataAdapter ma = MetadataAdapter.getInstance(token); Gson gson = new Gson(); String path = null; File fsPath = null; if (firstTime) { path = fs.getPath(); fsPath = new File(path); firstTime = false; } else { // Repository path needs to be "corrected" under Windoze path = fs.getPath() + File.separator + PathUtils.getName(fldPath).replace(':', '_'); fsPath = new File(path); fsPath.mkdirs(); FileLogger.info(BASE_NAME, "Created folder ''{0}''", fsPath.getPath()); if (out != null) { out.write(deco.print(fldPath, 0, null)); out.flush(); } } for (Iterator<Mail> it = mm.getChildren(token, fldPath).iterator(); it.hasNext();) { Mail mailChild = it.next(); path = fsPath.getPath() + File.separator + PathUtils.getName(mailChild.getPath()).replace(':', '_'); ImpExpStats mailStats = exportMail(token, mailChild.getPath(), path + ".eml", metadata, out, deco); // Stats stats.setSize(stats.getSize() + mailStats.getSize()); stats.setMails(stats.getMails() + mailStats.getMails()); } for (Iterator<Document> it = dm.getChildren(token, fldPath).iterator(); it.hasNext();) { Document docChild = it.next(); path = fsPath.getPath() + File.separator + PathUtils.getName(docChild.getPath()).replace(':', '_'); ImpExpStats docStats = exportDocument(token, docChild.getPath(), path, metadata, history, out, deco); // Stats stats.setSize(stats.getSize() + docStats.getSize()); stats.setDocuments(stats.getDocuments() + docStats.getDocuments()); } for (Iterator<Folder> it = fm.getChildren(token, fldPath).iterator(); it.hasNext();) { Folder fldChild = it.next(); ImpExpStats tmp = exportDocumentsHelper(token, fldChild.getPath(), fsPath, metadata, history, out, deco); path = fsPath.getPath() + File.separator + PathUtils.getName(fldChild.getPath()).replace(':', '_'); // Metadata if (metadata.equals("JSON")) { FolderMetadata fmd = ma.getMetadata(fldChild); String json = gson.toJson(fmd); FileOutputStream fos = new FileOutputStream(path + Config.EXPORT_METADATA_EXT); IOUtils.write(json, fos); fos.close(); } else if (metadata.equals("XML")) { FileOutputStream fos = new FileOutputStream(path + ".xml"); FolderMetadata fmd = ma.getMetadata(fldChild); JAXBContext jaxbContext; try { jaxbContext = JAXBContext.newInstance(FolderMetadata.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); // output pretty printed jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(fmd, fos); } catch (JAXBException e) { log.error(e.getMessage(), e); FileLogger.error(BASE_NAME, "XMLException ''{0}''", e.getMessage()); } } // Stats stats.setSize(stats.getSize() + tmp.getSize()); stats.setDocuments(stats.getDocuments() + tmp.getDocuments()); stats.setFolders(stats.getFolders() + tmp.getFolders() + 1); stats.setOk(stats.isOk() && tmp.isOk()); } log.debug("exportDocumentsHelper: {}", stats); return stats; }
From source file:main.java.refinement_class.Useful.java
public static void mashal(Object o, String xml_file, Class c) { Marshaller marshaller; // create a JAXBContext JAXBContext jaxbContext;/*from w ww . j a v a 2s .co m*/ 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:edu.uci.ics.asterix.event.service.AsterixEventServiceUtil.java
private static void writeAsterixConfigurationFile(AsterixInstance asterixInstance) throws IOException, JAXBException { String asterixInstanceName = asterixInstance.getName(); Cluster cluster = asterixInstance.getCluster(); String metadataNodeId = asterixInstance.getMetadataNodeId(); AsterixConfiguration configuration = asterixInstance.getAsterixConfiguration(); configuration.setInstanceName(asterixInstanceName); configuration.setMetadataNode(asterixInstanceName + "_" + metadataNodeId); String storeDir = null;/* ww w . j a v a 2 s. c o m*/ List<Store> stores = new ArrayList<Store>(); for (Node node : cluster.getNode()) { storeDir = node.getStore() == null ? cluster.getStore() : node.getStore(); stores.add(new Store(asterixInstanceName + "_" + node.getId(), storeDir)); } configuration.setStore(stores); List<Coredump> coredump = new ArrayList<Coredump>(); String coredumpDir = null; List<TransactionLogDir> txnLogDirs = new ArrayList<TransactionLogDir>(); String txnLogDir = null; for (Node node : cluster.getNode()) { coredumpDir = node.getLogDir() == null ? cluster.getLogDir() : node.getLogDir(); coredump.add(new Coredump(asterixInstanceName + "_" + node.getId(), coredumpDir + File.separator + asterixInstanceName + "_" + node.getId())); txnLogDir = node.getTxnLogDir() == null ? cluster.getTxnLogDir() : node.getTxnLogDir(); txnLogDirs.add(new TransactionLogDir(asterixInstanceName + "_" + node.getId(), txnLogDir)); } configuration.setCoredump(coredump); configuration.setTransactionLogDir(txnLogDirs); File asterixConfDir = new File(AsterixEventService.getAsterixDir() + File.separator + asterixInstanceName); asterixConfDir.mkdirs(); JAXBContext ctx = JAXBContext.newInstance(AsterixConfiguration.class); Marshaller marshaller = ctx.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); FileOutputStream os = new FileOutputStream(asterixConfDir + File.separator + ASTERIX_CONFIGURATION_FILE); marshaller.marshal(configuration, os); os.close(); }
From source file:com.ikon.util.impexp.RepositoryExporter.java
/** * Export document from openkm repository to filesystem. *//*from www. j av a2 s . c o m*/ public static ImpExpStats exportDocument(String token, String docPath, String destPath, String metadata, boolean history, Writer out, InfoDecorator deco) throws PathNotFoundException, RepositoryException, DatabaseException, IOException, AccessDeniedException, ParseException, NoSuchGroupException { DocumentModule dm = ModuleManager.getDocumentModule(); MetadataAdapter ma = MetadataAdapter.getInstance(token); Document docChild = dm.getProperties(token, docPath); Gson gson = new Gson(); ImpExpStats stats = new ImpExpStats(); // Version history if (history) { // Create dummy document file new File(destPath).createNewFile(); // Metadata if (metadata.equals("JSON")) { DocumentMetadata dmd = ma.getMetadata(docChild); String json = gson.toJson(dmd); FileOutputStream fos = new FileOutputStream(destPath + Config.EXPORT_METADATA_EXT); IOUtils.write(json, fos); IOUtils.closeQuietly(fos); } else if (metadata.equals("XML")) { FileOutputStream fos = new FileOutputStream(destPath + ".xml"); DocumentMetadata dmd = ma.getMetadata(docChild); JAXBContext jaxbContext; try { jaxbContext = JAXBContext.newInstance(DocumentMetadata.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); // output pretty printed jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(dmd, fos); } catch (JAXBException e) { log.error(e.getMessage(), e); FileLogger.error(BASE_NAME, "XMLException ''{0}''", e.getMessage()); } } for (Version ver : dm.getVersionHistory(token, docChild.getPath())) { String versionPath = destPath + "#v" + ver.getName() + "#"; FileOutputStream vos = new FileOutputStream(versionPath); InputStream vis = dm.getContentByVersion(token, docChild.getPath(), ver.getName()); IOUtils.copy(vis, vos); IOUtils.closeQuietly(vis); IOUtils.closeQuietly(vos); FileLogger.info(BASE_NAME, "Created document ''{0}'' version ''{1}''", docChild.getPath(), ver.getName()); // Metadata if (metadata.equals("JSON")) { VersionMetadata vmd = ma.getMetadata(ver, docChild.getMimeType()); String json = gson.toJson(vmd); vos = new FileOutputStream(versionPath + Config.EXPORT_METADATA_EXT); IOUtils.write(json, vos); IOUtils.closeQuietly(vos); } else if (metadata.equals("XML")) { FileOutputStream fos = new FileOutputStream(destPath + ".xml"); VersionMetadata vmd = ma.getMetadata(ver, docChild.getMimeType()); JAXBContext jaxbContext; try { jaxbContext = JAXBContext.newInstance(VersionMetadata.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); // output pretty printed jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(vmd, fos); } catch (JAXBException e) { log.error(e.getMessage(), e); FileLogger.error(BASE_NAME, "XMLException ''{0}''", e.getMessage()); } } } } else { FileOutputStream fos = new FileOutputStream(destPath); InputStream is = dm.getContent(token, docChild.getPath(), false); IOUtils.copy(is, fos); IOUtils.closeQuietly(is); IOUtils.closeQuietly(fos); FileLogger.info(BASE_NAME, "Created document ''{0}''", docChild.getPath()); // Metadata if (metadata.equals("JSON")) { DocumentMetadata dmd = ma.getMetadata(docChild); String json = gson.toJson(dmd); fos = new FileOutputStream(destPath + Config.EXPORT_METADATA_EXT); IOUtils.write(json, fos); IOUtils.closeQuietly(fos); } else if (metadata.equals("XML")) { fos = new FileOutputStream(destPath + ".xml"); DocumentMetadata dmd = ma.getMetadata(docChild); JAXBContext jaxbContext; try { jaxbContext = JAXBContext.newInstance(DocumentMetadata.class); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); // output pretty printed jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(dmd, fos); } catch (JAXBException e) { log.error(e.getMessage(), e); FileLogger.error(BASE_NAME, "XMLException ''{0}''", e.getMessage()); } } } if (out != null) { out.write(deco.print(docChild.getPath(), docChild.getActualVersion().getSize(), null)); out.flush(); } // Stats stats.setSize(stats.getSize() + docChild.getActualVersion().getSize()); stats.setDocuments(stats.getDocuments() + 1); return stats; }
From source file:labr_client.xml.ObjToXML.java
public static String marshallRequest(LabrRequest request) { String xml = ""; String date = String.format("%1$tY%1$tm%1$td", new Date()); String time = String.format("%1$tH%1$tM%1$tS", new Date()); try {//ww w . j a va 2s . co m JAXBContext context = JAXBContext.newInstance(LabrRequest.class); Marshaller m = context.createMarshaller(); //for pretty-print XML in JAXB m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); File labrRequest = new File("data/tmp/" + date + time + ".xml"); m.marshal(request, labrRequest); xml = FileUtils.readFileToString(labrRequest); } catch (JAXBException | IOException ex) { Logger.getLogger(ObjToXML.class.getName()).log(Level.SEVERE, null, ex); } return xml; }
From source file:labr_client.xml.ObjToXML.java
public static String marshallRequest(KmehrMessage message) { String xml = ""; String date = String.format("%1$tY%1$tm%1$td", new Date()); String time = String.format("%1$tH%1$tM%1$tS", new Date()); try {//from ww w . j a v a 2s .c om JAXBContext context = JAXBContext.newInstance(LabrRequest.class); Marshaller m = context.createMarshaller(); //for pretty-print XML in JAXB m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); File kmehrMessage = new File("data/tmp/kmehr" + date + time + ".xml"); m.marshal(message, kmehrMessage); xml = FileUtils.readFileToString(kmehrMessage); } catch (JAXBException | IOException ex) { Logger.getLogger(ObjToXML.class.getName()).log(Level.SEVERE, null, ex); } return xml; }
From source file:main.java.refinement_class.Useful.java
public static String mashal2(Object o, Class c) { Marshaller marshaller; // create a JAXBContext JAXBContext jaxbContext;/* ww w. j ava 2 s .c o m*/ String xmlString = ""; try { jaxbContext = JAXBContext.newInstance(c); marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true)); // marshaller.marshal(o, new FileOutputStream(xml_file)); StringWriter sw = new StringWriter(); marshaller.marshal(o, sw); xmlString = sw.toString(); } catch (JAXBException e) { e.printStackTrace(); } return xmlString; }