List of usage examples for org.dom4j Element elementIterator
Iterator<Element> elementIterator(QName qName);
From source file:com.flaptor.hounder.indexer.DocumentConverter.java
License:Apache License
/** * @todo refactor this method, is too long *///from w w w .j a va 2 s.c o m private org.apache.lucene.document.Document processAdd(final Element e) throws IllegalDocumentException { // TODO: This method is too long, refactor. logger.debug("Processing Add"); float documentBoost; Node node = e.selectSingleNode("boost"); if (null == node) { documentBoost = 1.0F; } else { documentBoost = Float.parseFloat(node.getText()); if (logger.isEnabledFor(Level.DEBUG)) { logger.debug("Using non-default document boost of " + documentBoost); } } if (Float.isNaN(documentBoost) || Float.isInfinite(documentBoost) || documentBoost <= 0) { throw new IllegalDocumentException("Document with invalid boost (" + documentBoost + ") received."); } org.apache.lucene.document.Document ldoc = new org.apache.lucene.document.Document(); ldoc.setBoost(documentBoost); // For comparison with the required fields we keep track of the added // fields. HashSet<String> providedFields = new HashSet<String>(); //First, we add the documentId as a field under the name provided in the configuration (docIdName) node = e.selectSingleNode("documentId"); if (null == node) { throw new IllegalDocumentException("Add document missing documentId."); } String docIdText = node.getText(); //now we add the documentId as another field, using the name provided in the configuration (docIdName) Field lfield = new Field(docIdName, docIdText, Field.Store.YES, Field.Index.NOT_ANALYZED); ldoc.add(lfield); providedFields.add(docIdName); if (logger.isEnabledFor(Level.DEBUG)) { logger.debug("Writer - adding documentId field:" + docIdName + ", index: true, store: true, token: false, text: " + docIdText); } // Now we add the regular fields for (Iterator iter = e.elementIterator("field"); iter.hasNext();) { Element field = (Element) iter.next(); String fieldName, storedS, indexedS, tokenizedS, boostS, fieldText; boolean stored, tokenized, indexed; float boost = 1; fieldName = field.valueOf("@name"); if (fieldName.equals("")) { throw new IllegalDocumentException("Field without name."); } //There cannot be a field with the name used to store the documentId (docIdName) //as it would collide with the documentId per se when saved to the lucene index. fieldText = field.getText(); if (fieldName.equals(docIdName)) { throw new IllegalDocumentException( "This document contains a field with the same name as the configured name " + "to save the documentId( " + docIdName + ")."); } storedS = field.valueOf("@stored"); if (storedS.equals("")) { throw new IllegalDocumentException("Field without stored attribute."); } stored = Boolean.valueOf(storedS); indexedS = field.valueOf("@indexed"); if (indexedS.equals("")) { throw new IllegalDocumentException("Field without indexed attribute."); } indexed = Boolean.valueOf(indexedS); //Lucene complains of an unindexed unstored field with a runtime exception //and it makes no sense anyway if (!(indexed || stored)) { throw new IllegalDocumentException("processAdd: unindexed unstored field \"" + fieldName + "\"."); } tokenizedS = field.valueOf("@tokenized"); if (tokenizedS.equals("")) { throw new IllegalDocumentException("Field without tokenized attribute."); } tokenized = Boolean.valueOf(tokenizedS); boostS = field.valueOf("@boost"); if (!boostS.equals("")) { try { boost = new Float(boostS).floatValue(); } catch (NumberFormatException exception) { throw new IllegalDocumentException( "Unparsable boost value (" + boostS + ") for field \"" + fieldName + "\"."); } } // Now we add the fields. Depending on the parameter stored, indexed // and tokenized we call a different field constructor. lfield = null; Field.Index indexType = (indexed ? (tokenized ? Field.Index.ANALYZED : Field.Index.NOT_ANALYZED) : Field.Index.NO); Field.Store storeType; if (!stored) { storeType = Field.Store.NO; } else { if (compressedFields.contains(fieldName)) { storeType = Field.Store.COMPRESS; } else { storeType = Field.Store.YES; } } lfield = new Field(fieldName, fieldText, storeType, indexType); lfield.setBoost(boost); providedFields.add(fieldName); // for later comparison with the required fields ldoc.add(lfield); if (logger.isEnabledFor(Level.DEBUG)) { logger.debug("Writer - adding field:" + fieldName + ", index:" + indexed + ", store:" + stored + ", token:" + tokenized + " ,boost: " + boost + ", text: " + fieldText); } } // for (field iterator) HashSet<String> providedPayloads = new HashSet<String>(); // Now we add the payloads for (Iterator iter = e.elementIterator("payload"); iter.hasNext();) { Element payload = (Element) iter.next(); String payloadName = payload.valueOf("@name"); if (payloadName.equals("")) { throw new IllegalDocumentException("Payload without name."); } providedPayloads.add(payloadName); try { Long payloadValue = Long.parseLong(payload.getText()); ldoc.add(new Field(payloadName, new FixedValueTokenStream(payloadName, payloadValue))); logger.debug("Adding payload \"" + payloadName + "\" to document \"" + docIdText + "\" with value " + payloadValue); } catch (NumberFormatException nfe) { throw new IllegalDocumentException( "Writer - while parsing Long payload \"" + payloadName + "\": " + nfe.getMessage()); } } // no we test for the presence of the required fields if (!providedFields.containsAll(requiredFields) || !providedPayloads.containsAll(requiredPayloads)) { StringBuffer sb = new StringBuffer(); sb.append("Document with missing required fields or payloads. Ignoring addition.\n"); sb.append("Provided fields are: \n"); for (String field : providedFields) { sb.append(field + "\n"); } sb.append("The fields required are: \n"); for (String field : requiredFields) { sb.append(field + "\n"); } sb.append("Provided payloads are: \n"); for (String payload : providedPayloads) { sb.append(payload + "\n"); } sb.append("Required payloads are: \n"); for (String payload : requiredPayloads) { sb.append(payload + "\n"); } throw new IllegalDocumentException(sb.toString()); } return ldoc; }
From source file:com.flaptor.util.DomUtil.java
License:Apache License
/** * Returns the first attribute's value of the first ocurrence of an element. * @param root the root element of the dom * @param elementName the name of the element * @param attribName the name of the attribute * @return the value of the first element, or null if not found. * The first matching element is considered only. */// www .j a va2s . c o m public static String getAttributeValue(final Element root, final String elementName, final String attribName) { String value = null; Element element; Iterator elementIterator = root.elementIterator(elementName); while (elementIterator.hasNext()) { element = (Element) elementIterator.next(); value = element.attributeValue(attribName); } return value; }
From source file:com.flaptor.util.DomUtil.java
License:Apache License
/** * Returns the text of the first ocurrence of an element. * @param root the root element of the dom * @param elementName the name of the element * @return the value of the text of the first element, or null if not found. * The first matching element is considered only. *///from w w w . j a v a 2s . c o m public static String getElementText(final Element root, final String elementName) { String value = null; Element element; Iterator elementIterator = root.elementIterator(elementName); while (elementIterator.hasNext()) { element = (Element) elementIterator.next(); value = element.getText(); if (value != null) break; } return value; }
From source file:com.flaptor.util.DomUtil.java
License:Apache License
/** * Returns the first attribute's value of the first ocurrence of an element. * @param root the root element of the dom * @param elementName the name of the element * @param attribName the name of the attribute * @return the value of the first element, or null if not found. * The first matching element is considered only. *///from ww w . ja v a 2s . c o m public static void replaceElementValueByName(final Element root, final String elementName, final String name, final String value) { Element element; Iterator elementIterator = root.elementIterator(elementName); while (elementIterator.hasNext()) { element = (Element) elementIterator.next(); if (name.equals(element.attributeValue("name"))) { element.setText(DomUtil.filterXml(value)); break; } } }
From source file:com.github.autoprimer3.AutoPrimer3Config.java
License:Open Source License
public LinkedHashSet<String> readTableFile(Document dasXml) throws DocumentException { LinkedHashSet<String> tables = new LinkedHashSet<>(); Element root = dasXml.getRootElement(); Element gff = root.element("GFF"); Element segment = gff.element("SEGMENT"); for (Iterator i = segment.elementIterator("TYPE"); i.hasNext();) { Element type = (Element) i.next(); Attribute id = type.attribute("id"); tables.add(id.getValue());//from w w w. j a v a2s . c om } return tables; }
From source file:com.github.autoprimer3.AutoPrimer3Config.java
License:Open Source License
public LinkedHashSet<String> readTableFile(Document dasXml, String category) throws DocumentException { LinkedHashSet<String> tables = new LinkedHashSet<>(); Element root = dasXml.getRootElement(); Element gff = root.element("GFF"); Element segment = gff.element("SEGMENT"); for (Iterator i = segment.elementIterator("TYPE"); i.hasNext();) { Element type = (Element) i.next(); Attribute id = type.attribute("id"); Attribute cat = type.attribute("category"); if (cat.getValue().equals(category)) { tables.add(id.getValue());//from ww w .ja v a 2s .c o m } } return tables; }
From source file:com.github.autoprimer3.GetUcscBuildsAndTables.java
License:Open Source License
public void readDasGenomeXmlDocument() { if (dasGenomeXml == null) { return;// www . j av a 2 s .co m } Element root = dasGenomeXml.getRootElement(); for (Iterator i = root.elementIterator("DSN"); i.hasNext();) { Element dsn = (Element) i.next(); Element source = dsn.element("SOURCE"); Attribute build = source.attribute("id"); Element mapmaster = dsn.element("MAPMASTER"); Element desc = dsn.element("DESCRIPTION"); buildToMapMaster.put(build.getValue(), mapmaster.getText()); buildToDescription.put(build.getValue(), desc.getText()); } }
From source file:com.github.autoprimer3.GetUcscBuildsAndTables.java
License:Open Source License
public String retrieveSequence(String build, String chrom, Integer start, Integer end) throws DocumentException, MalformedURLException { if (buildToDescription.isEmpty()) { this.connectToUcsc(); }/* w ww .j a v a2s .c om*/ if (!buildToMapMaster.containsKey(build)) { return null; } else { StringBuilder dna = new StringBuilder(); URL genomeUrl = new URL( buildToMapMaster.get(build) + "/dna?segment=" + chrom + ":" + (start + 1) + "," + end); SAXReader reader = new SAXReader(); Document dasXml; dasXml = reader.read(genomeUrl); Element root = dasXml.getRootElement(); for (Iterator i = root.elementIterator("SEQUENCE"); i.hasNext();) { Element dsn = (Element) i.next(); Element seq = dsn.element("DNA"); String text = seq.getText().replaceAll("\n", ""); dna.append(text); //dna.append(seq.getText()); } return dna.toString(); } }
From source file:com.github.autoprimer3.SequenceFromDasUcsc.java
License:Open Source License
SequenceFromDasUcsc() {//get build names and DAS urls try {/*from w ww.java 2s . c o m*/ SAXReader reader = new SAXReader(); URL url = new URL("http://genome.ucsc.edu/cgi-bin/das/dsn");//usa //URL url = new URL("http://genome-euro.ucsc.edu/cgi-bin/das/dsn"); Document dasXml; dasXml = reader.read(url); Element root = dasXml.getRootElement(); for (Iterator i = root.elementIterator("DSN"); i.hasNext();) { Element dsn = (Element) i.next(); Element source = dsn.element("SOURCE"); Attribute build = source.attribute("id"); Element mapmaster = dsn.element("MAPMASTER"); buildToMapMaster.put(build.getValue(), mapmaster.getText()); } } catch (DocumentException | MalformedURLException ex) { //TO DO - handle (throw) this error properly ex.printStackTrace(); } }
From source file:com.github.autoprimer3.SequenceFromDasUcsc.java
License:Open Source License
public String retrieveSequence(String build, String chrom, Integer start, Integer end) throws DocumentException, MalformedURLException { if (!buildToMapMaster.containsKey(build)) { return null; } else {//from www . ja v a 2 s . co m String chromNumber = chrom.replaceFirst("chr", ""); int length = 0; URL entryPointUrl = new URL(buildToMapMaster.get(build) + "/entry_points"); Document dasXml; SAXReader reader = new SAXReader(); dasXml = reader.read(entryPointUrl); Element root = dasXml.getRootElement(); for (Iterator i = root.elementIterator("ENTRY_POINTS"); i.hasNext();) { Element dsn = (Element) i.next(); for (Iterator j = dsn.elementIterator("SEGMENT"); j.hasNext();) { Element seg = (Element) j.next(); String id = seg.attributeValue("id"); if (id != null && id.equals(chromNumber)) { String stop = seg.attributeValue("stop"); length = Integer.valueOf(stop); break; } } } if (length > 0) { end = end <= length ? end : length; } StringBuilder dna = new StringBuilder(); URL genomeUrl = new URL( buildToMapMaster.get(build) + "/dna?segment=" + chrom + ":" + (start + 1) + "," + end); dasXml = reader.read(genomeUrl); root = dasXml.getRootElement(); for (Iterator i = root.elementIterator("SEQUENCE"); i.hasNext();) { Element dsn = (Element) i.next(); Element seq = dsn.element("DNA"); String text = seq.getText().replaceAll("\n", ""); dna.append(text); //dna.append(seq.getText()); } return dna.toString(); } }