List of usage examples for org.dom4j.io OutputFormat createPrettyPrint
public static OutputFormat createPrettyPrint()
From source file:dk.netarkivet.common.utils.StreamUtils.java
License:Open Source License
/** * Write document tree to stream. Note, the stream is flushed, but not closed. * * @param doc the document tree to save. * @param os the stream to write xml to// w w w . j av a 2 s . com * @throws IOFailure On trouble writing XML to stream. */ public static void writeXmlToStream(Document doc, OutputStream os) { ArgumentNotValid.checkNotNull(doc, "Document doc"); ArgumentNotValid.checkNotNull(doc, "OutputStream os"); XMLWriter xwriter = null; try { try { OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding(UTF8_CHARSET); xwriter = new XMLWriter(os, format); xwriter.write(doc); } finally { if (xwriter != null) { xwriter.close(); } os.flush(); } } catch (IOException e) { String errMsg = "Unable to write XML to stream"; log.warn(errMsg, e); throw new IOFailure(errMsg, e); } }
From source file:dk.nsi.stamdata.replication.tools.SchemaGenerator.java
License:Mozilla Public License
public static void generate(Class<? extends View> entity, Writer writer) throws IOException { Document doc = DocumentFactory.getInstance().createDocument(); String targetNamespace = entity.getPackage().getAnnotation(XmlSchema.class).namespace(); String entityName = entity.getSimpleName().toLowerCase(); Element all = generate(doc, targetNamespace, entityName); for (Field method : entity.getDeclaredFields()) { if (method.isAnnotationPresent(XmlTransient.class)) continue; String name = method.getName(); String type = convert2SchemaType(method); addElement(all, name, type, null); }// w ww . j a v a2 s. c o m XMLWriter xmlWriter = new XMLWriter(writer, OutputFormat.createPrettyPrint()); xmlWriter.write(doc); }
From source file:dk.nsi.stamdata.replication.tools.SchemaGenerator.java
License:Mozilla Public License
public static void generate(RecordSpecification specification, Writer writer, String register) throws IOException { Document doc = DocumentFactory.getInstance().createDocument(); // FIXME: Register added to record spec. String namespace = Namespace.STAMDATA_3_0 + "/" + register; String entityName = specification.getTable().toLowerCase(); Element all = generate(doc, namespace, entityName); for (RecordSpecification.FieldSpecification field : specification.getFieldSpecs()) { addElement(all, field.name, convert2XsdType(field.type), field.length); }//from w ww . ja v a 2s .c o m all.addElement("xs:element").addAttribute("name", "validFrom").addAttribute("type", "xs:dateTime"); all.addElement("xs:element").addAttribute("name", "validTo").addAttribute("type", "xs:dateTime"); XMLWriter xmlWriter = new XMLWriter(writer, OutputFormat.createPrettyPrint()); xmlWriter.write(doc); }
From source file:dom4j.Dom4JExample.java
public void write(Document document) throws IOException { // lets write to a file XMLWriter writer;//from ww w . j a v a 2 s . c om // = new XMLWriter( // new BufferedOutputStream(outputStream)); // writer.write( document ); // writer.close(); // Pretty print the document to System.out System.out.println("\n\nPretty format"); OutputFormat format = OutputFormat.createPrettyPrint(); writer = new XMLWriter(System.out, format); writer.write(document); // Compact format to System.out System.out.println("\n\nCompact format"); format = OutputFormat.createCompactFormat(); writer = new XMLWriter(System.out, format); writer.write(document); }
From source file:edu.ccut.saturn.manager.dict.core.dbmanager.Dom4jUtils.java
License:Apache License
public static String writeXml(String file, Document doc) { String message = null;// w w w . java 2 s . co m XMLWriter writer = null; OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("GB2312"); format.setIndent(true); format.setIndent(" "); format.setNewlines(true); OutputStream out = null; try { out = new FileOutputStream(file); } catch (FileNotFoundException e1) { try { String path = Thread.currentThread().getContextClassLoader().getResource(BLANK_STRING).getFile() + file.substring(1); out = new FileOutputStream(path); } catch (FileNotFoundException e) { e.printStackTrace(); } } try { writer = new XMLWriter(out, format); writer.write(doc); } catch (Exception e) { e.printStackTrace(); } finally { try { writer.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } message = TRUE; return message; }
From source file:edu.ku.brc.dbsupport.ImportExportDB.java
License:Open Source License
/** * print all the records of a table. * @param dbTable the class name of the table *///from w w w .j a va 2 s. c om @SuppressWarnings("unchecked") public void printXML(String dbTable) { Session dom4jSession = session.getSession(EntityMode.DOM4J); String query = "from " + dbTable; //$NON-NLS-1$ List userXML = dom4jSession.createQuery(query).list(); try { OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(System.out, format); for (int i = 0; i < userXML.size(); i++) { Element writeMe = (Element) userXML.get(i); writer.write(writeMe); } } catch (Exception ex) { edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ImportExportDB.class, ex); ex.printStackTrace(); } System.out.println(); }
From source file:edu.ku.brc.dbsupport.ImportExportDB.java
License:Open Source License
/** * print a single record/*w ww . j ava 2s . c o m*/ * @param dbTable the class name of the table * @param id the id number of the record */ @SuppressWarnings("unchecked") public void printSingleRecordXML(String dbTable, int id) { Session dom4jSession = session.getSession(EntityMode.DOM4J); // load the object by using its primary key DBTableInfo info = DBTableIdMgr.getInstance().getInfoByTableName(dbTable.toLowerCase()); String primaryKey = info.getPrimaryKeyName(); String query = "from " + dbTable + " where " + primaryKey + " = " + id; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ List userXML = dom4jSession.createQuery(query).list(); try { OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(System.out, format); for (int i = 0; i < userXML.size(); i++) { Element writeMe = (Element) userXML.get(i); writer.write(writeMe); } } catch (Exception ex) { edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ImportExportDB.class, ex); ex.printStackTrace(); } System.out.println(); }
From source file:edu.ku.brc.dbsupport.ImportExportDB.java
License:Open Source License
/** * write a single record//from w w w.ja v a2s. c o m * @param dbTable the class name of the table * @param id the id number of the record */ @SuppressWarnings("unchecked") public void writeSingleRecordXML(String dbTable, int id) { FileOutputStream fout; Session dom4jSession = session.getSession(EntityMode.DOM4J); // load the object by using its primary key DBTableInfo info = DBTableIdMgr.getInstance().getInfoByTableName(dbTable.toLowerCase()); String primaryKey = info.getPrimaryKeyName(); String query = "from " + dbTable + " where " + primaryKey + " = " + id; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ List userXML = dom4jSession.createQuery(query).list(); try { fout = new FileOutputStream(importFolderPath + dbTable + ".xml"); //$NON-NLS-1$ PrintStream p = new PrintStream(fout); p.print("<root>"); //$NON-NLS-1$ OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(fout, format); for (int i = 0; i < userXML.size(); i++) { Element writeMe = (Element) userXML.get(i); writer.write(writeMe); } p.println("\n</root>"); //$NON-NLS-1$ p.close(); fout.close(); writer.close(); System.out.println("Wrote: " + dbTable + ".xml"); //$NON-NLS-1$ //$NON-NLS-2$ } catch (Exception ex) { edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ImportExportDB.class, ex); ex.printStackTrace(); } System.out.println(); }
From source file:edu.ku.brc.dbsupport.ImportExportDB.java
License:Open Source License
/** * write all the records of the given table. * @param dbTable the class name of the table * @return creates an xml file with name of the table *///from w ww . j a va2 s. c o m @SuppressWarnings("unchecked") public void writeXMLfile(String dataBase) { FileOutputStream fout; Session dom4jSession = session.getSession(EntityMode.DOM4J); String query = "from " + dataBase + " where id = 1"; //$NON-NLS-1$ //$NON-NLS-2$ System.out.println(query); List userXML = dom4jSession.createQuery(query).list(); try { fout = new FileOutputStream(importFolderPath + dataBase + ".xml"); //$NON-NLS-1$ PrintStream p = new PrintStream(fout); p.print("<root>"); //$NON-NLS-1$ OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(fout, format); for (int i = 0; i < userXML.size(); i++) { Element writeMe = (Element) userXML.get(i); writer.write(writeMe); } p.println("\n</root>"); //$NON-NLS-1$ p.close(); fout.close(); writer.close(); System.out.println("Wrote: " + dataBase + ".xml"); //$NON-NLS-1$ //$NON-NLS-2$ } catch (Exception ex) { edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ImportExportDB.class, ex); ex.printStackTrace(); } }
From source file:edu.ku.brc.specify.tasks.services.CollectingEventLocalityKMLGenerator.java
License:Open Source License
/** * Write the KML out to a file./*w ww . ja va 2s . c om*/ * * @param filename the name of the output file * @throws IOException a file I/O exception occurred */ public void outputToFile(final String filename) throws IOException { Document document = DocumentHelper.createDocument(); Element root = document.addElement("kml").addAttribute("xmlns", KML_NAMESPACE_DECL); Element kmlDocument = root.addElement("Document"); if (StringUtils.isNotEmpty(description)) { kmlDocument.addElement("description").addText(description); } GenericKMLGenerator.generateStyle(kmlDocument, placemarkIconURL, balloonStyleBgColor, balloonStyleTextColor, balloonStyleText); boolean isDoingCollectingEvents = false; DataProviderSessionIFace session = null; try { session = DataProviderFactory.getInstance().createSession(); for (int i = 0; i < dataObjs.size(); ++i) { String label = labels.get(i); FormDataObjIFace dataObj = dataObjs.get(i); session.attach(dataObj); if (dataObj instanceof CollectingEvent) { generatePlacemark(kmlDocument, (CollectingEvent) dataObj, label); isDoingCollectingEvents = true; } else if (dataObj instanceof Locality) { generatePlacemark(kmlDocument, (Locality) dataObj, label); } else if (dataObj instanceof CollectionObject) { generatePlacemark(kmlDocument, (CollectionObject) dataObj, label); } } } catch (Exception ex) { ex.printStackTrace(); } finally { if (session != null) { session.close(); } } if (isDoingCollectingEvents) { /*String kmlStr = generatePathForLocalities(); if (kmlStr != null) { writer.write(kmlStr); }*/ } FileWriter out = new FileWriter(filename); OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(out, format); writer.write(document); writer.close(); out.close(); }