Skip to main content
summaryrefslogtreecommitdiffstats
blob: 83e392960d2f9385706194b95cc754b4d0d0af48 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
 * Copyright (c) 2008 Oracle Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Oracle Corporation - initial API and implementation
 */
package org.eclipse.jst.jsf.apache.trinidad.tagsupport.converter.operations;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jst.pagedesigner.dtmanager.converter.ITransformOperation;
import org.eclipse.jst.pagedesigner.dtmanager.converter.operations.AbstractTransformOperation;
import org.eclipse.jst.pagedesigner.dtmanager.converter.operations.TransformOperationFactory;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Extends AbstractTransformOperation to supply extra convenience methods.
 * 
 * @author Ian Trimble - Oracle
 */
public abstract class AbstractTrinidadTransformOperation extends AbstractTransformOperation {

	/**
	 * Appends the specified attribute with the specified value to the specified
	 * Element instance.
	 * 
	 * @param element Element instance to append attribute to.
	 * @param attributeName Name of attribute to be appended.
	 * @param attributeValue Value of attribute to be appended.
	 */
	protected void appendAttribute(
			Element element, String attributeName, String attributeValue) {
		ITransformOperation operation =
			TransformOperationFactory.getInstance().getTransformOperation(
					TransformOperationFactory.OP_CreateAttributeOperation,
					new String[]{attributeName, attributeValue});
		operation.transform(null, element);
	}

	/**
	 * Gets a child Element of the specified parent Element that has the node
	 * name "facet" and the specified value of the "name" attribute.
	 * 
	 * @param srcElement Parent Element instance.
	 * @param facetName Name of the facet Element for which to search.
	 * @return Child Element that is a facet with the specified name.
	 */
	protected Element getChildFacetByName(Element srcElement, String facetName) {
		Element element = null;
		@SuppressWarnings("unchecked")
		List facets = getChildElements(srcElement, "facet"); //$NON-NLS-1$
		@SuppressWarnings("unchecked")
		Iterator itFacets = facets.iterator();
		while (itFacets.hasNext()) {
			Element facet = (Element)itFacets.next();
			String facetAttrName = facet.getAttribute("name"); //$NON-NLS-1$
			if (facetAttrName != null && facetAttrName.equals(facetName)) {
				element = facet;
				break;
			}
		}
		return element;
	}

	/**
	 * Gets a list of child Elements of the specified parent Element, skipping
	 * any "facet" Elements.
	 * 
	 * @param srcElement Parent Element instance.
	 * @return List of child Elements of the specified parent Element that does
	 * not include any child "facet" Elements.
	 */
	protected List<Element> getChildElementsSkipFacets(Element srcElement) {
		List<Element> childElementsList = new ArrayList<Element>();
		NodeList childNodes = srcElement.getChildNodes();
		for (int i = 0; i < childNodes.getLength(); i++) {
			Node childNode = childNodes.item(i);
			if (childNode.getNodeType() == Node.ELEMENT_NODE) {
				if (!childNode.getLocalName().equals("facet")) { //$NON-NLS-1$
					childElementsList.add((Element)childNode);
				}
			}
		}
		return childElementsList;
	}

	/**
	 * Returns a List of child Node instances that will be added by the
	 * "CopyChildrenOperation" ITransformOperation. This can be useful in
	 * determining if any Node instances will, in fact, be copied. 
	 * 
	 * @param srcElement Source Element instance to query for child Node
	 * instances.
	 * @return A List of child Node instances that will be added by the
	 * "CopyChildrenOperation" ITransformOperation.
	 */
	protected List<Node> getCopyChildrenNodes(Element srcElement) {
		List<Node> children = new ArrayList<Node>();
		if (srcElement != null) {
			NodeList childNodes = srcElement.getChildNodes();
			for (int i = 0; i < childNodes.getLength(); i++) {
				Node childNode = childNodes.item(i);
				short childNodeType = childNode.getNodeType();
				if (childNodeType == Node.ELEMENT_NODE ||
						childNodeType == Node.TEXT_NODE ||
						childNodeType == Node.CDATA_SECTION_NODE) {
					children.add(childNode);
				}
			}
		}
		return children;
	}

}

Back to the top