List of usage examples for org.jdom2.output XMLOutputter XMLOutputter
public XMLOutputter(XMLOutputProcessor processor)
XMLOutputter
with the specified XMLOutputProcessor. From source file:edu.unc.lib.deposit.normalize.Proquest2N3BagJob.java
License:Apache License
/** * Transform the given root element from the data document into MODS and stores it as the metadata for the object * being ingested/*from w w w . j ava2 s . c o m*/ * * @param primaryPID * @param dataRoot * @param modified * @throws TransformerException * @throws FileNotFoundException * @throws IOException */ private Document extractMods(PID primaryPID, Element dataRoot, DateTime modified) throws TransformerException, FileNotFoundException, IOException { int month = modified.getMonthOfYear(); String gradSemester; if (month >= 2 && month <= 6) { gradSemester = "Spring"; } else if (month >= 7 && month <= 9) { gradSemester = "Summer"; } else { gradSemester = "Winter"; } JDOMResult mods = new JDOMResult(); // Transform the metadata into MODS synchronized (proquest2ModsTransformer) { proquest2ModsTransformer.setParameter("graduationSemester", gradSemester + " " + modified.getYear()); proquest2ModsTransformer.transform(new JDOMSource(dataRoot), mods); } // Create the description folder and write the MODS out to it final File modsFolder = getDescriptionDir(); modsFolder.mkdir(); File modsFile = new File(modsFolder, primaryPID.getUUID() + ".xml"); try (FileOutputStream fos = new FileOutputStream(modsFile)) { new XMLOutputter(Format.getPrettyFormat()).output(mods.getDocument(), fos); } return mods.getDocument(); }
From source file:edu.unc.lib.deposit.normalize.VocabularyEnforcementJob.java
License:Apache License
@Override public void runJob() { Model model = getWritableModel();//from w w w . j a v a 2 s . c o m // Get the list of all objects being ingested in this job List<String> resourcePIDs = new ArrayList<>(); Bag deposit = model.getBag(getDepositPID().getURI()); walkChildrenDepthFirst(deposit, resourcePIDs, true); SAXBuilder sb = new SAXBuilder(new XMLReaderSAX2Factory(false)); // Vocabulary mappings need to be resolved against the destination since they are not in the hierarchy yet PID destinationPID = new PID(getDepositStatus().get(DepositField.containerId.name())); for (String resourcePID : resourcePIDs) { PID pid = new PID(resourcePID); File modsFile = new File(getDescriptionDir(), pid.getUUID() + ".xml"); // Check if the resource has a description if (modsFile.exists()) { try { Document modsDoc = sb.build(modsFile); // Update the MODS document to use approved terms when possible if the vocabularies support remapping log.debug("Updating document terms for {} within destination {}", pid, destinationPID); boolean modified = updateDocumentTerms(destinationPID, modsDoc.getRootElement()); // Update the mods document if it was changed if (modified) { try (FileOutputStream fos = new FileOutputStream(modsFile)) { new XMLOutputter(Format.getPrettyFormat()).output(modsDoc.getDocument(), fos); } } // Capture any invalid affiliations as relations log.debug("Adding invalid terms for {} within destination {}", pid, destinationPID); addInvalidTerms(pid, destinationPID, modsDoc.getRootElement(), model); } catch (JDOMException | IOException e) { log.error("Failed to parse description file {}", modsFile.getAbsolutePath(), e); } } } }
From source file:es.ucm.fdi.ac.Analysis.java
License:Open Source License
/** * Saves this analysis to a file//from w w w . java 2s .c om * * @param f the file to write to * @throws java.io.IOException */ public void saveToFile(File f) throws IOException { FileOutputStream fos = null; try { XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()); fos = new FileOutputStream(f); outputter.output(new Document(saveToXML()), fos); } finally { if (fos != null) { fos.close(); } } }
From source file:es.ucm.fdi.ac.gui.MainGui.java
License:Open Source License
/** * Load a test help file, adhering to the following structure: * <helppairs>/*from w w w .j av a 2 s . c o m*/ * <testhelp> * <testname>...</testname> * <helpcontent>...</helpcontent> * </testhelp> * ... * </helppairs> * Notice that 'helpcontent' should be XHTML: old-fashioned HTML may fail to * validate. */ public HashMap<String, String> loadTestHelpFile(String fileName) throws IOException { HashMap<String, String> m = new HashMap<String, String>(); XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()); URL url = getClass().getClassLoader().getResource(fileName); try { Document doc = (new SAXBuilder()).build(url); for (Element th : doc.getRootElement().getChildren()) { m.put(th.getChildTextTrim("testname"), outputter.outputString(th.getChild("helpcontent"))); } } catch (JDOMException | NullPointerException e) { throw new IOException("Impossible to read XML file for " + url, e); } return m; }
From source file:es.ucm.fdi.clover.gui.CloverSave.java
License:Open Source License
/** * Saves the CloverSave to an XML file/* ww w . j a v a2 s . c o m*/ */ public static void save(Collection<ClusterView> views, File f) throws IOException { Element root = new Element("clover"); root.setAttribute("version", saveVersion); root.setAttribute("requiresVersion", compatibleWith); root.setAttribute("date", new Date().toString()); Element shared = new Element("shared"); root.addContent(shared); HashMap<Filter, String> filterToId = new HashMap<Filter, String>(); HashMap<ClusteringEngine, String> engineToId = new HashMap<ClusteringEngine, String>(); HashMap<ClusterHierarchy, String> hierarchyToId = new HashMap<ClusterHierarchy, String>(); for (ClusterView v : views) { Element view = new Element("view"); v.save(view); Element layoutCache = new Element("layoutCache"); v.getAnimator().getLayoutCache().save(layoutCache, v.getBase()); view.addContent(layoutCache); Element animatorProps = new Element("animatorProps"); v.getAnimator().save(animatorProps); view.addContent(animatorProps); ClusteredGraph cg = (ClusteredGraph) v.getBase(); ClusterHierarchy h = cg.getHierarchy(); BaseGraph bg = cg.getBase(); // create the hierarchy element (if necessary) if (!hierarchyToId.containsKey(h)) { String hierarchyId = "" + generateId(); hierarchyToId.put(h, hierarchyId); Element hierarchy = new Element("hierarchy"); hierarchy.setAttribute("id", hierarchyId); h.save(hierarchy); // save the filtering used in the hierarchy (if necessary) if (bg instanceof FilteredGraph) { Filter filter = ((FilteredGraph) bg).getFilter(); if (!filterToId.containsKey(filter)) { String filterId = "" + generateId(); filterToId.put(filter, filterId); Element fe = new Element("filter"); fe.setAttribute("id", filterId); filter.save(fe); shared.addContent(fe); } hierarchy.setAttribute("filterId", filterToId.get(filter)); } // save the hierarchy itself: clustering and update-engine ClusteringEngine e = h.getEngine(); Element engine = new Element("engine"); engine.setAttribute("engineClass", e.getClass().getName()); e.save(engine); hierarchy.addContent(engine); shared.addContent(hierarchy); } view.setAttribute("hierarchyId", hierarchyToId.get(h)); root.addContent(view); } XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()); outputter.output(new Document(root), new FileOutputStream(f)); }
From source file:es.upm.dit.xsdinferencer.Results.java
License:Apache License
/** * Converts a Map<String,Document> to a Map<String,String> with the same keys and String representations * of the documents as values.<br/> * The XMLs are generated using the format returned by {@link Format#getPrettyFormat()} and the system line separator. * For more information about this, see {@link Format}. * @param inputMap a map between Strings and Documents * @return a map between the Strings and the String representation of the documents (null if inputMap is null). *///from w ww.j a v a2 s . c o m private Map<String, String> getStringMapFromXMLMap(Map<String, Document> inputMap, TextMode textMode) { if (inputMap == null) { return null; } Map<String, String> results = new HashMap<>(inputMap.size()); Format xmlFormat = Format.getPrettyFormat(); xmlFormat.setLineSeparator(LineSeparator.SYSTEM); //xmlFormat.setTextMode(textMode); XMLOutputter outputter = new XMLOutputter(xmlFormat); for (String fileName : inputMap.keySet()) { Document currentDocument = inputMap.get(fileName); String xsdString = outputter.outputString(currentDocument); results.put(fileName, xsdString); } return ImmutableMap.copyOf(results); }
From source file:eu.himeros.cophi.ocr.proofreader.controller.pojo.HocrDocumentBufferedWriter.java
License:Open Source License
/** * Saves the hocrDocument/* www . j a va2 s. c o m*/ * @param hocrDocument the hocr document. * @param bw the buffered writer. */ @Override public void save(Document hocrDocument, BufferedWriter bw) { try { XMLOutputter xop = new XMLOutputter(Format.getPrettyFormat().setLineSeparator("\n")); xop.output(hocrDocument, bw); } catch (Exception ex) { ex.printStackTrace(System.err); } }
From source file:eu.himeros.digitaledition.AlignedQuotationParser.java
License:Open Source License
public static void align(String inFileName, String outFileName) throws Exception { AlignedQuotationParser aqp = new AlignedQuotationParser(); Element rootOut = aqp.parse(inFileName); //e.g. xxx002_001_ft-xi_frag.xml XMLOutputter xop = new XMLOutputter( Format.getPrettyFormat().setEncoding("UTF-8").setLineSeparator("\n").setIndent(" ")); String output = xop.outputString(rootOut); System.out.println(output);//from w ww. j a va 2 s.co m }
From source file:eu.himeros.hocr.FlatXml.java
License:Open Source License
private void init(File inFile, File outFile) throws Exception { SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(inFile); Element root = doc.getRootElement(); Namespace oldns = root.getNamespace(); Element newRoot = new Element("html", "http://www.w3.org/1999/xhtml"); Namespace xmlns = newRoot.getNamespace(); Element head = root.getChild("head", oldns); head.setNamespace(xmlns);// w w w.ja v a 2 s .com for (Element child : head.getChildren()) child.setNamespace(xmlns); Element title = new Element("title", xmlns); title.addContent("ocr"); if (head != null) head.addContent(title); Element body = root.getChild("body", oldns); body.setNamespace(xmlns); /*Element oldPage; try{ oldPage=body.getChild("div",xmlns); }catch(Exception ex){ oldPage=new Element("div",xmlns); }*/ Element page = new Element("div", xmlns); page.setAttribute("class", "ocr_page"); page.setAttribute("id", "i" + inFile.getName().substring(1).replace(".html", ".png")); XPathExpression<Element> xpath = XPathFactory.instance().compile("//*[@class='ocr_carea']", Filters.element(), null, Namespace.getNamespace("ns", "http://www.w3.org/1999/xhtml")); List<Element> careaElL = xpath.evaluate(body); for (Element careaEl : careaElL) { page.addContent(new Comment("<div class=\"" + careaEl.getAttributeValue("class") + "\" title=\"" + careaEl.getAttributeValue("title") + "\">")); for (Element pEl : careaEl.getChildren()) { page.addContent(new Comment("<p>")); for (Element lineEl : pEl.getChildren()) { lineEl.removeAttribute("id"); lineEl.setNamespace(xmlns); for (Element child : lineEl.getChildren()) { child.removeAttribute("id"); child.removeAttribute("lang"); child.removeAttribute("lang", xmlns); child.setNamespace(xmlns); } page.addContent(lineEl.clone()); } page.addContent(new Comment("</p>")); } page.addContent(new Comment("</div>")); } //oldPage.detach(); if (body != null) { body.removeContent(); body.addContent(page); } newRoot.addContent(root.removeContent()); doc.detachRootElement(); doc.setRootElement(newRoot); XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat()); xmlOutputter.output(doc, new BufferedWriter(new FileWriter(outFile))); }
From source file:eu.himeros.hocr.HocrInfoAggregator.java
License:Open Source License
public void output(String outFileName) { try (BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(outFileName), "UTF-8"))) { xop = new XMLOutputter(Format.getPrettyFormat().setLineSeparator("\n")); makeCompliantHocr();/*ww w .ja va 2 s .c o m*/ xop.output(doc, bw); } catch (Exception ex) { ex.printStackTrace(System.err); } }