Java Code Examples for org.dom4j.Element.valueOf()

The following are Jave code examples for showing how to use valueOf() of the org.dom4j.Element class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: joai-project   File: XMLQueryParser.java   View Source Code Vote up 6 votes
private static Query applyBoost(Element queryElement, Query query) throws Exception {
	if (query == null || queryElement == null)
		return query;

	String boost = queryElement.valueOf("@boost");
	if (boost != null && boost.length() > 0) {
		float b = 1;
		try {
			b = Float.parseFloat(boost);
		} catch (NumberFormatException nfe) {
			String path = queryElement.attribute("boost").getUniquePath();
			throw new Exception("Error parsing document: boost value was not valid (" +
				nfe.getMessage() + "). Value must be a number, for example 1.5, 2.4. Error found at " + path);
		}
		query.setBoost(b);
	}
	return query;
}
 
Example 2
Project: joai-project   File: XSDatatypeManager.java   View Source Code Vote up 5 votes
/**
 *  Relies on the SimpleType def to derive a XSDatatype
 *
 *@param  def            Description of the Parameter
 *@return                Description of the Return Value
 *@exception  Exception  Description of the Exception
 */
private XSDatatype deriveByRestriction(SimpleType def)
	throws Exception {

	// prtln ("deriveByRestriction with " + def.getElement().asXML());
	Element restriction = def.getFirstChild();
	String baseName = getBaseRestrictionName(restriction);
	// prtln (" ... baseName: " + baseName);
	if (baseName == null) {
		prtln("didn't get getBaseRestrictionName!\n\t" + def.toString());
		return null;
	}
	XSDatatype baseType = DatatypeFactory.getTypeByName(baseName);
	if (baseType == null)
		throw new Exception ("deriveByRestriction error: base XSDatatype not found for " + baseName);
	TypeIncubator incubator = new TypeIncubator(baseType);
	// now add facets to incubator
	for (Iterator i = restriction.elementIterator(); i.hasNext(); ) {
		Element child = (Element) i.next();
		String facetName = child.getName();
		String facetValue = child.valueOf("@value");
		try {
			incubator.addFacet(facetName, facetValue, true, null);
		} catch (Exception e) {
			prtln("couldnt add facet: " + facetName + ", " + facetValue + "\n" + e);
		}
	}
	// XSDatatype derived = incubator.derive("", def.getName());
	XSDatatype derived = incubator.derive (def.getNamespace().getURI(), def.getName());
	return derived;
}
 
Example 3
Project: joai-project   File: XSDatatypeManager.java   View Source Code Vote up 5 votes
/**
*  Given a restriction element, return the value of the <i>base</i> attribute. 
	Currently requires
 *  that a "xsd:" prefix is present in the base attribute, and remove the prefix before returning remainder
 *  of base attribute. 
 
 @see #deriveByRestriction(SimpleType)
 *
 *@param  e  Description of the Parameter
 *@return    The baseName value
 */
private String getBaseRestrictionName(Element e) {
	String bn = e.valueOf("@base");
	String prefix = e.getNamespacePrefix()+":";
	if (!bn.startsWith(prefix)) {
		// prtln ("getBaseRestrictionName report");
		// prtln ("\tnamespaceURI: " + e.getNamespaceURI());
		
		// return null;
		return bn;
	}
	else {
		return bn.substring(prefix.length());
	}
}
 
Example 4
Project: parabuild-ci   File: Filter.java   View Source Code Vote up 5 votes
private Matcher getMatcher(Element element) throws FilterException {
	// These will be either BugCode, Method, or Or elements.
	String name = element.getName();
	if (name.equals("BugCode")) {
		return new BugCodeMatcher(element.valueOf("@name"));
	} else if (name.equals("Method")) {
		Attribute nameAttr = element.attribute("name");
		Attribute paramsAttr = element.attribute("params");
		Attribute returnsAttr = element.attribute("returns");

		if (nameAttr == null)
			throw new FilterException("Missing name attribute in Method element");

		if ((paramsAttr != null || returnsAttr != null) && (paramsAttr == null || returnsAttr == null))
			throw new FilterException("Method element must have both params and returns attributes if either is used");

		if (paramsAttr == null)
			return new MethodMatcher(nameAttr.getValue());
		else
			return new MethodMatcher(nameAttr.getValue(), paramsAttr.getValue(), returnsAttr.getValue());
	} else if (name.equals("Or")) {
		OrMatcher orMatcher = new OrMatcher();
		Iterator i = element.elementIterator();
		while (i.hasNext()) {
			orMatcher.addChild(getMatcher((Element) i.next()));
		}
		return orMatcher;
	} else
		throw new FilterException("Unknown element: " + name);
}
 
Example 5
Project: parabuild-ci   File: ConvertToARFF.java   View Source Code Vote up 5 votes
public String getInstanceValue(Element element, String appName) throws MissingNodeException {
	String annotationText = element.valueOf("./UserAnnotation[text()]");
	//System.out.println("annotationText=" + annotationText);

	int state = getBugClassification(annotationText);

	if (state == NOT_BUG)
		return "not_bug";
	else if (state == BUG)
		return "bug";
	else if (state == HARMLESS_BUG)
		return "harmless_bug";
	else
		throw new MissingNodeException("Unclassified warning");
}
 
