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

The following are Jave code examples for showing how to use getPath() 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: SchemaHelper.java   View Source Code Vote up 6 votes
/**
 *  Gets the qualified element name of the repeatingComplexSingleton child of
 *  the node specified by the provided path, or an empty string if such a child
 *  does not exist.
 *
 * @param  xpath  NOT YET DOCUMENTED
 * @return        The repeatingComplexSingletonChildName value
 */
public String getRepeatingComplexSingletonChildName(String xpath) {
	String normalizedXPath = XPathUtils.normalizeXPath(xpath);
	Node instanceDocNode = getInstanceDocNode(normalizedXPath);
	if (instanceDocNode != null && instanceDocNode.getNodeType() == org.dom4j.Node.ELEMENT_NODE) {
		List children = ((Element) instanceDocNode).elements();
		if (children.size() == 1) {
			Element childElement = (Element) children.get(0);
			String childPath = childElement.getPath();
			SchemaNode schemaNode = getSchemaNode(childPath);
			if (schemaNode != null && schemaNode.getTypeDef().isComplexType() && isRepeatingElement(childPath))
				return childElement.getQualifiedName();
		}
	}
	return "";
}
 
Example 2
Project: joai-project   File: DcsViewRecord.java   View Source Code Vote up 6 votes
private void renderMaster () throws Exception {
	Element base = df.createElement("div");
	/* base.addAttribute ("class", "level--1"); */
	List elements = instanceDocument.getRootElement().elements();
	// prtln("batchRenderAndWrite found " + elements.size() + " elements");
	for (Iterator i = elements.iterator(); i.hasNext(); ) {
		Element child = (Element) i.next();
		String xpath = child.getPath();
		String pageName = XPathUtils.getNodeName(xpath);
		Element include = base.addElement("jsp__include");
		include.addAttribute("page", getMasterComponentPath (pageName));
	}
	
	File dest = getJspDest (null);
	if (!writeJsp(base, dest, this.getMasterJspHeader())) {
		throw new Exception ("renderMaster(): failed to write jsp to " + dest);
	}
	
}
 
Example 3
Project: joai-project   File: AutoForm.java   View Source Code Vote up 6 votes
/**
 *  Create a jsp file for each top-level element of the schema
 *
 * @exception  Exception  NOT YET DOCUMENTED
 */
public void batchRenderAndWrite() throws Exception {
	List elements = instanceDocument.getRootElement().elements();
	// prtln("batchRenderAndWrite found " + elements.size() + " elements");
	for (Iterator i = elements.iterator(); i.hasNext(); ) {
		Element child = (Element) i.next();
		String xpath = child.getPath();
		String pageName = XPathUtils.getNodeName(xpath);
		// prtln(pageName + " (" + xpath + ")");

		Element e = render(xpath);
		if (e != null &&
			!writeJsp(e, getBatchJspDest(pageName), this.getComponentJspHeader())) {
				throw new Exception ("batchRenderAndWrite could not write jsp for " + pageName);
		}
	}
}
 
Example 4
Project: lams   File: JPAOverriddenAnnotationReader.java   View Source Code Vote up 6 votes
private void addTargetClass(Element element, AnnotationDescriptor ad, String nodeName, XMLContext.Default defaults) {
	String className = element.attributeValue( nodeName );
	if ( className != null ) {
		Class clazz;
		try {
			clazz = ReflectHelper.classForName(
					XMLContext.buildSafeClassName( className, defaults ), this.getClass()
			);
		}
		catch ( ClassNotFoundException e ) {
			throw new AnnotationException(
					"Unable to find " + element.getPath() + " " + nodeName + ": " + className, e
			);
		}
		ad.setValue( getJavaAttributeNameFromXMLOne( nodeName ), clazz );
	}
}
 
