List of usage examples for org.jdom2 Document getRootElement
public Element getRootElement()
Element
for this Document
From source file:edu.unc.lib.deposit.normalize.Proquest2N3BagJob.java
License:Apache License
private void normalizePackage(File packageDir, Model model, Bag depositBag) { // Generate a uuid for the main object PID primaryPID = new PID("uuid:" + UUID.randomUUID()); Resource primaryResource;/* w ww .j a v a 2 s . c o m*/ // Identify the important files from the deposit File dataFile = null, contentFile = null, attachmentDir = null; File[] files = packageDir.listFiles(); for (File file : files) { if (file.isDirectory()) { attachmentDir = file; } else if (file.getName().endsWith(DATA_SUFFIX)) { dataFile = file; } else { contentFile = file; } } long lastModified = -1; File zipFile = new File(packageDir.getAbsolutePath() + ".zip"); try (ZipFile zip = new ZipFile(zipFile)) { ZipArchiveEntry entry = zip.getEntry(contentFile.getName()); lastModified = entry.getTime(); } catch (IOException e) { log.error("Failed to read zip file located at {}.zip", packageDir.getAbsolutePath(), e); } if (lastModified == -1) { lastModified = zipFile.lastModified(); } DateTime modified = new DateTime(lastModified, DateTimeZone.UTC); // Deserialize the data document SAXBuilder builder = new SAXBuilder(); Element dataRoot = null; Document mods = null; try { Document dataDocument = builder.build(dataFile); dataRoot = dataDocument.getRootElement(); // Transform the data into MODS and store it to its final resting place mods = extractMods(primaryPID, dataRoot, modified); } catch (TransformerException e) { failJob(e, Type.NORMALIZATION, "Failed to transform metadata to MODS"); } catch (Exception e) { failJob(e, Type.NORMALIZATION, "Unable to deserialize the metadata file"); } // Detect if there are any attachments List<?> attachmentElements = dataRoot.getChild("DISS_content").getChildren("DISS_attachment"); if (attachmentElements == null || attachmentElements.size() == 0) { // Simple object with the content as its source data primaryResource = populateSimple(model, primaryPID, contentFile); } else { String title = mods.getRootElement().getChild("titleInfo", MODS_V3_NS).getChildText("title", MODS_V3_NS); // Has attachments, so it is an aggregate primaryResource = populateAggregate(model, primaryPID, attachmentElements, attachmentDir, contentFile, title); } // Store primary resource as child of the deposit depositBag.add(primaryResource); // Add the data file as a metadata datastream of the primary object setSourceMetadata(model, primaryResource, dataFile); // Capture other metadata, like embargoes setEmbargoUntil(model, primaryResource, dataRoot); // Creation date for the content file model.add(primaryResource, cdrprop(model, dateCreated), modified.toString(), XSDDatatype.XSDdateTime); }
From source file:edu.unc.lib.deposit.normalize.VocabularyEnforcementJob.java
License:Apache License
@Override public void runJob() { Model model = getWritableModel();/*from ww w .j a va2 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:edu.unc.lib.dl.util.VocabularyHelperManager.java
License:Apache License
/** * Returns the authoritative vocabulary forms for selected fields from the given document using all applicable * helpers for the specified object. Terms are grouped by vocabulary URI. * * @param pid/*w w w. j av a2 s. c o m*/ * @param doc * @return */ public Map<String, List<List<String>>> getAuthoritativeForms(PID pid, Document doc) { return getAuthoritativeForms(pid, doc.getRootElement()); }
From source file:edu.unc.lib.dl.xml.DepartmentOntologyUtil.java
License:Apache License
/** * Parses a SKOS XML vocabulary located at filePath and populates a lookup index labels and alternative labels * referencing the authoritative version. * * @param ontologyURL/*from w ww . j a v a2 s .c o m*/ * @throws Exception */ private void parseVocabulary(byte[] content) throws Exception { departments = new HashMap<String, DepartmentConcept>(); log.debug("Parsing and building Department vocabulary from {}", getVocabularyURI()); SAXBuilder sb = new SAXBuilder(new XMLReaderSAX2Factory(false)); Document skosDoc = sb.build(new ByteArrayInputStream(content)); // Extract all of the concepts and store them to an index List<?> concepts = skosDoc.getRootElement().getChildren("Concept", SKOS_NS); Map<String, DepartmentConcept> tempDepts = new HashMap<String, DepartmentConcept>(concepts.size()); for (Object conceptObj : concepts) { DepartmentConcept dept = new DepartmentConcept((Element) conceptObj); tempDepts.put(cleanLabel(dept.getIdentifier()), dept); } // Expand out all the alternative labels into an index and resolve references for (Iterator<Entry<String, DepartmentConcept>> deptIt = tempDepts.entrySet().iterator(); deptIt .hasNext();) { Entry<String, DepartmentConcept> deptEntry = deptIt.next(); DepartmentConcept dept = deptEntry.getValue(); // Check if this concept should be ignored in favor of a preferred concept if (dept.prefLabel != null) { if (departments.containsKey(dept.prefLabel)) { // The preferred concept has already been indexed, grab extra labels from this concept and reindex pref DepartmentConcept prefDept = departments.get(dept.prefLabel); prefDept.merge(dept); addLabels(prefDept); } else { // Since the preferred concept isn't indexed yet, just need to merge labels into it DepartmentConcept prefDept = tempDepts.get(dept.prefLabel); if (prefDept == null) { log.warn("Preferred label {} referencing a concept which is not present", dept.prefLabel); } else { prefDept.merge(dept); } } continue; } String identifier = cleanLabel(dept.identifier); if (departments.containsKey(identifier) && dept.identifier.equals(departments.get(identifier).identifier)) { log.error("Illegal state, multiple concepts share the identifier {}, ignoring duplicate", identifier); } else { departments.put(identifier, dept); } addLabels(dept); } }
From source file:edu.utep.cs.jasg.Frontend.java
License:Open Source License
/** Parse build XML file. Returns a String array with the following content: * [0] = target module name/*from w w w . j a v a 2 s. com*/ * [1] = target module main scanner specification file * [2] = target module main parser specification file * */ public String[] parseBuildFile(String filePath) { String[] result = { "", "", "" }; if (Files.exists(Paths.get(filePath))) { try { SAXBuilder builder = new SAXBuilder(); Document doc = (Document) builder.build(filePath); Element root = doc.getRootElement(); if (root != null) { List<Element> propertyList = root.getChildren("property"); Iterator<Element> propertiesIterator = propertyList.iterator(); //Get module name String moduleName = root.getAttributeValue("name"); if (moduleName != null) result[0] = moduleName; //Get specification name properties while (propertiesIterator.hasNext()) { Element property = propertiesIterator.next(); String propertyName = property.getAttributeValue("name"); String propertyValue = property.getAttributeValue("value"); if (propertyName != null) { //Get scanner name if (propertyName.equals("scannerName")) { if (propertyValue != null) result[1] = propertyValue; } //Get scanner name else if (propertyName.equals("parserName")) { if (propertyValue != null) result[2] = propertyValue; } } } } } catch (IOException io) { System.out.println(io.getMessage()); } catch (JDOMException jdomex) { System.out.println(jdomex.getMessage()); } } return result; }
From source file:edu.utep.cs.jasg.specificationGenerator.XMLParser.java
License:Open Source License
/** Parser XML file. */ public void parse(String filePath) { if (Files.exists(Paths.get(filePath)) && DOMValidateDTD.validateXML(filePath)) { try {//from w ww.ja va2 s .c o m SAXBuilder builder = new SAXBuilder(); Document doc = (Document) builder.build(filePath); Element root = doc.getRootElement(); nameSpace = root.getChild("nameSpace").getText(); //TODO: should I replace all files? Like compiling new files //check existing name spaces if (!Files.exists(Paths.get(workspace + File.separator + nameSpace))) { fileFactory.createDirectory(nameSpace); } specificationGenerator = new SpecificationGenerator(fileFactory, nameSpace); //get root element declarations Element parserElement = root.getChild("parser"); Element scannerElement = root.getChild("scanner"); Element ASTNodeElement = root.getChild("AST"); Element ASTBehaviorElement = root.getChild("ASTBehavior"); //parse root elements (document specifications) if (parserElement != null) parseRootElement("parser", parserElement); if (scannerElement != null) parseRootElement("scanner", scannerElement); if (ASTNodeElement != null) parseRootElement("AST", ASTNodeElement); if (ASTBehaviorElement != null) parseRootElement("ASTBehavior", ASTBehaviorElement); } catch (IOException io) { System.out.println(io.getMessage()); } catch (JDOMException jdomex) { System.out.println(jdomex.getMessage()); } } }
From source file:edu.wisc.ssec.adapter.NetCDFFile.java
License:Open Source License
public static NetCDFFile makeUnion(String filename, String other) throws Exception { Object obj = new Object(); URL url = obj.getClass().getResource("/edu/wisc/ssec/mcidasv/data/hydra/resources/union.ncml"); SAXBuilder builder = new SAXBuilder(false); Document doc = null; try {/*from w ww .j a va2 s .c o m*/ doc = builder.build(url); } catch (Exception e) { e.printStackTrace(); } Element root = doc.getRootElement(); List list = root.getChildren(); list = ((Element) list.get(1)).getChildren(); org.jdom2.Attribute attr1 = (org.jdom2.Attribute) (((Element) list.get(0)).getAttributes()).get(0); attr1.setValue(filename); org.jdom2.Attribute attr2 = (org.jdom2.Attribute) (((Element) list.get(1)).getAttributes()).get(0); attr2.setValue(other); XMLOutputter xmlOut = new XMLOutputter(); String newStr = xmlOut.outputString(doc); ByteArrayInputStream is = new ByteArrayInputStream(newStr.getBytes()); return new NetCDFFile(is); }
From source file:edu.wisc.ssec.mcidasv.chooser.TDSRadarChooser.java
License:Open Source License
/** * Get the radar collections for the given server URL * * @param radarServerURL server URL//ww w. j a va 2 s . c o m * * @return a map of the collection names to URL */ private List getRadarCollections(String radarServerURL) { SAXBuilder builder; Document doc = null; XMLEntityResolver jaxp = new XMLEntityResolver(true); builder = jaxp.getSAXBuilder(); List collections = new ArrayList(); try { doc = builder.build(radarServerURL); } catch (JDOMException e) { userMessage("Invalid catalog"); } catch (IOException e) { userMessage("Unable to open catalog"); } org.jdom2.Element rootElem = doc.getRootElement(); org.jdom2.Element serviceElem = readElements(rootElem, "service"); String uriBase = serviceElem.getAttributeValue("base"); org.jdom2.Element dsElem = readElements(rootElem, "dataset"); String naming = "catalogRef"; Namespace nss = rootElem.getNamespace("xlink"); List children = dsElem.getChildren(); for (int j = 0; j < children.size(); j++) { org.jdom2.Element child = (org.jdom2.Element) children.get(j); String childName = child.getName(); if (childName.equals(naming)) { //String id = child.getAttributeValue("ID"); String desc = child.getAttributeValue("title", nss); String urlpath = child.getAttributeValue("href", nss); String[] c = radarServerURL.split(uriBase); //.replaceFirst("catalog.xml", ""); String ul = c[0] + uriBase + urlpath; TwoFacedObject twoObj = new TwoFacedObject(desc, ul); collections.add(twoObj); //collections.put(desc, ul); } } return collections; }
From source file:edu.wisc.ssec.mcidasv.data.hydra.NetCDFFile.java
License:Open Source License
public static NetCDFFile makeUnion(String filename, String other) throws Exception { Object obj = new Object(); URL url = obj.getClass().getResource("/edu/wisc/ssec/mcidasv/data/hydra/resources/union.ncml"); SAXBuilder builder = new SAXBuilder(false); Document doc = null; try {//w ww . ja va2 s .c o m doc = builder.build(url); } catch (Exception e) { e.printStackTrace(); } Element root = doc.getRootElement(); List list = root.getChildren(); list = ((Element) list.get(1)).getChildren(); org.jdom2.Attribute attr1 = (((Element) list.get(0)).getAttributes()).get(0); attr1.setValue(filename); org.jdom2.Attribute attr2 = (((Element) list.get(1)).getAttributes()).get(0); attr2.setValue(other); XMLOutputter xmlOut = new XMLOutputter(); String newStr = xmlOut.outputString(doc); logger.trace("union string:\n{}", newStr); ByteArrayInputStream is = new ByteArrayInputStream(newStr.getBytes()); return new NetCDFFile(is); }
From source file:egovframework.rte.fdl.xml.AbstractXMLUtility.java
License:Apache License
/** * Element // ww w .j a v a 2s . com * * @param doc - Document ? * @param addNDName - ? Element * @param list - Element * @param path - ? XML * @throws TransformerException * @throws TransformerConfigurationException * @throws FileNotFoundException */ public void addElement(Document doc, String addNDName, List<?> list, String path) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException { StreamResult sTResult = null; FileOutputStream fos = null; Element root = doc.getRootElement(); addNode(root, addNDName, list); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer serializer = transformerFactory.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "euc-kr"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); if (path != null) { fos = new FileOutputStream(path); sTResult = new StreamResult(fos); serializer.transform(new JDOMSource(doc), sTResult); fos.close(); } else { fos = new FileOutputStream(savedPath + "addElement.xml"); sTResult = new StreamResult(fos); serializer.transform(new JDOMSource(doc), sTResult); fos.close(); } }