Example 6
Project: parabuild-ci   File: ConvertToARFF.java   View Source Code Vote up 5 votes
private List getBugInstanceList(Document document) {
	List bugInstanceList = document.selectNodes("/BugCollection/BugInstance");
	if (dropUnclassifiedWarnings) {
		for (Iterator i = bugInstanceList.iterator(); i.hasNext(); ) {
			Element element = (Element) i.next();
			String annotationText = element.valueOf("./UserAnnotation[text()]");
			int classification = getBugClassification(annotationText);
			if (classification == UNCLASSIFIED)
				i.remove();
		}
	}
	return bugInstanceList;
}
 
Example 7
Project: joai-project   File: VirtualSearchFieldMapper.java   View Source Code Vote up 4 votes
private void addVirtualSearchField(Element virtualSearchField) throws Exception {

		String field = virtualSearchField.valueOf("@field");

		if (field == null || field.trim().length() == 0)

			throw new Exception("Error parsing document: element <virtualSearchField> must have a single, non-empty 'field' attribute. Error found at " + virtualSearchField.getPath());



		List virtualSearchTermDefinitions = virtualSearchField.selectNodes("virtualSearchTermDefinition");

		for (int i = 0; i < virtualSearchTermDefinitions.size(); i++)

			addVirtualSearchTermDefinition((Element) virtualSearchTermDefinitions.get(i), field);

	}
 
Example 8
Project: joai-project   File: ItemDocReader.java   View Source Code Vote up 4 votes
private List doGetMultiRelatedResources(boolean displayable) {

		ArrayList items = new ArrayList();
		items.add(this);

		ResultDocList results = null;
		if (displayable)
			results = getDisplayableAssociatedItemResultDocs();
		else
			results = getAssociatedItemResultDocs();
		if (results != null) {
			for (int i = 0; i < results.size(); i++)
				items.add(results.get(i).getDocReader());
		}

		//prtln("getMultiRelatedResources(): items size: " + items.size() );

		ArrayList theRelatedResources = new ArrayList();

		for (int i = 0; i < items.size(); i++) {
			ItemDocReader xmlDocReader = (ItemDocReader) items.get(i);
			String specifiedById = xmlDocReader.getId();
			List relationElms = xmlDocReader.getXmlDoc().selectNodes("//*[local-name()='itemRecord']/*[local-name()='relations']//*[local-name()='relation']/*");
			if (relationElms != null) {
				for (int j = 0; j < relationElms.size(); j++) {
					try {
						Element relationElm = (Element) relationElms.get(j);
						String id = null;
						String title = null;
						String url = null;
						String kind = relationElm.valueOf("@kind");
						// Handle idEntry
						if (relationElm.getName().equals("idEntry")) {
							id = relationElm.valueOf("@entry");
							if(id == null || id.trim().length() == 0)
								continue;
							
							ResultDoc relatedDoc = null;
							if (displayable)
								relatedDoc = recordDataService.getDisplayableItemResultDoc(id);
							else
								relatedDoc = recordDataService.getItemResultDoc(id);
							if (relatedDoc != null) {
								ItemDocReader itemDocReader = (ItemDocReader) relatedDoc.getDocReader();
								title = itemDocReader.getTitle();
								url = itemDocReader.getUrl();
							}
							else
								continue;
						}
						// Handle urlEntry
						else {
							title = relationElm.valueOf("@title");
							url = relationElm.valueOf("@url");
						}
						RelatedResource relatedResource = new RelatedResource(id, title, url, kind, specifiedById);
						//prtln(relatedResource.toString());
						if (!theRelatedResources.contains(relatedResource))
							theRelatedResources.add(relatedResource);
					} catch (Throwable t) {
						prtlnErr("error getting related resource: " + t);
					}
				}
			}
		}
		return theRelatedResources;
	}
 
