Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ca8e9eab23afcaf71001f4498e1dddb4c713e04e (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
 * 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;
	}

	/**
	 * Calculates required style class value, based on a specified base style
	 * class and the value of the source Element instance's "styleClass"
	 * attribute.
	 * 
	 * @param baseClass Specified base style class (may be null).
	 * @param srcElement Source Element instance (may be null).
	 * @return Required style class value, to be set as "class" attribute (may
	 * be null).
	 */
	protected String calculateStyleClass(String baseClass, Element srcElement) {
		String styleClass = null;
		if (baseClass != null) {
			styleClass = baseClass;
		}
		if (srcElement != null) {
			String srcStyleClass = srcElement.getAttribute("styleClass"); //$NON-NLS-1$
			if (srcStyleClass != null && srcStyleClass.length() > 0) {
				if (styleClass.length() > 0) {
					styleClass += "," + srcStyleClass; //$NON-NLS-1$
				} else {
					styleClass = srcStyleClass;
				}
			}
		}
		return styleClass;
	}

}

Back to the top