List of usage examples for org.dom4j Node getName
String getName();
getName
returns the name of this node.
From source file:bard.pubchem.xml.PubChemXMLParserFactory.java
License:Open Source License
protected PCAssay populateAssayFromXMLNode(Node topNode) throws PubChemException { // String assayDescPath = // "PC-AssaySubmit_assay/PC-AssaySubmit_assay_descr/PC-AssayDescription"; Node assayDescNode = null;// w w w . ja v a 2 s . c om if (topNode.getName().equals("PC-AssayDescription")) assayDescNode = topNode; else { assayDescNode = topNode.selectSingleNode(".//PC-AssayDescription"); } if (assayDescNode == null) throw new PubChemException( String.format("Cannot find PC-AssayDescription node in provided node %s", topNode.getPath())); Node node = assayDescNode.selectSingleNode("PC-AssayDescription_aid/PC-ID/PC-ID_id"); Integer aid = new Integer(node.getText()); try { PCAssay assay = new PCAssay(); if (aid > 0) assay.setAID(aid); node = assayDescNode.selectSingleNode("PC-AssayDescription_aid/PC-ID/PC-ID_version"); Integer version = new Integer(node.getText()); assay.setVersion(version); node = assayDescNode.selectSingleNode("PC-AssayDescription_revision"); Integer revision = new Integer(node.getText()); assay.setRevision(revision); Node trackingNode = assayDescNode .selectSingleNode("PC-AssayDescription_aid-source/PC-Source/PC-Source_db/PC-DBTracking"); node = trackingNode.selectSingleNode("PC-DBTracking_name"); assay.setSourceName(node.getText()); node = trackingNode.selectSingleNode("PC-DBTracking_source-id/Object-id/Object-id_str"); assay.setExtRegId(node.getText()); // hold until date node = trackingNode.selectSingleNode("PC-DBTracking_date"); if (node != null) { String year = node.selectSingleNode("Date/Date_std/Date-std/Date-std_year").getText(); String month = node.selectSingleNode("Date/Date_std/Date-std/Date-std_month").getText(); String day = node.selectSingleNode("Date/Date_std/Date-std/Date-std_day").getText(); if (DEBUGGING) log.info("year: " + year + " month: " + month + " day: " + day); Calendar calendar = Calendar.getInstance(); calendar.set(Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(day)); assay.setHoldUntilDate(calendar.getTime()); if (DEBUGGING) log.info(calendar.getTime().toString()); } node = assayDescNode.selectSingleNode("PC-AssayDescription_name"); assay.setName(node.getText()); List<Node> nodes = assayDescNode .selectNodes("PC-AssayDescription_description/PC-AssayDescription_description_E"); assay.setDescription(join(nodes, separator)); nodes = assayDescNode.selectNodes("PC-AssayDescription_protocol/PC-AssayDescription_protocol_E"); assay.setProtocol(join(nodes, separator)); nodes = assayDescNode.selectNodes("PC-AssayDescription_comment/PC-AssayDescription_comment_E"); assay.setComment(join(nodes, separator)); node = assayDescNode.selectSingleNode("PC-AssayDescription_activity-outcome-method"); if (node != null) assay.setActivityOutcomeMethod(node.valueOf("@value")); handlePlots(assay, assayDescNode); node = assayDescNode .selectSingleNode("PC-AssayDescription_grant-number/PC-AssayDescription_grant-number_E"); if (node != null) assay.setGrantNumber(node.getText()); node = assayDescNode.selectSingleNode("PC-AssayDescription_project-category"); if (node != null) assay.setProjectCategory(node.valueOf("@value")); assay.getAssayXRefs().removeAll(assay.getAssayXRefs()); nodes = assayDescNode.selectNodes("PC-AssayDescription_xref/PC-AnnotatedXRef"); handleXRefs(assay, null, nodes); nodes = assayDescNode.selectNodes("PC-AssayDescription_target/PC-AssayTargetInfo"); handleTargetXRefs(assay, null, nodes); handlePanels(assay, assayDescNode); handleColumns(assay, assayDescNode); handleComments(assay, assayDescNode); return assay; } catch (Exception ex) { throw new RuntimeException("Problem with AID " + aid, ex); } }
From source file:bard.pubchem.xml.PubChemXMLParserFactory.java
License:Open Source License
protected void handleXRefs(PCAssay assay, PCAssayPanel panel, List<Node> nodes) { for (Node n : nodes) { Node node = n.selectSingleNode("PC-AnnotatedXRef_comment"); String comment = node == null ? "" : node.getText(); node = n.selectSingleNode("PC-AnnotatedXRef_xref/PC-XRefData/*"); String type = node.getName(); type = type.substring(type.lastIndexOf("_") + 1, type.length()); String database = targetType.containsKey(type) ? targetType.get(type) : type; String id = node.getText(); XRef xref = new XRef(); xref.setXRefId(id);/*from w ww.j a v a 2 s . c o m*/ xref.setDatabase(database); xref.setType(type); PCAssayXRef aXref = new PCAssayXRef(); aXref.setPanel(panel); aXref.setAssay(assay); aXref.setComment(comment); aXref.setXRef(xref); aXref.setTarget(false); assay.getAssayXRefs().add(aXref); } }
From source file:ch.javasoft.xml.config.XmlUtil.java
License:BSD License
/** * Returns an xpath like string for the given xml node * //from w w w. j av a 2s.co m * @param node the node to convert to a string * @param recurseParents true if parents should be included * * @return an xpath like string for the given node */ public static String getNodePath(Node node, boolean recurseParents) { if (node instanceof Element) { return getElementPath((Element) node, recurseParents); } final StringBuilder sb = new StringBuilder(node.getName()); if (recurseParents && node.getParent() != null) { sb.insert(0, node instanceof Attribute ? '@' : '/'); sb.insert(0, getElementPath(node.getParent(), recurseParents)); } return sb.toString(); }
From source file:com.adspore.splat.xep0060.PubSubService.java
License:Open Source License
/** * Returns Discovery Items based on the the targetJID and the sender JID's ability * to discover those items./*from w ww . j a va2 s .co m*/ */ public Iterator<DiscoItem> getItems(JID targetJID, JID senderJID) { List<DiscoItem> result = new ArrayList<DiscoItem>(); if (null == targetJID.getResource()) { Collection<Node> rootCollection = mRootCollectionNode.getNodes(); Iterator<Node> itor = rootCollection.iterator(); while (itor.hasNext()) { Node nestedNode = itor.next(); if (canDiscoverNode(nestedNode)) { result.add( new DiscoItem(mComponent.getJID(), nestedNode.getName(), nestedNode.getNodeID(), null)); } } LOG.debug("#getItems()...ROOT: {} items returned", result.size()); } else if (null != targetJID.getResource()) { // Specific Node within hierarchy if (mNodes.containsKey(targetJID.getResource())) { Node targetNode = mNodes.get(targetJID.getResource()); if (targetNode.isCollectionNode()) { Collection<Node> collection = targetNode.getNodes(); Iterator<Node> itor = collection.iterator(); while (itor.hasNext()) { Node nestedNode = itor.next(); if (canDiscoverNode(nestedNode)) { result.add(new DiscoItem(mComponent.getJID(), nestedNode.getName(), nestedNode.getNodeID(), null)); } } LOG.debug("#getItems()...Target is Collection: {}, {} items returned", targetJID.getResource(), result.size()); } else { for (PublishedItem publishedItem : targetNode.getPublishedItems()) { DiscoItem toAdd = new DiscoItem(mComponent.getJID(), publishedItem.getID(), null, null); result.add(toAdd); } LOG.debug("#getItems()...Target is Leaf: {}, {} items returned", targetJID.getResource(), result.size()); } } } return result.iterator(); }
From source file:com.alibaba.toolkit.util.resourcebundle.xml.XMLResourceBundle.java
License:Open Source License
/** * ?XML Node?resource.// www . j ava 2 s . c o m * * @param resourceNode resource?XML Node * @param enumType <code>Enum</code> * @throws ResourceBundleCreateException ? */ protected void initResource(Node resourceNode, Class enumType) throws ResourceBundleCreateException { String id = (String) resourceNode.selectObject(ResourceBundleConstant.XPATH_RESOURCE_ID); // enum, enumresource key. if (enumType != null) { Enum enumObj = Enum.getEnumByName(enumType, id); if (enumObj == null) { throw new ResourceBundleCreateException(ResourceBundleConstant.RB_ENUM_ID_NOT_FOUND, new Object[] { id, enumType.getName() }, null); } id = enumObj.toString(); } Object value = null; String type = resourceNode.getName(); if (ResourceBundleConstant.RB_RESOURCE_TYPE_MESSAGE.equals(type)) { value = getMessageResource(id, resourceNode); } else if (ResourceBundleConstant.RB_RESOURCE_TYPE_MAP.equals(type)) { value = getMapResource(id, resourceNode); } else if (ResourceBundleConstant.RB_RESOURCE_TYPE_LIST.equals(type)) { value = getListResource(id, resourceNode); } if (values.containsKey(id)) { throw new ResourceBundleCreateException(ResourceBundleConstant.RB_DUPLICATED_RESOURCE_KEY, new Object[] { id }, null); } values.put(id, value); }
From source file:com.alibaba.toolkit.util.resourcebundle.xml.XMLResourceBundle.java
License:Open Source License
/** * ?XML Nodemap resource./*from www . j a va 2 s . c om*/ * * @param id resource ID * @param resourceNode resource?XML Node * @return resource * @throws ResourceBundleCreateException ? */ protected Object getMapResource(String id, Node resourceNode) throws ResourceBundleCreateException { ListMap map = new ArrayHashMap(); for (Iterator i = resourceNode.selectNodes(ResourceBundleConstant.XPATH_RESOURCES).iterator(); i .hasNext();) { Node mapItemNode = (Node) i.next(); Object mapKey = mapItemNode.selectObject(ResourceBundleConstant.XPATH_RESOURCE_ID); if (map.containsKey(id)) { throw new ResourceBundleCreateException(ResourceBundleConstant.RB_DUPLICATED_MAP_RESOURCE_KEY, new Object[] { mapKey, id }, null); } String mapItemType = mapItemNode.getName(); Object value = null; if (ResourceBundleConstant.RB_RESOURCE_TYPE_MESSAGE.equals(mapItemType)) { value = getMessageResource(id, mapItemNode); } else if (ResourceBundleConstant.RB_RESOURCE_TYPE_MAP.equals(mapItemType)) { value = getMapResource(id, mapItemNode); } else if (ResourceBundleConstant.RB_RESOURCE_TYPE_LIST.equals(mapItemType)) { value = getListResource(id, mapItemNode); } map.put(mapKey, value); } return Collections.unmodifiableMap(map); }
From source file:com.alibaba.toolkit.util.resourcebundle.xml.XMLResourceBundle.java
License:Open Source License
/** * ?XML Nodelist resource./*from www . jav a 2 s . c o m*/ * * @param id resource ID * @param resourceNode resource?XML Node * @return resource * @throws ResourceBundleCreateException ? */ protected Object getListResource(String id, Node resourceNode) throws ResourceBundleCreateException { List list = new ArrayList(); for (Iterator i = resourceNode.selectNodes(ResourceBundleConstant.XPATH_RESOURCES).iterator(); i .hasNext();) { Node listItemNode = (Node) i.next(); String listItemType = listItemNode.getName(); Object value = null; if (ResourceBundleConstant.RB_RESOURCE_TYPE_MESSAGE.equals(listItemType)) { value = getMessageResource(id, listItemNode); } else if (ResourceBundleConstant.RB_RESOURCE_TYPE_MAP.equals(listItemType)) { value = getMapResource(id, listItemNode); } else if (ResourceBundleConstant.RB_RESOURCE_TYPE_LIST.equals(listItemType)) { value = getListResource(id, listItemNode); } list.add(value); } return Collections.unmodifiableList(list); }
From source file:com.appeligo.ccdataindexer.Indexer.java
License:Apache License
public boolean indexProgram(File programFile, Network network) throws IOException { log.debug("processing file " + programFile + " for " + network.getStationName()); boolean needToClose = openIndex(); StringBuilder captions = new StringBuilder(); InputStream is = null;//w w w .j a v a 2s. c om try { is = new GZIPInputStream(new BufferedInputStream(new FileInputStream(programFile))); SAXReader reader = new SAXReader(); reader.setEntityResolver(new ExternalResolver()); Document document = null; try { document = reader.read(is); } catch (DocumentException e) { log.warn("Could not open document " + programFile + "; ", e); return false; } //Node startTimeNode = document.selectSingleNode("//meta[@name='StartTime']"); Node startTimeNode = document.selectSingleNode("//*[name()='meta'][@name='StartTime']"); long startTime; try { startTime = Long.parseLong(startTimeNode.valueOf("@content")); } catch (NumberFormatException e) { log.warn("Error parsing StartTime " + startTimeNode + "; ", e); return false; } //Node programNode = document.selectSingleNode("//meta[@name='ProgramID']"); Node programNode = document.selectSingleNode("//*[name()='meta'][@name='ProgramID']"); String programId = programNode.valueOf("@content"); programId = updateProgramId(programId); //Node endTimeNode = document.selectSingleNode("//meta[@name='EndTime']"); Node endTimeNode = document.selectSingleNode("//*[name()='meta'][@name='EndTime']"); long endTime; try { endTime = Long.parseLong(endTimeNode.valueOf("@content")); } catch (NumberFormatException e) { log.warn("Error parsing endTime " + endTimeNode + "; ", e); return false; } //List divs = document.selectNodes("/html/body/div"); List divs = document.selectNodes("/*[name()='html']/*[name()='body']/*[name()='div']"); while (divs.size() > 0) { Element div = (Element) divs.remove(0); List children = div.selectNodes("child::node()"); while (children.size() > 0) { Node a = (Node) children.remove(0); while (!("a".equals(a.getName()))) { if (children.size() == 0) { break; } a = (Node) children.remove(0); } if (!("a".equals(a.getName()))) { break; } Node afterA = (Node) children.remove(0); if (afterA instanceof Element) { if (!("span".equals(afterA.getName()))) { throw new IOException("span expected... bad data in " + programFile); } //Don't include this in the captions or should we? //Element span = (Element)afterA; //captions.append(' '); //captions.append(span.getText().replace(">>", "").trim()); afterA = (Node) children.remove(0); } StringBuilder sentence = new StringBuilder(); if (afterA instanceof Text) { Text sentenceNode = (Text) afterA; sentence.append(sentenceNode.getText()); } else { Entity entity = (Entity) afterA; sentence.append(entity.asXML()); } /* while (children.get(0) instanceof Text) { Text moreText = (Text)children.remove(0); captions.append(' '); captions.append(DocumentUtil.prettySentence(moreText.getText())); } */ while (children.get(0) instanceof Text || children.get(0) instanceof Entity) { if (children.get(0) instanceof Text) { Text moreText = (Text) children.remove(0); sentence.append(moreText.getText()); } else { Entity entity = (Entity) children.remove(0); sentence.append(entity.asXML()); } } captions.append(DocumentUtil.prettySentence(sentence.toString().trim())); captions.append(' '); } } ArrayList<ScheduledProgram> skedulePrograms = new ArrayList<ScheduledProgram>(); long lookupTimeStart = System.currentTimeMillis(); for (String lineupId : networkLineups) { log.debug("looking for future scheduled program: " + programId + " on lineup " + lineupId); ScheduledProgram skedProg = epg.getNextShowing(lineupId, programId, false, true); if (skedProg == null) { log.debug("looking for past scheduled program: " + programId + " on lineup " + lineupId); skedProg = epg.getLastShowing(lineupId, programId); } log.debug("Sked prog for program: " + programId + " is null " + (skedProg == null)); //We Can we fake a scheduled program? //Program program = epg.getProgram(programId); if (skedProg == null) { log.debug("Unable to locate ScheduleProgram for " + programId); Program program = epg.getProgram(programId); if (program != null) { skedProg = new ScheduledProgram(); skedProg.setNetwork(network); skedProg.setDescription(program.getDescription()); skedProg.setDescriptionWithActors(program.getDescriptionWithActors()); skedProg.setEndTime(new Date(endTime)); skedProg.setStartTime(new Date(startTime)); skedProg.setCredits(program.getCredits()); skedProg.setGenreDescription(program.getGenreDescription()); skedProg.setProgramId(program.getProgramId()); skedProg.setProgramTitle(program.getProgramTitle()); skedProg.setEpisodeTitle(program.getEpisodeTitle()); skedProg.setLastModified(program.getLastModified()); skedProg.setOriginalAirDate(program.getOriginalAirDate()); skedProg.setRunTime(program.getRunTime()); skedProg.setScheduleId(0); skedProg.setTvRating(program.getTvRating()); skedProg.setNewEpisode(false); skedProg.setLineupId(lineupId); } } if (skedProg != null) { skedulePrograms.add(skedProg); if (indexWriter == null) { // probably only doing composite index, so one is all we need break; } } } lookupTime += (System.currentTimeMillis() - lookupTimeStart); if (captions.length() > 250 && skedulePrograms.size() > 0) { //Delete any old duplicates Term term = new Term("programID", skedulePrograms.get(0).getProgramId()); if (indexWriter != null) { indexWriter.deleteDocuments(term); //Now insert the new document. org.apache.lucene.document.Document doc = new org.apache.lucene.document.Document(); DocumentUtil.addCaptions(doc, captions.toString()); DocumentUtil.populateDocument(doc, skedulePrograms, new Date()); indexWriter.addDocument(doc); } if (compositeIndexWriter != null) { compositeIndexWriter.deleteDocuments(term); org.apache.lucene.document.Document compositeDoc = new org.apache.lucene.document.Document(); DocumentUtil.populateCompositeDocument(compositeDoc, captions.toString().replaceAll("[.!?]* ", " "), skedulePrograms); compositeIndexWriter.addDocument(compositeDoc); } log.debug("Adding to index now:" + skedulePrograms.get(0).getProgramId() + " " + skedulePrograms.get(0).getProgramTitle()); return true; } else { log.debug("Limited CC data(" + captions.length() + " character) or unable to locate EPG date for the program. Not adding this program to the index:" + programId); return false; } } finally { if (is != null) { try { is.close(); } catch (IOException e) { log.error("Error closing file", e); } } if (needToClose) { closeIndex(); } } }
From source file:com.appeligo.channelfeed.work.FileReaderThread.java
License:Apache License
public void run() { try {/*from w w w. j a va 2 s.c om*/ SAXReader reader = new SAXReader(); Document document; try { document = reader.read(new File(ccDocumentRoot + captionFilename)); } catch (DocumentException e) { log.error("Could not open document " + ccDocumentRoot + captionFilename + "; ", e); return; } do { //Node startTimeNode = document.selectSingleNode("//meta[@name='StartTime']"); Node startTimeNode = document.selectSingleNode("//*[name()='meta'][@name='StartTime']"); long startTime; try { startTime = Long.parseLong(startTimeNode.valueOf("@content")); } catch (NumberFormatException e) { throw new Error(e); } //Node endTimeNode = document.selectSingleNode("//meta[@name='EndTime']"); Node endTimeNode = document.selectSingleNode("//*[name()='meta'][@name='EndTime']"); long endTime; try { endTime = Long.parseLong(endTimeNode.valueOf("@content")); } catch (NumberFormatException e) { throw new Error(e); } long durationMillis = endTime - startTime; if (autoAdvance) { long durationSeconds = durationMillis / 1000; Calendar midnight = Calendar.getInstance(); midnight.setTime(new Date()); midnight.setTimeZone(TimeZone.getTimeZone("GMT")); int year = midnight.get(Calendar.YEAR); int month = midnight.get(Calendar.MONTH); int dayOfMonth = midnight.get(Calendar.DAY_OF_MONTH); midnight.clear(); midnight.set(year, month, dayOfMonth); long midnightMillis = midnight.getTimeInMillis(); long secondsSinceMidnight = (System.currentTimeMillis() - midnightMillis) / 1000; //int loopsSinceMidnight = (int)(secondsSinceMidnight / durationSeconds); advanceSeconds = (int) (secondsSinceMidnight % durationSeconds); } long newStartTime = System.currentTimeMillis() - (advanceSeconds * 1000); long newEndTime = newStartTime + durationMillis; setProgram(newStartTime).setStartTime(new Date(newStartTime)); // List divs = document.selectNodes("/html/body/div"); List divs = document.selectNodes("/*[name()='html']/*[name()='body']/*[name()='div']"); while ((divs.size() > 0) && (!aborted)) { Element div = (Element) divs.remove(0); List children = div.selectNodes("child::node()"); while ((children.size() > 0) && (!aborted)) { Node a = (Node) children.remove(0); while (!("a".equals(a.getName()))) { if (children.size() == 0) { break; } a = (Node) children.remove(0); } if (!("a".equals(a.getName()))) { break; } long timestamp; try { timestamp = Long.parseLong(a.valueOf("@name")); } catch (NumberFormatException e) { throw new Error(e); } long offset = timestamp - startTime; long delayUntil = newStartTime + offset; log.debug("Next sentence in " + ((delayUntil - System.currentTimeMillis()) / 1000) + " seconds"); while (System.currentTimeMillis() < delayUntil) { try { Thread.sleep(delayUntil - System.currentTimeMillis()); } catch (InterruptedException e) { } } String speakerChange = null; Node afterA = (Node) children.remove(0); if (afterA instanceof Element) { if (!("span".equals(afterA.getName()))) { throw new Error("span expected"); } Element span = (Element) afterA; speakerChange = span.getText().replace(">>", "").trim(); afterA = (Node) children.remove(0); } String sentence; if (afterA instanceof Text) { Text sentenceNode = (Text) afterA; sentence = sentenceNode.getText(); } else { Entity entity = (Entity) afterA; sentence = entity.asXML(); } while (children.get(0) instanceof Text || children.get(0) instanceof Entity) { if (children.get(0) instanceof Text) { Text moreText = (Text) children.remove(0); sentence += moreText.getText(); } else { Entity entity = (Entity) children.remove(0); sentence += entity.asXML(); } } sentence = sentence.trim(); /* if (!docOpened) { openDoc(newStartTime - SCHEDULE_VARIANCE); } */ //if (program != null) { getDestinations().writeSentence(getProgram(), delayUntil, speakerChange, sentence); //} } } if (loop) { autoAdvance = false; advanceSeconds = 0; log.debug("Waiting for end of program in " + ((newEndTime - System.currentTimeMillis()) / 1000) + " seconds"); while (System.currentTimeMillis() < newEndTime) { try { Thread.sleep(newEndTime - System.currentTimeMillis()); } catch (InterruptedException e) { } } } } while (loop); } catch (Exception e) { log.error("Uncaught exception!", e); } }
From source file:com.beetle.framework.persistence.access.DBConfig.java
License:Apache License
public static List<String> getAllDatasourcename() { Document doc = null;// ww w .ja v a 2 s.co m try { List<String> myList = new ArrayList<String>(); doc = XMLReader.getXmlDoc(AppProperties.getAppHome() + "DBConfig.xml"); Element root = doc.getRootElement(); for (Iterator<?> i = root.elementIterator("DataSources"); i.hasNext();) { Element e = (Element) i.next(); @SuppressWarnings("unchecked") Iterator<Node> n = e.nodeIterator(); while (n.hasNext()) { Node at = n.next(); if (at.getName() != null) { myList.add(at.getName()); } } } return myList; } catch (Exception e) { throw new AppRuntimeException(e); } finally { if (doc != null) { doc.clearContent(); } } }