001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.xslt.tmap.model.impl;
020:
021: import java.util.Collection;
022: import java.util.LinkedList;
023: import java.util.List;
024: import org.netbeans.modules.xml.wsdl.model.Part;
025: import org.netbeans.modules.xml.xam.AbstractComponent;
026: import org.netbeans.modules.xml.xam.Reference;
027: import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
028: import org.netbeans.modules.xml.xam.dom.Attribute;
029: import org.netbeans.modules.xslt.tmap.model.api.Invoke;
030: import org.netbeans.modules.xslt.tmap.model.api.MappedReference;
031: import org.netbeans.modules.xslt.tmap.model.api.Operation;
032: import org.netbeans.modules.xslt.tmap.model.api.Param;
033: import org.netbeans.modules.xslt.tmap.model.api.TMapAttributes;
034: import org.netbeans.modules.xslt.tmap.model.api.TMapReference;
035: import org.netbeans.modules.xslt.tmap.model.api.TMapReferenceable;
036: import org.netbeans.modules.xslt.tmap.model.api.Transform;
037: import org.netbeans.modules.xslt.tmap.model.api.Variable;
038: import org.netbeans.modules.xslt.tmap.model.api.VariableDeclarator;
039: import org.netbeans.modules.xslt.tmap.model.api.VariableReference;
040:
041: /**
042: *
043: * @author Vitaly Bychkov
044: * @version 1.0
045: */
046: public class TMapReferenceBuilder {
047:
048: private TMapReferenceBuilder() {
049: myCollection = new LinkedList<TMapReferenceFactory>();
050: myCollection.add(new VariableReferenceFactory());
051: }
052:
053: public static TMapReferenceBuilder getInstance() {
054: return INSTANCE;
055: }
056:
057: public <T extends TMapReferenceable> TMapReference<T> build(
058: Class<T> clazz, AbstractDocumentComponent component,
059: Attribute attr) {
060: TMapReference<T> ref = build(clazz, component, component
061: .getAttribute(attr));
062: if (ref instanceof MappedReference) {
063: ((MappedReference) ref).setAttribute(attr);
064: }
065: return ref;
066: }
067:
068: public <T extends TMapReferenceable> TMapReference<T> build(
069: Class<T> clazz, AbstractComponent component,
070: String refString) {
071: if (refString == null) {
072: return null;
073: }
074: for (TMapReferenceFactory resolver : myCollection) {
075: if (resolver.isApplicable(clazz)) {
076: return resolver.createUnresolvedReference(clazz,
077: component, refString);
078: }
079: }
080: return null;
081: }
082:
083: public <T extends TMapReferenceable> TMapReference<T> build(
084: T target, Attribute targetAttr, Class<T> clazz,
085: AbstractComponent component, Part part) {
086: for (TMapReferenceFactory resolver : myCollection) {
087: if (resolver.isApplicable(clazz)) {
088: return resolver.create(target, targetAttr, clazz,
089: component, part);
090: }
091: }
092: return null;
093: }
094:
095: public void setAttribute(TMapReference ref, Attribute attr) {
096: if (ref instanceof MappedReference) {
097: ((MappedReference) ref).setAttribute(attr);
098: }
099: }
100:
101: public AttributesType.AttrType getAttributeType(Attribute attr) {
102: return AttributesType.AttrType.NCNAME;
103: }
104:
105: interface TMapResolver {
106: <T extends TMapReferenceable> T resolve(AbstractReference<T> ref);
107:
108: <T extends TMapReferenceable> boolean haveRefString(
109: Reference<T> ref, T component);
110: }
111:
112: private static final TMapReferenceBuilder INSTANCE = new TMapReferenceBuilder();
113:
114: private Collection<TMapReferenceFactory> myCollection;
115: }
116:
117: interface TMapReferenceFactory extends
118: TMapReferenceBuilder.TMapResolver {
119:
120: <T extends TMapReferenceable> boolean isApplicable(Class<T> clazz);
121:
122: <T extends TMapReferenceable> TMapReference<T> create(T target,
123: Attribute targetAttr, Class<T> clazz,
124: AbstractComponent component, Part part);
125:
126: <T extends TMapReferenceable> TMapReference<T> createUnresolvedReference(
127: Class<T> clazz, AbstractComponent component,
128: String refString);
129:
130: <T extends TMapReferenceable> TMapReference<T> create(T target,
131: Class<T> clazz, AbstractComponent component,
132: String refString);
133:
134: AttributesType.AttrType getAttributeType();
135: }
136:
137: abstract class AbstractTMapVarReferenceFactory implements
138: TMapReferenceFactory {
139:
140: public <T extends TMapReferenceable> TMapReference<T> create(
141: T target, Class<T> clazz, AbstractComponent component,
142: String refString) {
143: assert target instanceof VariableDeclarator;
144: return (TMapReference<T>) new VariableReferenceImpl(
145: (VariableDeclarator) target, component, refString, this );
146: }
147:
148: public AttributesType.AttrType getAttributeType() {
149: return AttributesType.AttrType.NCNAME;
150: }
151:
152: public <T extends TMapReferenceable> TMapReference<T> createUnresolvedReference(
153: Class<T> clazz, AbstractComponent component,
154: String refString) {
155: VariableReference varRef = new VariableReferenceImpl(component,
156: refString, this );
157:
158: return (TMapReference<T>) new VariableReferenceImpl(component,
159: refString, this );
160: }
161:
162: public <T extends TMapReferenceable> boolean haveRefString(
163: Reference<T> ref, T component) {
164: assert component instanceof VariableDeclarator;
165: if (ref instanceof VariableReference
166: && component instanceof VariableDeclarator) {
167: return haveInputVarRef((VariableReference) ref,
168: (VariableDeclarator) component)
169: || haveOutputVarRef((VariableReference) ref,
170: (VariableDeclarator) component);
171: }
172: return false;
173: }
174:
175: private boolean compare(Variable var, VariableReference varRef) {
176: boolean isEqual = false;
177: if (var != null && varRef != null) {
178: String refString = varRef.getRefString();
179: if (refString != null) {
180: int lastDotIndex = refString.lastIndexOf(".");
181: refString = lastDotIndex > 0 ? refString.substring(0,
182: lastDotIndex) : null;
183: }
184:
185: isEqual = refString != null
186: && refString.equals(var.getName());
187: }
188:
189: return isEqual;
190: }
191:
192: private boolean haveInputVarRef(VariableReference ref,
193: VariableDeclarator component) {
194: return compare(component.getInputVariable(), ref);
195: }
196:
197: private boolean haveOutputVarRef(VariableReference ref,
198: VariableDeclarator component) {
199: return compare(component.getOutputVariable(), ref);
200: }
201:
202: public <T extends TMapReferenceable> TMapReference<T> create(
203: T target, Attribute targetAttr, Class<T> clazz,
204: AbstractComponent component, Part part) {
205: assert target instanceof VariableDeclarator;
206: assert TMapAttributes.INPUT_VARIABLE.equals(targetAttr)
207: || TMapAttributes.OUTPUT_VARIABLE.equals(targetAttr);
208:
209: String refString = ((VariableDeclarator) target)
210: .getAttribute(targetAttr);
211: refString = refString == null || part == null ? null
212: : VariableReferenceImpl.getVarRefString(refString, part
213: .getName());
214:
215: return (TMapReference<T>) (refString == null ? null
216: : new VariableReferenceImpl(
217: (VariableDeclarator) target, component,
218: refString, this ));
219: }
220: }
221:
222: class VariableReferenceFactory extends AbstractTMapVarReferenceFactory {
223:
224: public <T extends TMapReferenceable> boolean isApplicable(
225: Class<T> clazz) {
226: return VariableDeclarator.class.isAssignableFrom(clazz);
227: }
228:
229: public <T extends TMapReferenceable> T resolve(
230: AbstractReference<T> ref) {
231: AbstractDocumentComponent parentComponent = (AbstractDocumentComponent) ref
232: .getParent();
233: String refString = ref.getRefString();
234: Class<T> clazz = ref.getType();
235:
236: if (!(ref instanceof VariableReference)) {
237: return null;
238: }
239:
240: String varName = VariableReferenceImpl.getVarName(refString);
241:
242: Operation operation = null;
243: if (parentComponent instanceof Param) {
244: parentComponent = (AbstractDocumentComponent) parentComponent
245: .getParent();
246: }
247:
248: if (parentComponent instanceof Transform) {
249: operation = (Operation) ((Transform) parentComponent)
250: .getParent();
251: VariableDeclarator varCont = resolveByOperation(operation,
252: varName);
253: return clazz.cast(varCont);
254: }
255:
256: return null;
257: }
258:
259: private VariableDeclarator resolveByOperation(Operation operation,
260: String varName) {
261: if (operation == null || varName == null) {
262: return null;
263: }
264: if (isVariableDeclarator(operation, varName)) {
265: return operation;
266: }
267:
268: List<Invoke> invokes = operation.getInvokes();
269: if (invokes != null && invokes.size() > 0) {
270: for (Invoke invoke : invokes) {
271: if (isVariableDeclarator(invoke, varName)) {
272: return invoke;
273: }
274: }
275: }
276:
277: return null;
278: }
279:
280: private boolean isVariableDeclarator(
281: VariableDeclarator varContainer, String varName) {
282: boolean isDeclarator = false;
283: if (varContainer == null || varName == null) {
284: return isDeclarator;
285: }
286:
287: Variable tmpVar = varContainer.getInputVariable();
288: isDeclarator = tmpVar != null
289: && varName.equals(tmpVar.getName());
290:
291: if (!isDeclarator) {
292: tmpVar = varContainer.getOutputVariable();
293: isDeclarator = tmpVar != null
294: && varName.equals(tmpVar.getName());
295: }
296:
297: return isDeclarator;
298: }
299:
300: }
|