Example 9
Project: joai-project   File: XMLQueryParser.java   View Source Code Vote up 4 votes
private static Query makeBooleanQuery(Element booleanQueryElement, QueryParser queryParser) throws Exception {
	String operator = booleanQueryElement.valueOf("@type");

	boolean isRequired = false;
	boolean isProhibited = false;

	if (operator == null)
		throw new Exception("Error parsing document: element <booleanQuery> must contain an attribite named 'type' that contains the value 'AND' or 'OR'. Error found at " + booleanQueryElement.getUniquePath());
	else if (operator.equalsIgnoreCase("OR"))
		isRequired = false;
	else if (operator.equalsIgnoreCase("AND"))
		isRequired = true;
	else
		throw new Exception("Error parsing document: element <booleanQuery> must contain an attribite named 'type' that contains the value 'AND' or 'OR' but value '" + operator + "' was found. Error found at " + booleanQueryElement.getUniquePath());

	BooleanQuery booleanQuery = new BooleanQuery();

	// iterate through child elements of booleanClause
	Query query = null;
	for (Iterator i = booleanQueryElement.elementIterator(); i.hasNext(); ) {
		Element element = (Element) i.next();

		// Exclude from results or require (overrides previous boolean designation)?
		String excludeOrRequire = element.attributeValue("excludeOrRequire");
		if (excludeOrRequire != null) {
			excludeOrRequire = excludeOrRequire.trim();
			if (excludeOrRequire.equalsIgnoreCase("exclude")) {
				isRequired = false;
				isProhibited = true;
			}
			else if (excludeOrRequire.equalsIgnoreCase("require")) {
				isRequired = true;
				isProhibited = false;
			}
			else if (excludeOrRequire.equalsIgnoreCase("neither")) {
				isRequired = false;
				isProhibited = false;
			}
			else {
				throw new Exception("Error parsing document: the value of attribute excludeOrRequire must be one of 'exclude', 'require' or 'neither' but '"
					 + excludeOrRequire + "' was found at " + booleanQueryElement.getUniquePath());
			}
		}

		if (element.getName().equals("booleanQuery")) {
			query = makeBooleanQuery(element, queryParser);
			if (query != null) {
				if (isRequired && !isProhibited)
					booleanQuery.add(query, BooleanClause.Occur.MUST);
				else if (!isRequired && isProhibited)
					booleanQuery.add(query, BooleanClause.Occur.MUST_NOT);
				else
					booleanQuery.add(query, BooleanClause.Occur.SHOULD);
			}
		}
		else {
			query = makeLuceneQuery(element, queryParser);
			if (query != null) {
				if (isRequired && !isProhibited)
					booleanQuery.add(query, BooleanClause.Occur.MUST);
				else if (!isRequired && isProhibited)
					booleanQuery.add(query, BooleanClause.Occur.MUST_NOT);
				else
					booleanQuery.add(query, BooleanClause.Occur.SHOULD);
			}
		}
	}

	return applyBoost(booleanQueryElement, booleanQuery);
}
 
Example 10
Project: parabuild-ci   File: Filter.java   View Source Code Vote up 4 votes
private void parse(String fileName) throws IOException, FilterException {

		Document filterDoc = null;

		try {
			SAXReader reader = new SAXReader();
			filterDoc = reader.read(new BufferedInputStream(new FileInputStream(fileName)));
		} catch (DocumentException e) {
			throw new FilterException("Couldn't parse filter file " + fileName, e);
		}

		// Iterate over Match elements
		Iterator i = filterDoc.selectNodes("/FindBugsFilter/Match").iterator();
		while (i.hasNext()) {
			Element matchNode = (Element) i.next();

			AndMatcher matchMatcher = new AndMatcher();

			// Each match node must have either "class" or "classregex" attributes
			Matcher classMatcher = null;
			String classAttr = matchNode.valueOf("@class");
			if (!classAttr.equals("")) {
				classMatcher = new ClassMatcher(classAttr);
			} else {
				String classRegex = matchNode.valueOf("@classregex");
				if (!classRegex.equals(""))
					classMatcher = new ClassRegexMatcher(classRegex);
			}

			if (classMatcher == null)
				throw new FilterException("Match node must specify either class or classregex attribute");

			matchMatcher.addChild(classMatcher);

			if (DEBUG) System.out.println("Match node");

			// Iterate over child elements of Match node.
			Iterator j = matchNode.elementIterator();
			while (j.hasNext()) {
				Element child = (Element) j.next();
				Matcher matcher = getMatcher(child);
				matchMatcher.addChild(matcher);
			}

			// Add the Match matcher to the overall Filter
			this.addChild(matchMatcher);
		}

	}
 
Example 11
Project: joai-project   File: VirtualSearchFieldMapper.java   View Source Code Vote up 3 votes
private void addVirtualSearchTermDefinition(Element virtualSearchTermDefinition, String field) throws Exception {

		String term = virtualSearchTermDefinition.valueOf("@term");

		if (term == null || term.trim().length() == 0)

			throw new Exception("Error parsing document: element <virtualSearchTermDefinition> must have a single, non-empty 'term' attribute. Error found at " + virtualSearchTermDefinition.getPath());



		List virtualDefinitionElement = virtualSearchTermDefinition.selectNodes("Query/*");

		if (virtualDefinitionElement == null || virtualDefinitionElement.size() == 0)

			throw new Exception("Error parsing document: <virtualSearchTermDefinition> child element '<Query>' is empty or missing. Error found at " + virtualSearchTermDefinition.getPath());

		if (virtualDefinitionElement.size() > 1)

			throw new Exception("Error parsing document: element <virtualSearchTermDefinition> must contain a single child element within the '<Query>' element but contains more than one. Error found at " + virtualSearchTermDefinition.getPath());



		Query query = XMLQueryParser.getLuceneQuery((Element) virtualDefinitionElement.get(0), localQueryParser);



		setQuery(field, term, query);

	}