Example 5
Project: lams   File: JPAOverriddenAnnotationReader.java   View Source Code Vote up 6 votes
private void getMapKeyClass(List<Annotation> annotationList, Element element, XMLContext.Default defaults) {
	String nodeName = "map-key-class";
	Element subelement = element != null ? element.element( nodeName ) : null;
	if ( subelement != null ) {
		String mapKeyClassName = subelement.attributeValue( "class" );
		AnnotationDescriptor ad = new AnnotationDescriptor( MapKeyClass.class );
		if ( StringHelper.isNotEmpty( mapKeyClassName ) ) {
			Class clazz;
			try {
				clazz = ReflectHelper.classForName(
						XMLContext.buildSafeClassName( mapKeyClassName, defaults ),
						this.getClass()
				);
			}
			catch ( ClassNotFoundException e ) {
				throw new AnnotationException(
						"Unable to find " + element.getPath() + " " + nodeName + ": " + mapKeyClassName, e
				);
			}
			ad.setValue( "value", clazz );
		}
		annotationList.add( AnnotationFactory.create( ad ) );
	}
}
 
Example 6
Project: lams   File: JPAOverriddenAnnotationReader.java   View Source Code Vote up 6 votes
private Column getColumn(Element element, boolean isMandatory, Element current) {
	//Element subelement = element != null ? element.element( "column" ) : null;
	if ( element != null ) {
		AnnotationDescriptor column = new AnnotationDescriptor( Column.class );
		copyStringAttribute( column, element, "name", false );
		copyBooleanAttribute( column, element, "unique" );
		copyBooleanAttribute( column, element, "nullable" );
		copyBooleanAttribute( column, element, "insertable" );
		copyBooleanAttribute( column, element, "updatable" );
		copyStringAttribute( column, element, "column-definition", false );
		copyStringAttribute( column, element, "table", false );
		copyIntegerAttribute( column, element, "length" );
		copyIntegerAttribute( column, element, "precision" );
		copyIntegerAttribute( column, element, "scale" );
		return (Column) AnnotationFactory.create( column );
	}
	else {
		if ( isMandatory ) {
			throw new AnnotationException( current.getPath() + ".column is mandatory. " + SCHEMA_VALIDATION );
		}
		return null;
	}
}
 
Example 7
Project: lams   File: JPAOverriddenAnnotationReader.java   View Source Code Vote up 6 votes
private static void copyIntegerAttribute(AnnotationDescriptor annotation, Element element, String attributeName) {
	String attribute = element.attributeValue( attributeName );
	if ( attribute != null ) {
		String annotationAttributeName = getJavaAttributeNameFromXMLOne( attributeName );
		annotation.setValue( annotationAttributeName, attribute );
		try {
			int length = Integer.parseInt( attribute );
			annotation.setValue( annotationAttributeName, length );
		}
		catch ( NumberFormatException e ) {
			throw new AnnotationException(
					element.getPath() + attributeName + " not parseable: " + attribute + " (" + SCHEMA_VALIDATION + ")"
			);
		}
	}
}
 
Example 8
Project: joai-project   File: DocMap.java   View Source Code Vote up 5 votes
/**
 *  Insert an element in it's parent element using compositor. The compositor
 *  can provide information that allows us to place the new element at the
 *  correct place in the parent element.
 *
 * @param  element        NOT YET DOCUMENTED
 * @param  parent         NOT YET DOCUMENTED
 * @exception  Exception  NOT YET DOCUMENTED
 */
private void untargetedInsert(Element element, Element parent) throws Exception {
	// prtln("\n untargetedInsert()");
	// prtln("\telement name: " + element.getQualifiedName());
	// prtln ("\telement to insert: " + Dom4jUtils.prettyPrint(element) + "\n");
	String parentPath = parent.getPath();

	if (schemaHelper == null || !schemaHelper.hasSequenceCompositor(parentPath)) {

		// prtln ("no help from schemaHelper - inserting element at end of parent");
		if (schemaHelper == null)
			prtln("SchemaHelper is NULL");
		parent.add(element);
		return;
	}
	else {
		Sequence compositor = (Sequence) schemaHelper.getCompositor(parentPath);
		String compositorMemberName = resolveElementName(parentPath, element);
		int memberIndex = compositor.getIndexOfMember(compositorMemberName);

		/*
		 insert element immediately following other like-named elements, or just ahead
		 of the following member if no like-named elements exist.
		 when the childMemberIndex is greater than the (target) memberIndex, then it is
		 time to insert.
		*/
		List elements = parent.elements();
		for (int i = 0; i < elements.size(); i++) {
			Element child = (Element) elements.get(i);
			String childCompositorMemberName = resolveElementName(parentPath, child);
			int childMemberIndex = compositor.getIndexOfMember(childCompositorMemberName);

			if (childMemberIndex > memberIndex) {
				elements.add(i, element);
				return;
			}
		}
		parent.add(element);
	}
}
 
