List of usage examples for org.jdom2 Document getRootElement
public Element getRootElement()
Element
for this Document
From source file:de.hbrs.oryx.yawl.converter.layout.YAWLLayoutConverter.java
License:Open Source License
private void initXMLReader() throws JDOMException, IOException { SAXBuilder builder = new SAXBuilder(); Document document = builder.build(new StringReader(specificationString)); Element root = document.getRootElement(); layout = root.getChild("layout", root.getNamespace()); if (layout == null) layout = yawlLayoutArranger.arrangeLayout(root); yawlNamespace = layout.getNamespace(); setYAWLLocale(layout);//from w w w.j av a 2 s .co m }
From source file:de.herm_detlef.java.application.io.Export.java
License:Apache License
private static Document createJdomDocument(ObservableList<ExerciseItem> exerciseItemList) { Namespace ns = Namespace.getNamespace(ApplicationConstants.XML_NAMESPACE); Element catalog = new Element(TAG.CATALOG.name(), ns); Document doc = new Document(catalog); Namespace xsi = Namespace.getNamespace("xsi", ApplicationConstants.XML_SCHEMA_INSTANCE); doc.getRootElement().addNamespaceDeclaration(xsi); doc.getRootElement().setAttribute("schemaLocation", ApplicationConstants.XML_SCHEMA_DEFINITION, xsi); int count = 0; for (ExerciseItem exItem : exerciseItemList) { Element item = new Element(TAG.ITEM.name(), ns); catalog.addContent(item);/*from ww w . j av a 2s . c o m*/ Element id = new Element(TAG.ID.name(), ns); id.addContent(String.valueOf(++count)); item.addContent(id); Element element = null; for (ItemPart itemPart : exItem.getExerciseItemParts()) { Object obj = itemPart.get(); if (obj == null) { // assert false : String.format( // "%s", // itemPart.getClass() ); // TODO continue; } if (itemPart instanceof QuestionText || itemPart instanceof QuestionText2) { isAnswerPart = false; if (!isQuestionPart) { element = new Element(TAG.QUESTION.name(), ns); item.addContent(element); isQuestionPart = true; } if (element == null) continue; if (itemPart instanceof QuestionText) { Element text = new Element(TAG.TEXT.name(), ns); element.addContent(text); text.addContent(((QuestionText) itemPart).getStr().get()); } if (itemPart instanceof QuestionText2) { Element text = new Element(TAG.TEXT2.name(), ns); element.addContent(text); text.addContent(((QuestionText2) itemPart).getStr().get()); } } else if (itemPart instanceof QuestionCode) { isAnswerPart = false; if (!isQuestionPart) { element = new Element(TAG.QUESTION.name(), ns); item.addContent(element); isQuestionPart = true; } if (element == null) continue; Element code = new Element(TAG.CODE.name(), ns); element.addContent(code); code.addContent(((QuestionCode) itemPart).getStr().get()); } else if (itemPart instanceof SingleChoiceAnswerText) { isQuestionPart = false; if (!isAnswerPart) { element = new Element(TAG.SINGLE_CHOICE_ANSWER.name(), ns); item.addContent(element); isAnswerPart = true; } if (element == null) continue; Element answer = new Element(TAG.TEXT.name(), ns); element.addContent(answer); if (((AnswerText) itemPart).isMarked()) { Attribute mark = new Attribute("mark", "true"); answer.setAttribute(mark); } answer.addContent(((SingleChoiceAnswerText) itemPart).getStr().get()); } else if (itemPart instanceof MultipleChoiceAnswerText) { isQuestionPart = false; if (!isAnswerPart) { element = new Element(TAG.MULTIPLE_CHOICE_ANSWER.name(), ns); item.addContent(element); isAnswerPart = true; } if (element == null) continue; Element answer = new Element(TAG.TEXT.name(), ns); element.addContent(answer); if (((AnswerText) itemPart).isMarked()) { Attribute mark = new Attribute("mark", "true"); answer.setAttribute(mark); } answer.addContent(((MultipleChoiceAnswerText) itemPart).getStr().get()); } else if (itemPart instanceof SolutionText) { isQuestionPart = false; isAnswerPart = false; element = new Element(TAG.SOLUTION.name(), ns); item.addContent(element); Element solution = new Element(TAG.TEXT.name(), ns); element.addContent(solution); solution.addContent(((SolutionText) itemPart).getStr().get()); } if (element == null) { assert false; return null; } } } return doc; }
From source file:de.herm_detlef.java.application.io.Import.java
License:Apache License
public static ArrayList<ExerciseItem> importExerciseItemListFromFile(String filename) { exerciseItemList = new ArrayList<ExerciseItem>(); try {/* w w w. j a va 2 s .c o m*/ Document doc = createDocument(filename); Element catalogElement = doc.getRootElement(); createNode(catalogElement); } catch (JDOMException | IOException e) { Utilities.showErrorMessage(e.getClass().getSimpleName(), e.getMessage()); e.printStackTrace(); return null; } return exerciseItemList; }
From source file:de.huberlin.german.korpling.laudatioteitool.MergeTEI.java
License:Apache License
private void mergeMainCorpusHeader(Element root) throws SAXException, IOException, LaudatioException, JDOMException { // append global header File corpusHeaderDir = new File(inputDir, "CorpusHeader"); Preconditions.checkArgument(corpusHeaderDir.isDirectory()); File[] corpusHeaderFiles = corpusHeaderDir.listFiles(new FilenameFilter() { @Override/* www.ja va 2s . c om*/ public boolean accept(File dir, String name) { return name.endsWith(".xml"); } }); Preconditions.checkArgument(corpusHeaderFiles.length > 0); File headerFile = corpusHeaderFiles[0]; TEIValidator validator = corpusSchemeURL == null ? new TEICorpusValidator() : new FromURLValidator(corpusSchemeURL); if (validator.validate(headerFile)) { SAXBuilder sax = new SAXBuilder(); Document corpusDoc = sax.build(headerFile); // remove the pending text element corpusDoc.getRootElement().removeChild("text", null); // append to our new root root.addContent(corpusDoc.getRootElement().getChild("teiHeader", null).clone()); } else { System.err.println(validator.toString()); throw new LaudatioException("Corpus header is not valid"); } }
From source file:de.huberlin.german.korpling.laudatioteitool.MergeTEI.java
License:Apache License
private void mergeDocumentHeader(Element root) throws SAXException, JDOMException, IOException, LaudatioException { // append document headers File documentHeaderDir = new File(inputDir, "DocumentHeader"); Preconditions.checkArgument(documentHeaderDir.isDirectory()); File[] documentHeaderFiles = documentHeaderDir.listFiles(new FilenameFilter() { @Override// ww w . j a v a 2s .co m public boolean accept(File dir, String name) { return name.endsWith(".xml"); } }); Preconditions.checkArgument(documentHeaderFiles.length > 0); SAXBuilder sax = new SAXBuilder(); TEIValidator validator = documentSchemeURL == null ? new TEIDocumentValidator() : new FromURLValidator(documentSchemeURL); for (File f : documentHeaderFiles) { if (validator.validate(f)) { Document documentDoc = sax.build(f); // remove the pending text element documentDoc.getRootElement().removeChild("text", null); // append to our new root root.addContent(documentDoc.getRootElement().getChild("teiHeader", null).clone()); } else { System.err.println(validator.toString()); throw new LaudatioException("A document header is not valid"); } } }
From source file:de.huberlin.german.korpling.laudatioteitool.MergeTEI.java
License:Apache License
private void mergePreparationHeader(Element root) throws SAXException, JDOMException, IOException, LaudatioException { // append preparation headers File preparationHeaderDir = new File(inputDir, "PreparationHeader"); Preconditions.checkState(preparationHeaderDir.isDirectory()); File[] preparationHeaderFiles = preparationHeaderDir.listFiles(new FilenameFilter() { @Override/*from ww w .j a va 2s . c o m*/ public boolean accept(File dir, String name) { return name.endsWith(".xml"); } }); Preconditions.checkState(preparationHeaderFiles.length > 0); SAXBuilder sax = new SAXBuilder(); TEIValidator validator = preparationSchemeURL == null ? new TEIPreparationValidator() : new FromURLValidator(preparationSchemeURL); for (File f : preparationHeaderFiles) { if (validator.validate(f)) { Document preparation = sax.build(f); // remove the pending text element preparation.getRootElement().removeChild("text", null); // append to our new root root.addContent(preparation.getRootElement().getChild("teiHeader", null).clone()); } else { System.err.println(validator.toString()); throw new LaudatioException("A preparation header ist not valid."); } } }
From source file:de.huberlin.german.korpling.laudatioteitool.SplitTEI.java
License:Apache License
private TEIValidator.Errors extractMainCorpusHeader(Document doc) throws LaudatioException, IOException, SAXException { TEIValidator validator = corpusSchemeURL == null ? new TEICorpusValidator() : new FromURLValidator(corpusSchemeURL); Element corpusHeader = doc.getRootElement().getChild("teiHeader", null); if (corpusHeader != null) { File corpusDir = new File(outputDirectory, "CorpusHeader"); if (!corpusDir.exists() && !corpusDir.mkdir()) { throw new LaudatioException( messages.getString("COULD NOT CREATE DIRECTORY") + corpusDir.getAbsolutePath()); }/*from w w w.j a va 2 s . c o m*/ // create the subtree for the global corpus header Namespace teiNS = Namespace.getNamespace("http://www.tei-c.org/ns/1.0"); Element newRootForCorpus = new Element("TEI", teiNS); newRootForCorpus.addContent(corpusHeader.clone()); Document corpusDoc = new Document(newRootForCorpus); if (corpusSchemeURL == null) { corpusDoc.addContent(0, new ProcessingInstruction("xml-model", "href=\"" + TEICorpusValidator.DEFAULT_SCHEME_URL + "\"")); } else { corpusDoc.addContent(0, new ProcessingInstruction("xml-model", "href=\"" + corpusSchemeURL + "\"")); } // we need to append an empty "text" element after the header Element text = new Element("text", teiNS); text.setText(""); newRootForCorpus.addContent(text); // we work with the copy from now corpusHeader = newRootForCorpus.getChild("teiHeader", null); Preconditions.checkNotNull(corpusHeader, messages.getString("ERROR NO CORPUS TITLE GIVEN")); Preconditions.checkState("CorpusHeader".equals(corpusHeader.getAttributeValue("type"))); Preconditions.checkNotNull(corpusHeader.getChild("fileDesc", null), messages.getString("ERROR NO CORPUS TITLE GIVEN")); Preconditions.checkNotNull(corpusHeader.getChild("fileDesc", null).getChild("titleStmt", null), messages.getString("ERROR NO CORPUS TITLE GIVEN")); String title = corpusHeader.getChild("fileDesc", null).getChild("titleStmt", null) .getChildTextNormalize("title", null); Preconditions.checkNotNull(title, messages.getString("ERROR NO CORPUS TITLE GIVEN")); // save the file with the title as file name File outputFile = new File(corpusDir, title + ".xml"); XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat()); xmlOut.output(corpusDoc, new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8")); log.info(messages.getString("WRITTEN CORPUS HEADER"), outputFile.getPath()); validator.validate(outputFile); } return validator.getErrors(); }
From source file:de.huberlin.german.korpling.laudatioteitool.SplitTEI.java
License:Apache License
private TEIValidator.Errors extractDocumentHeaders(Document doc) throws LaudatioException, IOException, SAXException { TEIValidator validator = documentSchemeURL == null ? new TEIDocumentValidator() : new FromURLValidator(documentSchemeURL); File documentDir = new File(outputDirectory, "DocumentHeader"); if (!documentDir.exists() && !documentDir.mkdir()) { throw new LaudatioException( messages.getString("COULD NOT CREATE DIRECTORY") + documentDir.getAbsolutePath()); }/*from w w w.j a v a 2 s .c o m*/ Element documentRoot = Preconditions.checkNotNull(doc.getRootElement().getChild("teiCorpus", null)); for (Element docHeader : documentRoot.getChildren("teiHeader", null)) { Preconditions.checkState("DocumentHeader".equals(docHeader.getAttributeValue("type"))); // create the subtree for the global corpus header Namespace teiNS = Namespace.getNamespace("http://www.tei-c.org/ns/1.0"); Element tei = new Element("TEI", teiNS); tei.addContent(docHeader.clone()); Document newDoc = new Document(tei); if (documentSchemeURL == null) { newDoc.addContent(0, new ProcessingInstruction("xml-model", "href=\"" + TEIDocumentValidator.DEFAULT_SCHEME_URL + "\"")); } else { newDoc.addContent(0, new ProcessingInstruction("xml-model", "href=\"" + documentSchemeURL + "\"")); } // we need to append an empty "text" element after the header Element text = new Element("text", teiNS); text.setText(""); tei.addContent(text); Element fileDesc = Preconditions .checkNotNull(tei.getChild("teiHeader", null).getChild("fileDesc", null)); String outName = UUID.randomUUID().toString(); String id = fileDesc.getAttributeValue("id", Namespace.XML_NAMESPACE); if (id != null) { outName = id; } else { Element titleStmt = Preconditions.checkNotNull(fileDesc.getChild("titleStmt", null)); String title = titleStmt.getChildText("title", null); if (title != null) { outName = title; } } File outputFile = new File(documentDir, outName + ".xml"); XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat()); xmlOut.output(newDoc, new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8")); log.info(messages.getString("WRITTEN DOCUMENT HEADER"), outputFile.getPath()); validator.validate(outputFile); } return validator.getErrors(); }
From source file:de.huberlin.german.korpling.laudatioteitool.SplitTEI.java
License:Apache License
private TEIValidator.Errors extractPreparationSteps(Document doc) throws LaudatioException, IOException, SAXException { TEIValidator validator = preparationSchemeURL == null ? new TEIPreparationValidator() : new FromURLValidator(preparationSchemeURL); Multiset<String> knownPreparationTitles = HashMultiset.create(); File documentDir = new File(outputDirectory, "PreparationHeader"); if (!documentDir.exists() && !documentDir.mkdir()) { throw new LaudatioException( messages.getString("COULD NOT CREATE DIRECTORY") + documentDir.getAbsolutePath()); }//from w w w. ja v a 2 s. c o m Preconditions.checkNotNull(doc.getRootElement().getChild("teiCorpus", null)); Element preparationRoot = Preconditions .checkNotNull(doc.getRootElement().getChild("teiCorpus", null).getChild("teiCorpus", null)); for (Element preparationHeader : preparationRoot.getChildren("teiHeader", null)) { Preconditions.checkState("PreparationHeader".equals(preparationHeader.getAttributeValue("type"))); // create the subtree for the global corpus header Namespace teiNS = Namespace.getNamespace("http://www.tei-c.org/ns/1.0"); Element tei = new Element("TEI", teiNS); tei.addContent(preparationHeader.clone()); Document newDoc = new Document(tei); if (preparationSchemeURL == null) { newDoc.addContent(0, new ProcessingInstruction("xml-model", "href=\"" + TEIPreparationValidator.DEFAULT_SCHEME_URL + "\"")); } else { newDoc.addContent(0, new ProcessingInstruction("xml-model", "href=\"" + preparationSchemeURL + "\"")); } // we need to append an empty "text" element after the header Element text = new Element("text", teiNS); text.setText(""); tei.addContent(text); Element fileDesc = Preconditions .checkNotNull(tei.getChild("teiHeader", null).getChild("fileDesc", null)); String outName = UUID.randomUUID().toString(); Element titleStmt = Preconditions.checkNotNull(fileDesc.getChild("titleStmt", null)); Element title = Preconditions.checkNotNull(titleStmt.getChild("title", null)); String corresp = title.getAttributeValue("corresp"); if (corresp != null) { if (knownPreparationTitles.contains(corresp)) { knownPreparationTitles.add(corresp); outName = corresp + "_" + knownPreparationTitles.count(corresp); log.warn(messages.getString("MORE THAN ONE PREPARATION HEADER"), corresp); } else { outName = corresp; knownPreparationTitles.add(corresp); } } File outputFile = new File(documentDir, outName + ".xml"); XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat()); xmlOut.output(newDoc, new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8")); log.info(messages.getString("WRITTEN PREPARATION HEADER"), outputFile.getPath()); validator.validate(outputFile); } return validator.getErrors(); }
From source file:de.ing_poetter.binview.BinaryFormat.java
License:Open Source License
public static BinaryFormat loadFromFile(final File f) { final SAXBuilder builder = new SAXBuilder(); Document doc; try {/*from w ww .j a v a 2s .c o m*/ doc = builder.build(f); Element root = null; root = doc.getRootElement(); if (false == ROOT_ELEMENT_NAME.equalsIgnoreCase(root.getName())) { System.err.println("Format has invalid root Element of " + root.getName()); return null; } final BinaryFormat res = new BinaryFormat(); final List<Element> vars = root.getChildren(); for (int i = 0; i < vars.size(); i++) { final Element curVar = vars.get(i); final Variable v = VariableFactory.createVariableFrom(curVar); res.addVariable(v); } return res; } catch (final JDOMException e) { e.printStackTrace(); } catch (final IOException e) { e.printStackTrace(); } return null; }