List of usage examples for org.dom4j Node getParent
Element getParent();
getParent
returns the parent Element
if this node supports the parent relationship or null if it is the root element or does not support the parent relationship.
From source file:edu.scripps.fl.pubchem.web.pug.PowerUserGateway.java
License:Apache License
private String makeRequest() throws Exception { InputStream is = fetchResponse(getRequest()); Document doc = getDocument(is); setResponse(doc);/*w w w.j a va 2s. com*/ Node node = doc.selectSingleNode("//PCT-Status[@value='server-error']"); if (node != null) { String msg = node.getParent().getParent().getText(); throw new Exception(String.format("PUG Error: %s", msg)); } node = doc.selectSingleNode("//*/PCT-Waiting_reqid"); String requestId = null; if (node != null) { requestId = node.getText(); log.debug("PCT-Waiting_reqid: " + requestId); } return requestId; }
From source file:edu.scripps.fl.pubchem.web.session.PCWebSession.java
License:Apache License
/** * // w w w .j a v a 2s. co m * Returns counts of active, all, inactive, inconclusive and probe * substances in the aid. * * @param aid * @return OutComeCount * @throws Exception */ public PCOutcomeCounts getSubstanceOutcomeCounts(int aid) throws Exception { PCOutcomeCounts oc = null; Document doc = getDocument("http://" + SITE + "/assay/assay.cgi?aid=" + aid); Node node = doc.selectSingleNode("//div[@id='uptsub']"); // Node node = doc.selectSingleNode("//b[. = 'Links:']"); //Node node = doc.selectSingleNode("//b[. = 'Substances: ']"); if (node != null) { node = node.getParent(); AppendingVisitorSupport visitor = new AppendingVisitorSupport(); node.accept(visitor); String text = visitor.getText(); Pattern sectionPattern = Pattern.compile("Tested Substances(.+)", Pattern.DOTALL | Pattern.MULTILINE); Matcher matcher = sectionPattern.matcher(text); Boolean found = matcher.find(); if (found) { text = matcher.group(1); Pattern countPattern; if (text.contains("(")) countPattern = Pattern.compile("([\\w]+)\\((\\d+)\\)"); else countPattern = Pattern.compile("([\\w]+):\\s+(\\d+)"); matcher = countPattern.matcher(text); oc = new PCOutcomeCounts(); while (matcher.find()) { String outcome = matcher.group(1); int count = Integer.parseInt(matcher.group(2)); if ("All".equalsIgnoreCase(outcome)) oc.all = count; else if (outcome.contains("All")) oc.all = count; else if ("Active".equalsIgnoreCase(outcome)) oc.active = count; else if ("Inactive".equalsIgnoreCase(outcome)) oc.inactive = count; else if ("Inconclusive".equalsIgnoreCase(outcome)) oc.inconclusive = count; else if ("Probe".equalsIgnoreCase(outcome)) oc.probe = count; } } } return oc; }
From source file:edu.ucsd.library.xdre.imports.RDFDAMS4ImportHandler.java
/** * Procedure to populate the RDF metadata and ingest the files *///from w w w.j a v a 2s . c o m public boolean execute() throws Exception { if (filesPaths != null) { File file = null; // List the source files for (int i = 0; i < filesPaths.length; i++) { file = new File(filesPaths[i]); if (file.exists()) { listFile(filesMap, file); } } } String message = ""; Document doc = null; DamsURI damsURI = null; String oid = null; int fLen = rdfFiles.length; String currFile = null; SAXReader saxReader = new SAXReader(); for (int i = 0; i < fLen && !interrupted; i++) { currFile = rdfFiles[i].getName(); setStatus( "Processing external import for file " + currFile + " (" + (i + 1) + " of " + fLen + ") ... "); try { doc = saxReader.read(rdfFiles[i]); List<Node> nodes = doc.selectNodes("//@rdf:about"); for (int j = 0; j < nodes.size(); j++) { Node nUri = nodes.get(j); String iUri = nUri.getStringValue(); Node parentNode = nUri.getParent(); String nName = parentNode.getName(); if (iUri.endsWith("/COL") || !(iUri.startsWith("http") && iUri.indexOf("/ark:/") > 0)) { // Assign ARK if (nName.endsWith("Object") || nName.endsWith("Component") || nName.endsWith("File") || (((Element) parentNode).isRootElement() || (parentNode.getParent().isRootElement() && parentNode.getParent().getName().equals("RDF")))) { String objId = iUri; if (nName.endsWith("Component") || nName.endsWith("File")) { damsURI = DamsURI.toParts(iUri, null); objId = damsURI.getObject(); } String srcObjKey = objId + "::" + rdfFiles[i].getAbsolutePath(); oid = idsMap.get(srcObjKey); // Assign new ARK if (oid == null) { oid = getNewId(); idsMap.put(srcObjKey, oid); } if (nName.endsWith("Object")) { objId = oid; objRecords.put(objId, currFile); } else if (nName.endsWith("Component") || nName.endsWith("File")) { damsURI.setObject(oid); // XXX // Assign cid and fid for Component and FIle if required objId = damsURI.toString(); } else objId = oid; nUri.setText(objId); updateReference(doc, iUri, objId); } else { String field = null; Node tNode = null; String xPath = null; Map<String, String> props = new TreeMap<String, String>(); String elemXPath = parentNode.getPath(); if (nName.endsWith("Collection") || nName.endsWith("CollectionPart")) { // Retrieve the Collection record field = "title_tesim"; xPath = "dams:title/mads:Title/mads:authoritativeLabel"; tNode = parentNode.selectSingleNode(xPath); if (tNode == null) { // Loop through to locate the rdfs:label if not selected by xPath. Node n = parentNode.selectSingleNode("dams:title"); for (Iterator<Element> it = ((Element) n).elementIterator(); it.hasNext();) { Element elem = it.next(); if (elem.getNamespacePrefix().equals("mads") && elem.getName().equals("Title")) tNode = elem.selectSingleNode("mads:authoritativeLabel"); } } } /* else if (nName.endsWith("Language") || nName.endsWith("Authority") || nName.endsWith("Subject") || nName.endsWith("Name") || nName.endsWith("Topic") || nName.endsWith("GenreForm") || nName.endsWith("Temporal") || nName.endsWith("Geographic")){ // Subject, Authority records use mads:authoritativeLabel field = "name_tesim"; xPath = "mads:authoritativeLabel"; tNode = parentNode.selectSingleNode(xPath); } */else if (nName.endsWith(COPYRIGHT)) { // Copyright records use dams:copyrightStatus, plus other properties in the next step. field = "status_tesim"; xPath = "dams:copyrightStatus"; tNode = parentNode.selectSingleNode(xPath); props = copyrightProperties(parentNode); } else if (nName.endsWith(LICENSE)) { // License records use dams:LicenseNote, plus other properties in the next step. field = "note_tesim"; xPath = "dams:licenseNote"; tNode = parentNode.selectSingleNode(xPath); props = licenseProperties(parentNode); } else if (nName.endsWith(OTHERRIGHTS)) { // Copyright records use dams:copyrightStatus, plus other properties in the next step. field = "otherRightsBasis_tesim"; xPath = "dams:otherRightsBasis"; tNode = parentNode.selectSingleNode(xPath); props = licenseProperties(parentNode); } else if (nName.endsWith(RELATEDRESOURCE)) { // RelatedResource records use dams:description, plus other properties in the next step. field = "description_tesim"; xPath = "dams:description"; tNode = parentNode.selectSingleNode(xPath); props = relatedResourceProperties(parentNode); } else if (nName.endsWith(SOURCECAPTURE)) { // SourceCapture records use dams:sourceType, plus other properties in the next step. field = "sourceType_tesim"; xPath = "dams:sourceType"; tNode = parentNode.selectSingleNode(xPath); props = sourceCaptureProperties(parentNode); } else if (elemXPath.indexOf("mads", elemXPath.lastIndexOf('/') + 1) >= 0) { // MADSScheme and Language if (nName.endsWith(MADSSCHEME) || nName.equals(LANGUAGE)) { field = "code_tesim"; xPath = "mads:code"; tNode = parentNode.selectSingleNode(xPath); if (tNode == null) { field = "name_tesim"; xPath = "rdfs:label"; tNode = parentNode.selectSingleNode(xPath); if (tNode == null) { // Loop through to locate the rdfs:label if not selected by xPath. for (Iterator<Element> it = ((Element) parentNode).elementIterator(); it .hasNext();) { Element elem = it.next(); if (elem.getNamespacePrefix().equals("rdfs") && elem.getName().equals("label")) tNode = elem; } } } } else { // Subject, Authority records use mads:authoritativeLabel field = "name_tesim"; xPath = "mads:authoritativeLabel"; tNode = parentNode.selectSingleNode(xPath); if (tNode == null) { // Try to use the mads:code for mapping when mads:authoritativeLabel is not available field = "code_tesim"; xPath = "mads:code"; tNode = parentNode.selectSingleNode(xPath); } // Mapping for mads:isMemberOfMADSScheme String madsScheme = null; Node madsSchemeNode = parentNode.selectSingleNode("mads:isMemberOfMADSScheme"); if (madsSchemeNode != null) { Node msValueNode = madsSchemeNode.selectSingleNode("@rdf:resource"); if (msValueNode != null) { madsScheme = madsSchemeNode.getStringValue(); props.put("scheme_tesim", madsScheme); } else if ((madsSchemeNode = madsSchemeNode .selectSingleNode("mads:MADSScheme")) != null && madsSchemeNode.hasContent()) { if ((msValueNode = madsSchemeNode .selectSingleNode("mads:code")) != null) { madsScheme = msValueNode.getText(); props.put("scheme_code_tesim", madsScheme); } else if ((msValueNode = madsSchemeNode .selectSingleNode("rdfs:label")) != null) { madsScheme = msValueNode.getText(); props.put("scheme_name_tesim", madsScheme); } } else { props.put("scheme_tesim", ""); } } else { props.put("scheme_tesim", null); } } } else { // XXX Other Rights records like Statute, License, Other Rights etc. field = "value_tesim"; xPath = "rdf:value"; tNode = parentNode.selectSingleNode(xPath); field = "code_tesim"; if (tNode == null) { xPath = "dams:code"; tNode = parentNode.selectSingleNode(xPath); } } if (tNode == null) { throw new Exception("Element " + xPath + " is missing from the " + nName + " record " + iUri + " in file " + currFile + "."); } updateDocument(doc, parentNode, field, tNode.getText(), props); } } else if (nName.endsWith("Object")) { objRecords.put(iUri, currFile); } } String dams4Rdf = doc.asXML(); logData("dams4_" + rdfFiles[i].getName(), dams4Rdf); // Ingest the records String subjectId = null; DamsURI objURI = null; List<DamsURI> objURIs = null; RDFStore graph = null; rdfStore = new RDFStore(); Model rdf = rdfStore.loadRDFXML(dams4Rdf); initHandler(); Model iRdf = null; int jLen = items.size(); for (int j = 0; j < jLen && !interrupted; j++) { graph = new RDFStore(); recordsCount++; // Add subject subjectId = items.get(j); try { setStatus("Processing metadata for record " + subjectId + " (" + (j + 1) + " of " + jLen + ") in file " + currFile + " ... "); boolean succeeded = false; objURIs = objects.get(subjectId); for (int k = 0; k < objURIs.size(); k++) { objURI = objURIs.get(k); iRdf = rdfStore.querySubject(objURI.toString()); graph.merge(iRdf); } // Update object //log.info(j + " ingesting record " + subjectId + ":\n" + graph.export(RDFStore.RDFXML_ABBREV_FORMAT) + "\n\n"); succeeded = damsClient.updateObject(subjectId, graph.export(RDFStore.RDFXML_ABBREV_FORMAT), Constants.IMPORT_MODE_ADD); if (!succeeded) { if (metadataFailed.indexOf(currFile) < 0) failedCount++; metadataFailed.append(subjectId + " (" + currFile + "), \n"); message = "Metadata import for record " + subjectId + " failed (" + (j + 1) + " of " + jLen + ") in file " + currFile + "."; setStatus(message); logError(message + "\n Error RDF: \n" + graph.export(RDFStore.RDFXML_ABBREV_FORMAT)); } else { recordsIngested.add(subjectId); message = "Metadata import for record " + subjectId + " succeeded (" + (j + 1) + " of " + jLen + ") in file " + currFile + ". "; setStatus(message); logMessage(message); log.info(message); // Update SOLR fre records ingested. updateSOLR(subjectId); } } catch (Exception e) { e.printStackTrace(); if (metadataFailed.indexOf(currFile) < 0) failedCount++; metadataFailed.append(subjectId + " (" + currFile + "), \n"); message = "Metadata import failed: " + e.getMessage(); setStatus(message + " (" + (j + 1) + " of " + jLen + ") in file " + currFile + "."); logError(message); } try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); interrupted = true; failedCount++; metadataFailed.append(subjectId + " (" + currFile + ") \n"); message = "Metadata import interrupted for subject " + subjectId + ". \n Error: " + e.getMessage() + "\n"; setStatus("Canceled"); clearSession(); logError(message); } } // Ingest the source file if (importOption.equalsIgnoreCase("metadataAndFiles")) { uploadFiles(rdf, currFile); } } catch (Exception e) { e.printStackTrace(); failedCount++; message = "Import failed for " + currFile + ": " + e.getMessage(); setStatus(message + " (" + (i + 1) + " of " + fLen + ")."); logError(message); } finally { // Update SOLR for files uploaded int iLen = objWithFiles.size(); for (int j = 0; j < iLen && !interrupted; j++) { updateSOLR(objWithFiles.get(j)); } } setProgressPercentage(((i + 1) * 100) / fLen); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); interrupted = true; failedCount++; message = "Import interrupted for oject in " + currFile + ". \n Error: " + e.getMessage() + "\n"; setStatus("Canceled"); clearSession(); logError(message); } } return exeResult; }
From source file:edu.ucsd.library.xdre.imports.RDFDAMS4ImportHandler.java
/** * Update record for resource linking// ww w. j ava 2 s. c o m * @param url * @param node */ public void toResourceLinking(String url, Node record) { Element pNode = record.getParent(); if (pNode.getName().endsWith("List") && !record.getName().toLowerCase().endsWith(pNode.getName().toLowerCase())) { //List elements record.setName("rdf:Description"); ((Element) record).clearContent(); ((Element) record).selectSingleNode("@rdf:about").setText(toDamsUrl(url)); } else { pNode.addAttribute("rdf:resource", toDamsUrl(url)); record.detach(); } }
From source file:edu.ucsd.library.xdre.imports.RDFDAMS4ImportTsHandler.java
/** * Procedure to populate the RDF metadata and ingest the files *///w w w .jav a 2 s . c om @Override public boolean execute() throws Exception { if (filesPaths != null) { File file = null; // List the source files for (int i = 0; i < filesPaths.length; i++) { file = new File(filesPaths[i]); if (file.exists()) { listFile(filesMap, file); } } } String message = ""; Document doc = null; DamsURI damsURI = null; List<String> recordsToReplace = null; String oid = null; int fLen = rdfFiles.length; String currFile = null; SAXReader saxReader = new SAXReader(); for (int i = 0; i < fLen && !interrupted; i++) { if (i == 0) logMessage("Object Import status:\n[Object title] - [URI] - [Status] - [Timestamp]"); recordsToReplace = new ArrayList<>(); currFile = rdfFiles[i].getName(); preprocessedTimestamp = damsDateFormat.format(rdfFiles[i].lastModified()); setStatus( "Processing external import for file " + currFile + " (" + (i + 1) + " of " + fLen + ") ... "); try { doc = saxReader.read(rdfFiles[i]); List<Node> nodes = doc.selectNodes("//@rdf:about"); for (int j = 0; j < nodes.size(); j++) { Node nUri = nodes.get(j); String iUri = nUri.getStringValue(); Node parentNode = nUri.getParent(); String nName = parentNode.getName(); if (iUri.endsWith("/COL") || !(iUri.startsWith("http") && iUri.indexOf("/ark:/") > 0)) { // Assign ARK if (nName.endsWith("Object") || nName.endsWith("Component") || nName.endsWith("File") || (((Element) parentNode).isRootElement() || (parentNode.getParent().isRootElement() && parentNode.getParent().getName().equals("RDF")))) { String objId = iUri; if (nName.endsWith("Component") || nName.endsWith("File")) { damsURI = DamsURI.toParts(iUri, null); objId = damsURI.getObject(); } String srcObjKey = objId + "::" + rdfFiles[i].getAbsolutePath(); oid = idsMap.get(srcObjKey); // Assign new ARK if (oid == null) { oid = getNewId(); idsMap.put(srcObjKey, oid); } if (nName.endsWith("Object")) { objId = oid; objRecords.put(objId, currFile); } else if (nName.endsWith("Component") || nName.endsWith("File")) { damsURI.setObject(oid); // XXX // Assign cid and fid for Component and FIle if required objId = damsURI.toString(); } else objId = oid; nUri.setText(objId); updateReference(doc, iUri, objId); } else { String field = null; Node tNode = null; String xPath = null; Map<String, String> props = new TreeMap<String, String>(); String elemXPath = parentNode.getPath(); if (nName.endsWith("Collection") || nName.endsWith("CollectionPart")) { // Retrieve the Collection record field = "dams:title/mads:authoritativeLabel"; xPath = "dams:title/mads:Title/mads:authoritativeLabel"; tNode = parentNode.selectSingleNode(xPath); if (tNode == null) { // Loop through to locate the rdfs:label if not selected by xPath. Node n = parentNode.selectSingleNode("dams:title"); for (Iterator<Element> it = ((Element) n).elementIterator(); it.hasNext();) { Element elem = it.next(); if (elem.getNamespacePrefix().equals("mads") && elem.getName().equals("Title")) tNode = elem.selectSingleNode("mads:authoritativeLabel"); } } } /* else if (nName.endsWith("Language") || nName.endsWith("Authority") || nName.endsWith("Subject") || nName.endsWith("Name") || nName.endsWith("Topic") || nName.endsWith("GenreForm") || nName.endsWith("Temporal") || nName.endsWith("Geographic")){ // Subject, Authority records use mads:authoritativeLabel field = "name_tesim"; xPath = "mads:authoritativeLabel"; tNode = parentNode.selectSingleNode(xPath); } */else if (nName.endsWith(COPYRIGHT)) { // Copyright records use dams:copyrightStatus, plus other properties in the next step. field = "dams:copyrightStatus"; xPath = "dams:copyrightStatus"; tNode = parentNode.selectSingleNode(xPath); props = copyrightProperties(parentNode); } else if (nName.endsWith(LICENSE)) { // License records use dams:LicenseNote, plus other properties in the next step. field = "dams:licenseNote"; xPath = "dams:licenseNote"; tNode = parentNode.selectSingleNode(xPath); props = licenseProperties(parentNode); } else if (nName.endsWith(OTHERRIGHTS)) { // Copyright records use dams:copyrightStatus, plus other properties in the next step. field = "dams:otherRightsBasis"; xPath = "dams:otherRightsBasis"; tNode = parentNode.selectSingleNode(xPath); props = otherRightsProperties(parentNode); } else if (nName.endsWith(RELATEDRESOURCE)) { // RelatedResource records use dams:description, plus other properties in the next step. field = "dams:description"; xPath = "dams:description"; tNode = parentNode.selectSingleNode(xPath); props = relatedResourceProperties(parentNode); } else if (nName.endsWith(SOURCECAPTURE)) { // SourceCapture records use dams:sourceType, plus other properties in the next step. field = "dams:sourceType"; xPath = "dams:sourceType"; tNode = parentNode.selectSingleNode(xPath); props = sourceCaptureProperties(parentNode); } else if (nName.endsWith(NOTE)) { // Note records use rdf:value, dams:type, dams:displayLabel. field = "rdf:value"; xPath = "rdf:value"; tNode = parentNode.selectSingleNode(xPath); props = noteProperties(parentNode); } else if (nName.endsWith(PERMISSION) || nName.equals(RESTRICTION)) { field = "dams:type"; xPath = "dams:type"; tNode = parentNode.selectSingleNode(xPath); props = dateProperties(parentNode); } else if (elemXPath.indexOf("mads", elemXPath.lastIndexOf('/') + 1) >= 0) { // MADSScheme and Language if (nName.endsWith(MADSSCHEME)) { field = "mads:code"; xPath = "mads:code"; tNode = parentNode.selectSingleNode(xPath); if (tNode == null) { field = "rdfs:label"; xPath = "rdfs:label"; tNode = parentNode.selectSingleNode("*[name()='" + xPath + "']"); } } else if (nName.endsWith(LANGUAGE)) { field = "mads:code"; xPath = "mads:code"; tNode = parentNode.selectSingleNode(xPath); if (tNode == null) { field = "mads:authoritativeLabel"; xPath = "mads:authoritativeLabel"; tNode = parentNode.selectSingleNode(xPath); } } else { // Subject, Authority records use mads:authoritativeLabel field = "mads:authoritativeLabel"; xPath = "mads:authoritativeLabel"; tNode = parentNode.selectSingleNode(xPath); if (tNode == null) { // Try to use the mads:code for mapping when mads:authoritativeLabel is not available field = "mads:code"; xPath = "mads:code"; tNode = parentNode.selectSingleNode(xPath); } else { Node diplayLabelNode = parentNode .selectSingleNode("*[name()='dams:displayLabel']"); props.put("dams:displayLabel", encodeLiteralValue(diplayLabelNode)); } // Mapping for mads:isMemberOfMADSScheme String madsScheme = null; Node madsSchemeNode = parentNode.selectSingleNode("mads:isMemberOfMADSScheme"); if (madsSchemeNode != null) { Node msValueNode = madsSchemeNode.selectSingleNode("@rdf:resource"); if (msValueNode != null) { madsScheme = madsSchemeNode.getStringValue(); props.put("mads:isMemberOfMADSScheme", "<" + madsScheme + ">"); } else if ((madsSchemeNode = madsSchemeNode .selectSingleNode("mads:MADSScheme")) != null && madsSchemeNode.hasContent()) { if ((msValueNode = madsSchemeNode .selectSingleNode("mads:code")) != null) { madsScheme = encodeLiteralValue(msValueNode); props.put("mads:isMemberOfMADSScheme/mads:code", madsScheme); } else if ((msValueNode = madsSchemeNode .selectSingleNode("rdfs:label")) != null) { madsScheme = encodeLiteralValue(msValueNode); props.put("mads:isMemberOfMADSScheme/rdfs:label", madsScheme); } } else { props.put("mads:isMemberOfMADSScheme/rdfs:label", "\"\""); } } else { props.put("mads:isMemberOfMADSScheme/rdfs:label", null); } } } else { // XXX Other Rights records like Statute, License, Other Rights etc. field = "rdf:value"; xPath = "rdf:value"; tNode = parentNode.selectSingleNode(xPath); field = "dams:code"; if (tNode == null) { xPath = "dams:code"; tNode = parentNode.selectSingleNode(xPath); } } if (tNode == null && !field.equals("dams:licenseNote")) { throw new Exception("Element " + xPath + " is missing from the " + nName + " record " + iUri + " in file " + currFile + "."); } updateDocument(doc, parentNode, field, encodeLiteralValue(tNode), props); } } else { if (nName.endsWith("Object")) objRecords.put(iUri, currFile); if (replace && !(nName.endsWith("Component") || nName.endsWith("File"))) recordsToReplace.add(iUri); } } String dams4Rdf = doc.asXML(); logData("dams4_" + rdfFiles[i].getName(), dams4Rdf); // Ingest the records String subjectId = null; DamsURI objURI = null; List<DamsURI> objURIs = null; RDFStore graph = null; rdfStore = new RDFStore(); rdfStore.loadRDFXML(dams4Rdf); initHandler(); Model iRdf = null; items = sortRecords(items); int jLen = items.size(); //System.out.println(currFile + " records found: " + jLen); for (int j = 0; j < jLen && !interrupted; j++) { processIndex = 0; status = new boolean[processNames.length]; messages = new StringBuilder[processNames.length]; for (int k = 0; k < messages.length; k++) { messages[k] = new StringBuilder(); } Model objModel = null; graph = new RDFStore(); recordsCount++; // Add subject subjectId = items.get(j); try { setStatus("Processing metadata for record " + subjectId + " (" + (j + 1) + " of " + jLen + ") in file " + currFile + " ... "); boolean succeeded = false; objURIs = objects.get(subjectId); for (int k = 0; k < objURIs.size(); k++) { objURI = objURIs.get(k); iRdf = rdfStore.querySubject(objURI.toString()); objModel = graph.merge(iRdf); } // Update object //log.info(j + " ingesting record " + subjectId + ":\n" + graph.export(RDFStore.RDFXML_ABBREV_FORMAT) + "\n\n"); String importMode = Constants.IMPORT_MODE_ADD; if (replace && recordsToReplace.indexOf(subjectId) >= 0) importMode = Constants.IMPORT_MODE_ALL; succeeded = damsClient.updateObject(subjectId, graph.export(RDFStore.RDFXML_ABBREV_FORMAT), importMode); // Logging for Object RDF/XML validation status[processIndex] = succeeded; messages[processIndex].append(damsDateFormat.format(new Date())); if (!succeeded) { if (metadataFailed.indexOf(currFile) < 0) failedCount++; metadataFailed.append(subjectId + " (" + currFile + "), \n"); message = "Metadata import for record " + subjectId + " failed (" + (j + 1) + " of " + jLen + ") in file " + currFile + "."; setStatus(message); log.error(message + "\n Error RDF: \n" + graph.export(RDFStore.RDFXML_ABBREV_FORMAT)); } else { recordsIngested.add(subjectId); message = "Metadata import for record " + subjectId + " succeeded (" + (j + 1) + " of " + jLen + ") in file " + currFile + ". "; setStatus(message); log.info(message); processIndex++; status[processIndex] = succeeded; messages[processIndex].append(damsDateFormat.format(new Date())); // Ingest the source file only if metadata ingested successfully if (status[processIndex] && importOption.equalsIgnoreCase("metadataAndFiles")) { uploadFiles(objModel, currFile, subjectId); } } } catch (Exception e) { e.printStackTrace(); if (metadataFailed.indexOf(currFile) < 0) failedCount++; metadataFailed.append(subjectId + " (" + currFile + "), \n"); message = "Metadata import failed: " + e.getMessage(); setStatus(message + " (" + (j + 1) + " of " + jLen + ") in file " + currFile + "."); log.error(message); String error = e.getMessage(); if (error.indexOf("Invalid RDF input") >= 0) { messages[processIndex].append(error); } else { status[processIndex] = true; messages[processIndex].append(damsDateFormat.format(new Date())); processIndex++; messages[processIndex].append(error); } } finally { int solrRequestIndex = processNames.length - 1; try { // Update SOLR for the record. status[solrRequestIndex] = updateSOLR(subjectId); messages[solrRequestIndex].append(damsDateFormat.format(new Date())); log.info("SOLR update requested for " + subjectId + ": " + damsClient.getRequestURL() + " " + status[solrRequestIndex]); } catch (Exception e) { e.printStackTrace(); exeResult = false; log.error("SOLR Index failed " + subjectId + ": " + e.getMessage()); messages[processNames.length - 1].append(e.getMessage()); } if (exeResult) exeResult = status[processIndex]; String resultMessage = "http://" + Constants.CLUSTER_HOST_NAME + ".ucsd.edu/dc/object/" + subjectId.substring(subjectId.lastIndexOf("/") + 1) + " - " + (status[processIndex] && status[solrRequestIndex] ? "successful" : "failed") + " - " + damsDateFormat.format(new Date()); if (objRecords.containsKey(subjectId)) { String title = getTitle(objModel, subjectId); if (StringUtils.isBlank(title)) title = "[Unknown Title]"; logMessage("\n" + title + " - " + resultMessage); if (!status[processIndex] || !status[solrRequestIndex]) { // Logging for pre-procesing - succeeded. logMessage("* Pre-processing - successful - " + preprocessedTimestamp); for (int k = 0; k <= processIndex; k++) { if (status[k] || !status[k] && status[k - 1]) { logMessage("* " + processNames[k] + " - " + (status[k] ? "successful" : "failed") + " - " + messages[k].toString()); } } // SOLR index request logging if (!status[solrRequestIndex]) logMessage("* " + processNames[solrRequestIndex] + " - " + (status[solrRequestIndex] ? "successful" : "failed") + " - " + messages[solrRequestIndex].toString()); } } else { ingestMessages.append("\n" + resultMessage); if (!status[processIndex]) { for (int k = 0; k + 1 < processIndex; k++) { if (status[k] || !status[k] && status[k - 1]) { logMessage("* " + processNames[k] + " - " + (status[k] ? "successful" : "failed") + " - " + messages[k].toString()); } } } } } try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); interrupted = true; exeResult = false; failedCount++; metadataFailed.append(subjectId + " (" + currFile + ") \n"); message = "Metadata import interrupted for subject " + subjectId + ". \n Error: " + e.getMessage() + "\n"; setStatus("Canceled"); clearSession(); log.error(message); logMessage("Client Cancled - " + damsDateFormat.format(new Date())); } } } catch (Exception e) { e.printStackTrace(); exeResult = false; failedCount++; message = "Import failed for " + currFile + ": " + e.getMessage(); setStatus(message + " (" + (i + 1) + " of " + fLen + ")."); log.error(message); } setProgressPercentage(((i + 1) * 100) / fLen); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); exeResult = false; interrupted = true; failedCount++; message = "Import interrupted for oject in " + currFile + ". \n Error: " + e.getMessage() + "\n"; setStatus("Canceled"); clearSession(); log.error(message); messages[processIndex].append("Client canceled - " + damsDateFormat.format(new Date())); } } return exeResult; }
From source file:eu.sisob.uma.crawlerWorks.WebPagesOfUniversities.Format.FileFormatConversor.java
License:Open Source License
public static boolean createResearchersXMLFileFromCSV(File filePathCSV, File filePathXml) throws FileNotFoundException, IOException { boolean sucess = false; CSVReader reader = new CSVReader(new FileReader(filePathCSV), ';'); String[] nextLine;/*from w w w. ja v a 2 s .com*/ int idStaffIdentifier = -1; int idName = -1; int idFirstName = -1; int idLastName = -1; int idInitials = -1; int idUnitOfAssessment_Description = -1; int idInstitutionName = -1; int idWebAddress = -1; int idResearchGroupDescription = -1; int idResearcherWebAddress = -1; int idResearcherWebAddressType = -1; int idResearcherWebAddressExt = -1; if ((nextLine = reader.readNext()) != null) { //Locate indexes //Locate indexes for (int i = 0; i < nextLine.length; i++) { String column_name = nextLine[i]; if (column_name.equals(CSV_COL_ID)) idStaffIdentifier = i; else if (column_name.equals(CSV_COL_NAME)) idName = i; else if (column_name.equals(CSV_COL_FIRSTNAME)) idFirstName = i; else if (column_name.equals(CSV_COL_LASTNAME)) idLastName = i; else if (column_name.equals(CSV_COL_INITIALS)) idInitials = i; else if (column_name.equals(CSV_COL_SUBJECT)) idUnitOfAssessment_Description = i; else if (column_name.equals(CSV_COL_INSTITUTION_NAME)) idInstitutionName = i; else if (column_name.equals(CSV_COL_INSTITUTION_URL)) idWebAddress = i; else if (column_name.equals(CSV_COL_RESEARCHER_PAGE_URL)) idResearcherWebAddress = i; else if (column_name.equals(CSV_COL_RESEARCHER_PAGE_TYPE)) idResearcherWebAddressType = i; else if (column_name.equals(CSV_COL_RESEARCHER_PAGE_EXT)) idResearcherWebAddressExt = i; } } if (idLastName != -1 && idInitials != -1 && idStaffIdentifier != -1 && idWebAddress != -1 && idInstitutionName != -1 && idUnitOfAssessment_Description != -1) { Document document = DocumentHelper.createDocument(); Element root = document.addElement("root"); while ((nextLine = reader.readNext()) != null) { for (int k = 0; k < nextLine.length; k++) { nextLine[k] = nextLine[k].replace("'", ""); } Node n = root.selectSingleNode(XMLTags.INSTITUTION + "/" + XMLTags.INSTITUTION_NAME + "[text()='" + nextLine[idInstitutionName] + "']"); Element eInstitution = null; if (n == null) { eInstitution = root.addElement(XMLTags.INSTITUTION); eInstitution.addElement(XMLTags.INSTITUTION_NAME).addCDATA(nextLine[idInstitutionName]); eInstitution.addElement(XMLTags.INSTITUTION_WEBADDRESS) .addCDATA(idWebAddress == -1 ? "" : nextLine[idWebAddress]); } else { eInstitution = n.getParent(); } { n = eInstitution.selectSingleNode( XMLTags.UNIT_OF_ASSESSMENT + "/" + XMLTags.UNIT_OF_ASSESSMENT_DESCRIPTION + "[text()='" + nextLine[idUnitOfAssessment_Description] + "']"); Element eUnitOfAssessment = null; if (n == null) { eUnitOfAssessment = eInstitution.addElement(XMLTags.UNIT_OF_ASSESSMENT); eUnitOfAssessment.addElement(XMLTags.UNIT_OF_ASSESSMENT_DESCRIPTION) .addCDATA(nextLine[idUnitOfAssessment_Description]); //eUnitOfAssessment.addElement("DepartamentWebAddress").addCDATA(""); } else { eUnitOfAssessment = n.getParent(); } { String researchGroupDescription = ""; if (idResearchGroupDescription != -1) { researchGroupDescription = nextLine[idResearchGroupDescription]; } n = eUnitOfAssessment .selectSingleNode(XMLTags.RESEARCHGROUP + "/" + XMLTags.RESEARCHGROUP_DESCRIPTION + "[text()='" + researchGroupDescription + "']"); Element eResearchGroup = null; if (n == null) { eResearchGroup = eUnitOfAssessment.addElement(XMLTags.RESEARCHGROUP); eResearchGroup.addElement(XMLTags.RESEARCHGROUP_DESCRIPTION) .addCDATA(researchGroupDescription); } else { eResearchGroup = n.getParent(); } { Element eResearcher = eResearchGroup.addElement(XMLTags.RESEARCHER); eResearcher.addElement(XMLTags.RESEARCHER_STAFFIDENTIFIER) .addCDATA(nextLine[idStaffIdentifier]); if (idName != -1) { eResearcher.addElement(XMLTags.RESEARCHER_NAME).addCDATA(nextLine[idName]); } if (idFirstName != -1) { eResearcher.addElement(XMLTags.RESEARCHER_FIRSTNAME) .addCDATA(nextLine[idFirstName]); } eResearcher.addElement(XMLTags.RESEARCHER_LASTNAME).addCDATA(nextLine[idLastName]); eResearcher.addElement(XMLTags.RESEARCHER_INITIALS).addCDATA(nextLine[idInitials]); if (idResearcherWebAddress != -1) { String researcherWebAddress = ""; String researcherWebAddressType = ""; String researcherWebAddressExt = ""; researcherWebAddress = nextLine[idResearcherWebAddress].trim(); if (!researcherWebAddress.equals("")) { if (idResearcherWebAddressType != -1) researcherWebAddressType = nextLine[idResearcherWebAddressType]; else researcherWebAddressType = XMLTags.RESEARCHER_WEB_ADDRESS_ATTR_TYPE_VALUE_DEFAULT_CV; if (idResearcherWebAddressExt != -1) researcherWebAddressExt = nextLine[idResearcherWebAddressExt]; else researcherWebAddressExt = XMLTags.RESEARCHER_WEB_ADDRESS_ATTR_EXT_VALUE_DEFAULT_HTML; eResearcher.addElement(XMLTags.RESEARCHER_WEB_ADDRESS) .addAttribute(XMLTags.RESEARCHER_WEB_ADDRESS_ATTR_TYPE, researcherWebAddressType) .addAttribute(XMLTags.RESEARCHER_WEB_ADDRESS_ATTR_EXT, researcherWebAddressExt) .addCDATA(researcherWebAddress); } } } } } } reader.close(); FileOutputStream fileOS = new java.io.FileOutputStream(filePathXml, false); OutputStreamWriter writer = new java.io.OutputStreamWriter(fileOS, "UTF-8"); BufferedWriter bw = new java.io.BufferedWriter(writer); String sOut = document.asXML(); bw.write(sOut); bw.close(); writer.close(); fileOS.close(); sucess = true; } else { sucess = false; } return sucess; }
From source file:hudson.model.Api.java
License:Open Source License
/** * Exposes the bean as XML./*from w w w. j ava2 s . c om*/ */ public void doXml(StaplerRequest req, StaplerResponse rsp, @QueryParameter String xpath, @QueryParameter String wrapper, @QueryParameter String tree, @QueryParameter int depth) throws IOException, ServletException { setHeaders(rsp); String[] excludes = req.getParameterValues("exclude"); if (xpath == null && excludes == null) { // serve the whole thing rsp.serveExposedBean(req, bean, Flavor.XML); return; } StringWriter sw = new StringWriter(); // first write to String Model p = MODEL_BUILDER.get(bean.getClass()); TreePruner pruner = (tree != null) ? new NamedPathPruner(tree) : new ByDepth(1 - depth); p.writeTo(bean, pruner, Flavor.XML.createDataWriter(bean, sw)); // apply XPath FilteredFunctionContext functionContext = new FilteredFunctionContext(); Object result; try { Document dom = new SAXReader().read(new StringReader(sw.toString())); // apply exclusions if (excludes != null) { for (String exclude : excludes) { XPath xExclude = dom.createXPath(exclude); xExclude.setFunctionContext(functionContext); List<org.dom4j.Node> list = (List<org.dom4j.Node>) xExclude.selectNodes(dom); for (org.dom4j.Node n : list) { Element parent = n.getParent(); if (parent != null) parent.remove(n); } } } if (xpath == null) { result = dom; } else { XPath comp = dom.createXPath(xpath); comp.setFunctionContext(functionContext); List list = comp.selectNodes(dom); if (wrapper != null) { Element root = DocumentFactory.getInstance().createElement(wrapper); for (Object o : list) { if (o instanceof String) { root.addText(o.toString()); } else { root.add(((org.dom4j.Node) o).detach()); } } result = root; } else if (list.isEmpty()) { rsp.setStatus(HttpServletResponse.SC_NOT_FOUND); rsp.getWriter().print(Messages.Api_NoXPathMatch(xpath)); return; } else if (list.size() > 1) { rsp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); rsp.getWriter().print(Messages.Api_MultipleMatch(xpath, list.size())); return; } else { result = list.get(0); } } } catch (DocumentException e) { LOGGER.log(Level.FINER, "Failed to do XPath/wrapper handling. XML is as follows:" + sw, e); throw new IOException("Failed to do XPath/wrapper handling. Turn on FINER logging to view XML.", e); } if (isSimpleOutput(result) && !permit(req)) { // simple output prohibited rsp.sendError(HttpURLConnection.HTTP_FORBIDDEN, "primitive XPath result sets forbidden; implement jenkins.security.SecureRequester"); return; } // switch to gzipped output OutputStream o = rsp.getCompressedOutputStream(req); try { if (isSimpleOutput(result)) { // simple output allowed rsp.setContentType("text/plain;charset=UTF-8"); String text = result instanceof CharacterData ? ((CharacterData) result).getText() : result.toString(); o.write(text.getBytes("UTF-8")); return; } // otherwise XML rsp.setContentType("application/xml;charset=UTF-8"); new XMLWriter(o).write(result); } finally { o.close(); } }
From source file:io.mashin.oep.hpdl.XMLReadUtils.java
License:Open Source License
public static Node schemaVersionParentNode(Node node) { Node[] parentNode = new Node[1]; node.accept(new VisitorSupport() { @Override/*w w w .j a v a 2 s.com*/ public void visit(Attribute node) { if (node.getName().equalsIgnoreCase(XMLUtils.SCHEMA_VERSION_TAG)) { parentNode[0] = node.getParent(); } } }); return parentNode[0]; }
From source file:lost.tok.Link.java
License:Open Source License
/** * Links an existing discussion to a segment in the root of the ToK project *//*from w w w . jav a2 s. c o m*/ public void linkDiscussionRoot(Source linkedSource, LinkedList<Excerption> exp) { SubLink sl = new SubLink(linkedSource, exp); subLinkList.add(sl); String discFileName = linkedDiscussion.getDiscFileName(); // Open the Links file Document doc = GeneralFunctions.readFromXML(linkFile); Node link = doc.selectSingleNode("//link/discussionFile[text()=\"" //$NON-NLS-1$ + discFileName + "\"]"); //$NON-NLS-1$ Element newLink = null; if (link != null) { newLink = link.getParent(); } else { Element links = doc.getRootElement(); newLink = links.addElement("link"); //$NON-NLS-1$ newLink.addElement("discussionFile").addText(discFileName); //$NON-NLS-1$ newLink.addElement("type").addText(linkTypeXML); //$NON-NLS-1$ newLink.addElement("linkSubject").addText(subject); //$NON-NLS-1$ } Element subLink = newLink.addElement("sublink"); //$NON-NLS-1$ subLink.addElement("sourceFile").addText(linkedSource.toString()); //$NON-NLS-1$ for (Excerption element : exp) { subLink.add(element.toXML()); } GeneralFunctions.writeToXml(linkFile, doc); }
From source file:meddle.RString.java
License:Open Source License
public void treeWalk(Element element) { for (int i = 0, size = element.nodeCount(); i < size; i++) { Node node = element.node(i); if (node == null) continue; if (node instanceof Element) { treeWalk((Element) node); } else {//www . j a va 2 s .co m if (node.getParent() == null) continue; Util.debug(node.getParent().getName() + "\t" + node.getText()); String term = node.getParent().getName() + "=" + node.getText(); xmlTerms.add(term); } } }