Example 9
Project: joai-project   File: DocMap.java   View Source Code Vote up 5 votes
/**
 *  Insert an element into the parent immediately following the targetIndex.
 *  For example, if a parent element had three children elements, and the
 *  targetIndex were 2, then the new element would be inserted after the second
 *  element and before the third. NOTE: throws exception if targetIndex is
 *  greater than one and an element corresponding to targetIndex does not
 *  exist.
 *
 * @param  element        element to be inserted
 * @param  parent         parent in which to be inserted
 * @param  targetIndex    index after which to be inserted
 * @exception  Exception  if targetIndex is greater than one and an element
 *      corresponding to targetIndex does not exist.
 */
private void targetedInsert(Element element, Element parent, int targetIndex) throws Exception {
	prtln("\ntargetedInsert()");
	// prtln ("\telement to insert: " + Dom4jUtils.prettyPrint(element));
	String parentPath = parent.getPath();
	String targetName = resolveElementName(parentPath, element);
	prtln("  ... target info -- name: " + targetName + "  targetIndex: " + targetIndex);
	int n = 0;

	List elements = parent.elements();
	for (int i = 0; i < elements.size(); i++) {
		Element child = (Element) elements.get(i);
		String childName = resolveElementName(parentPath, child);
		if (childName.equals(targetName)) {
			// increment index, and look for target
			n++;
			if (n == targetIndex - 1) {
				// insert element after elements[i]
				if (i + 1 < elements.size())
					elements.add(i + 1, element);
				else
					elements.add(element);
				return;
			}
		}
	}

	// when the editor needs to create a new repeating node for an empty parent, it supplies an index of 1 ...
	if (targetIndex == 1) {
		untargetedInsert(element, parent);
	}
	else {
		throw new Exception("targetedInsert failed to find target");
	}

}
 
Example 10
Project: joai-project   File: DocMap.java   View Source Code Vote up 5 votes
/**
 *  Put sequence elements in the order specified by the sequence compositor.
 *
 * @param  parent  NOT YET DOCUMENTED
 */
public void orderSequenceElements(Element parent) {
	String xpath = parent.getPath();
	List order = schemaHelper.getChildrenOrder(xpath);

	// just as a safeguard, don't reorder if there is only one child
	// for the schemaNode, since parent is probably a repeating element
	// and reordering would wipe out all but one child
	if (order.size() < 2)
		return;

	// map element name to a list of elements to accomodate repeating elements
	Map elementMap = new HashMap();
	List elements = parent.elements();
	for (int i = elements.size() - 1; i > -1; i--) {
		Element e = (Element) elements.get(i);
		String tagName = e.getName();
		List items = (List) elementMap.get(tagName);
		if (items == null)
			items = new ArrayList();
		items.add(0, e.detach()); // add to beginning to maintain doc ordering
		elementMap.put(tagName, items);
	}

	for (Iterator i = order.iterator(); i.hasNext(); ) {
		String name = (String) i.next();
		elements = (List) elementMap.get(name);
		if (elements != null) {
			for (Iterator ii = elements.iterator(); ii.hasNext(); ) {
				parent.add((Element) ii.next());
			}
		}
	}
}
 
Example 11
Project: joai-project   File: DocumentPruner.java   View Source Code Vote up 5 votes
/**
 *  Gets the attributePath attribute of the DocumentPruner class
 *
 * @param  a  NOT YET DOCUMENTED
 * @return    The attributePath value
 */
