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 org.netbeans.modules.xml.wsdl.model.Part;
022: import org.netbeans.modules.xml.wsdl.model.ReferenceableWSDLComponent;
023: import org.netbeans.modules.xml.xam.AbstractComponent;
024: import org.netbeans.modules.xml.xam.Reference;
025: import org.netbeans.modules.xml.xam.dom.Attribute;
026: import org.netbeans.modules.xslt.tmap.model.api.Variable;
027: import org.netbeans.modules.xslt.tmap.model.api.VariableDeclarator;
028: import org.netbeans.modules.xslt.tmap.model.api.VariableReference;
029: import org.netbeans.modules.xslt.tmap.model.api.WSDLReference;
030:
031: /**
032: *
033: * @author Vitaly Bychkov
034: * @version 1.0
035: */
036: public class VariableReferenceImpl extends
037: TMapReferenceImpl<VariableDeclarator> implements
038: VariableReference {
039: public static final String DOT = "."; // NOI18N
040:
041: VariableReferenceImpl(VariableDeclarator target,
042: AbstractComponent parent, String value,
043: TMapReferenceBuilder.TMapResolver resolver) {
044: super (target, VariableDeclarator.class, parent, value, resolver);
045: }
046:
047: VariableReferenceImpl(AbstractComponent parent, String value,
048: TMapReferenceBuilder.TMapResolver resolver) {
049: super (VariableDeclarator.class, parent, value, resolver);
050: }
051:
052: public Variable getReferencedVariable() {
053: VariableDeclarator varContainer = get();
054: if (varContainer == null) {
055: return null;
056: }
057:
058: String varName = getVarName();
059: if (varName == null) {
060: return null;
061: }
062:
063: Variable var = varContainer.getInputVariable();
064: if (var == null || !varName.equals(var.getName())) {
065: var = varContainer.getOutputVariable();
066: var = var != null && varName.equals(var.getName()) ? var
067: : null;
068: }
069: return var;
070: }
071:
072: public Reference[] getReferences() {
073: return new Reference[] { this , getPart() };
074: }
075:
076: public <T extends ReferenceableWSDLComponent> WSDLReference<T> createWSDLReference(
077: T target, Class<T> type) {
078: // TODO m | r
079: ////// readLock();
080: ////// try {
081: return WSDLReferenceBuilder.getInstance().build(target, type,
082: (TMapComponentAbstract) this .get());
083: ////// }
084: ////// finally {
085: ////// readUnlock();
086: ////// }
087: }
088:
089: public void setPart(WSDLReference<Part> part) {
090: Attribute attr = getAttribute();
091:
092: // TODO
093: ////// writeLock();
094: ////// try {
095: TMapComponentAbstract component = (TMapComponentAbstract) getParent();
096: if (component != null) {
097: WSDLReference<Part> old = getPart();
098:
099: String partRefStr = part.getRefString();
100: String attrValue = component.getAttribute(getAttribute());
101:
102: Variable var = getReferencedVariable();
103: String varName = var == null ? null : var.getName();
104: assert varName != null;
105:
106: // TODO m | r
107: //// PropertyUpdateEvent event = preUpdateAttribute(attr.getName(), old,
108: //// ref );
109:
110: component.setAttribute(attr.getName(), attr,
111: getVarRefString(varName, partRefStr));
112: // TODO m | r
113: //// getComponent().postGlobalEvent(event);
114: }
115: ////// }
116: ////// catch (VetoException e) {
117: ////// assert false;
118: ////// }
119: ////// finally {
120: ////// writeUnlock();
121: ////// }
122: }
123:
124: public WSDLReference<Part> getPart() {
125: Attribute attr = getAttribute();
126: TMapComponentAbstract component = (TMapComponentAbstract) getParent();
127:
128: if (component == null || component.getAttribute(attr) == null) {
129: return null;
130: }
131:
132: WSDLReference<Part> ref = WSDLReferenceBuilder.getInstance()
133: .build(Part.class, component, attr);
134: return ref;
135:
136: // Variable var = getReferenced();
137: // String refStr = getRefString();
138: // String partName = null;
139: // int dotIndex = refStr == null ? -1 : refStr.lastIndexOf(".");
140: // if (dotIndex > 0) {
141: // partName = refStr.substring(dotIndex+1);
142: // }
143: //
144: // if (partName == null) {
145: // return null;
146: // }
147: //
148: // Message message = null;
149: // if (var != null) {
150: // WSDLReference<Message> messageRef = var.getMessage();
151: // message = messageRef == null ? null : messageRef.get();
152: // }
153: //
154: // Collection<Part> parts = null;
155: // if (message != null) {
156: // parts = message.getParts();
157: // }
158: //
159: // if (parts != null && parts.size() > 0) {
160: // for (Part part : parts) {
161: // if (part != null && partName.equals(part.getName())) {
162: // return part;
163: // }
164: // }
165: // }
166: //
167: // return null;
168: }
169:
170: public static String getVarRefString(String varName, String partRef) {
171: return varName + DOT + partRef;
172: }
173:
174: public static String getVarName(String refStr) {
175: String varName = null;
176: int dotIndex = refStr == null ? -1 : refStr.lastIndexOf(DOT);
177:
178: if (dotIndex > 0) {
179: varName = refStr.substring(0, dotIndex);
180: }
181:
182: return varName;
183: }
184:
185: public static String getPartName(String refStr) {
186: String partName = null;
187: int dotIndex = refStr == null ? -1 : refStr.lastIndexOf(DOT);
188:
189: if (dotIndex > 0 && dotIndex < refStr.length() - 1) {
190: partName = refStr.substring(dotIndex + 1);
191: }
192:
193: return partName;
194: }
195:
196: private String getVarName() {
197: String varName = null;
198: String refStr = getRefString();
199: int dotIndex = refStr == null ? -1 : refStr.lastIndexOf(DOT);
200:
201: if (dotIndex > 0) {
202: varName = refStr.substring(0, dotIndex);
203: }
204:
205: return varName;
206: }
207:
208: }
|