private static String getAttributePath(Attribute a) {

	Element parent = a.getParent();
	if (parent == null) {
		prtln(" ... parent is null!");
	}
	return parent.getPath() + "/@" + a.getQualifiedName();
}
 
Example 12
Project: joai-project   File: SchemEditValidator.java   View Source Code Vote up 5 votes
/**
 *  Removes empty elements of repeating fields.<p>
 *
 *  DISABLED 5/11/04 - the repository will contain non-valid elements, so we
 *  don't worry so much about getting rid of the empty elements (many of which
 *  will be invalid due to the "stringTextType" convention) Removes empty child
 *  elements from each repeating field in the toPrune list. Children are pruned
 *  if they are empty and their occurance attribute allows them to be deleted.
 */
protected void pruneRepeatingFields() {
	prtln ("\nPruneRepeatingFields()");
	List toPruneList = sef.getRepeatingFieldsToPrune();

	// for each repeating field on the toPrune list
	for (Iterator p = toPruneList.iterator(); p.hasNext(); ) {
		String parentPath = (String) p.next();
		prtln ("\t parent: " + parentPath);
		Element parent = (Element) docMap.selectSingleNode(parentPath);
		if (parent == null) {
			prtln("pruneRepeatingFields(): Element not found for " + parentPath);
			continue;
		}
		List children = parent.elements();
		SchemaNode schemaNode = schemaHelper.getSchemaNode(XPathUtils.normalizeXPath(parentPath));
		if (schemaNode == null) {
			prtln("pruneRepeatingFields() couldn't find SchemaNode");
			continue;
		}

		for (int i = children.size() - 1; i > -1; i--) {
			Element child = (Element) children.get(i);
			String childName = child.getName();
			prtln ("\t     childName: " + childName);
			if (!Dom4jUtils.isEmpty(child)) {
				continue;
			}
			if (parent.elements().size() >= schemaNode.getMinOccurs()) {
				String childPath = child.getPath();
				parent.remove(child);
			}
		}
		parent.setText("");
	}
	sef.clearRepeatingFieldsToPrune();
}
 
Example 13
Project: lams   File: JPAOverriddenAnnotationReader.java   View Source Code Vote up 5 votes
private EntityListeners getEntityListeners(Element tree, XMLContext.Default defaults) {
	Element element = tree != null ? tree.element( "entity-listeners" ) : null;
	if ( element != null ) {
		List<Class> entityListenerClasses = new ArrayList<Class>();
		for ( Element subelement : (List<Element>) element.elements( "entity-listener" ) ) {
			String className = subelement.attributeValue( "class" );
			try {
				entityListenerClasses.add(
						ReflectHelper.classForName(
								XMLContext.buildSafeClassName( className, defaults ),
								this.getClass()
						)
				);
			}
			catch ( ClassNotFoundException e ) {
				throw new AnnotationException(
						"Unable to find " + element.getPath() + ".class: " + className, e
				);
			}
		}
		AnnotationDescriptor ad = new AnnotationDescriptor( EntityListeners.class );
		ad.setValue( "value", entityListenerClasses.toArray( new Class[entityListenerClasses.size()] ) );
		return AnnotationFactory.create( ad );
	}
	else if ( defaults.canUseJavaAnnotations() ) {
		return getPhysicalAnnotation( EntityListeners.class );
	}
	else {
		return null;
	}
}
 
Example 14
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 15
Project: joai-project   File: FrameworkTester.java   View Source Code Vote up 4 votes
void addRequiredPaths (Element e) {
	String path = e.getPath();
	SchemaNode schemaNode = this.framework.getSchemaHelper().getSchemaNode (path);
	prtln (schemaNode.toString());
}
 
Example 16
Project: lams   File: ElementWrapper.java   View Source Code Vote up 4 votes
public String getPath(Element element) {
	return element.getPath( element );
}
 
Example 17
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);

	}