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.Iterator;
022: import javax.xml.namespace.QName;
023: import org.netbeans.modules.xml.wsdl.model.ReferenceableWSDLComponent;
024: import org.netbeans.modules.xml.xam.dom.Attribute;
025: import org.netbeans.modules.xslt.tmap.model.api.ExNamespaceContext;
026: import org.netbeans.modules.xslt.tmap.model.api.ParamType;
027: import org.netbeans.modules.xslt.tmap.model.api.TMapReference;
028: import org.netbeans.modules.xslt.tmap.model.api.TMapReferenceable;
029: import org.netbeans.modules.xslt.tmap.model.api.Variable;
030: import org.netbeans.modules.xslt.tmap.model.api.VariableDeclarator;
031: import org.netbeans.modules.xslt.tmap.model.api.VariableReference;
032: import org.netbeans.modules.xslt.tmap.model.api.WSDLReference;
033:
034: /**
035: *
036: * incapuslate all attribute logic.
037: * @author Vitaly Bychkov
038: * @author ads
039: * @version 1.0
040: */
041: public class AttributeAccess {
042:
043: private TMapComponentAbstract myComponent;
044:
045: public AttributeAccess(TMapComponentAbstract component) {
046: myComponent = component;
047: }
048:
049: Object getAttributeValueOf(Attribute attr, String stringValue) {
050: if (stringValue == null) {
051: return null;
052: }
053: Class clazz = attr.getType();
054: if (String.class.isAssignableFrom(clazz)) {
055: return stringValue;
056: } else if (ParamType.class.isAssignableFrom(clazz)) {
057: return ParamType.parseParamType(stringValue);
058: }
059: assert false;
060: return null;
061: }
062:
063: VariableReference getTMapVarReference(Attribute attr) {
064:
065: // readLock();
066: // try {
067: return resolveTMapVarReference(attr);
068: // }
069: // finally {
070: // readUnlock();
071: // }
072: }
073:
074: // <T extends BpelReferenceable> List<BpelReference<T>> getBpelReferenceList(
075: // Attribute attr, Class<T> type )
076: // {
077: // readLock();
078: // try {
079: // String str = getEntity().getAttribute(attr);
080: // return getBpelReferenceList(type, str, attr);
081: // }
082: // finally {
083: // readUnlock();
084: // }
085: // }
086: //
087: // private <T extends BpelReferenceable> List<BpelReference<T>>
088: // getBpelReferenceList( Class<T> type, String str, Attribute attr )
089: // {
090: // if (str == null) {
091: // return null;
092: // }
093: // StringTokenizer tokenizer = new StringTokenizer(str, " ");
094: // List<BpelReference<T>> list = new LinkedList<BpelReference<T>>();
095: // while (tokenizer.hasMoreTokens()) {
096: // String next = tokenizer.nextToken();
097: // BpelReference<T> ref = BpelReferenceBuilder.getInstance().build(
098: // type, getEntity(), next);
099: // BpelReferenceBuilder.getInstance().setAttribute(ref, attr);
100: // list.add(ref);
101: // }
102: // return Collections.unmodifiableList(list);
103: // }
104: void setTMapVarReference(Attribute attr, VariableReference ref) {
105: // writeLock();
106: // try {
107: VariableReference old = getTMapVarReference(attr);
108:
109: String str = ref.getRefString();
110: // PropertyUpdateEvent event = preUpdateAttribute(attr.getName(), old,
111: // ref );
112: setAttribute(attr, str);
113: // getEntity().postGlobalEvent(event);
114: // }
115: // catch (VetoException e) {
116: // assert false;
117: // }
118: // finally {
119: // writeUnlock();
120: // }
121: }
122:
123: private VariableReference resolveTMapVarReference(Attribute attr) {
124: if (getComponent().getAttribute(attr) == null) {
125: return null;
126: }
127: VariableReference ref = (VariableReference) TMapReferenceBuilder
128: .getInstance().build(VariableDeclarator.class,
129: getComponent(), attr);
130: return ref;
131: }
132:
133: public <T extends ReferenceableWSDLComponent> void setWSDLReference(
134: Attribute attr, WSDLReference<T> ref) {
135:
136: // TODO
137: ////// writeLock();
138: ////// try {
139: WSDLReference<T> old = getWSDLReference(attr, ref.getType());
140:
141: String str = ref.getRefString();
142:
143: boolean isQname = ((AttributesType) ref).getAttributeType() == AttributesType.AttrType.QNAME;
144: if (isQname) {
145: str = prepareQNameAttribute(ref.getQName());
146: }
147: /*
148: * ref = WSDLReferenceBuilder.getInstance().build( ref.getType() ,
149: * this , str )
150: * setting in event new created reference is more appropriate
151: * actually.
152: */
153: // TODO m | r
154: //// PropertyUpdateEvent event = preUpdateAttribute(attr.getName(), old,
155: //// ref );
156: setAttribute(attr, str);
157: // TODO m | r
158: //// getComponent().postGlobalEvent(event);
159: ////// }
160: ////// catch (VetoException e) {
161: ////// assert false;
162: ////// }
163: ////// finally {
164: ////// writeUnlock();
165: ////// }
166: }
167:
168: public <T extends ReferenceableWSDLComponent> WSDLReference<T> getWSDLReference(
169: Attribute attr, Class<T> clazz) {
170:
171: // TODO m
172: //// readLock();
173: //// try {
174: return resolveWSDLReference(attr, clazz);
175: // }
176: // finally {
177: // readUnlock();
178: // }
179: }
180:
181: private <T extends ReferenceableWSDLComponent> WSDLReference<T> resolveWSDLReference(
182: Attribute attr, Class<T> clazz) {
183:
184: TMapComponentAbstract opImpl = getComponent();
185:
186: if (getComponent().getAttribute(attr) == null) {
187: return null;
188: }
189:
190: WSDLReference<T> ref = WSDLReferenceBuilder.getInstance()
191: .build(clazz, getComponent(), attr);
192: return ref;
193: }
194:
195: /**
196: * Prepare XML for setting qName as value. 1) Corresponded namespace
197: * possibly should be added. 2) attribute value will be returned as String (
198: * prefix will be determined and value will be formed ).
199: */
200: private String prepareQNameAttribute(QName qName) {
201: String attributeValue;
202: String prefix = qName.getPrefix();
203: String localPart = qName.getLocalPart();
204: String uri = qName.getNamespaceURI();
205:
206: ExNamespaceContext context = getComponent()
207: .getNamespaceContext();
208:
209: String existedPrefix = context.getPrefix(uri);
210: if (existedPrefix != null) {
211: attributeValue = existedPrefix + ":" + localPart;
212: } else {
213: boolean isGoodPrefix = true;
214:
215: /*
216: * check for presence prefix already in context. If It exists then
217: * it have different uri ( becuase we don't have this uri here at
218: * all - cathed previous case in "if" ).
219: */
220: if ((prefix != null) && (prefix.length() > 0)) {
221: Iterator<String> iterator = context.getPrefixes();
222: while (iterator.hasNext()) {
223: String pref = iterator.next();
224: if (pref.equals(prefix)) {
225: isGoodPrefix = false;
226: break;
227: }
228: }
229: } else {
230: isGoodPrefix = false;
231: }
232:
233: try {
234: if (isGoodPrefix) {
235: context.addNamespace(prefix, uri);
236: } else {
237: prefix = context.addNamespace(uri);
238: }
239: } catch (InvalidNamespaceException e) {
240: /*
241: * we should not appear here - becuase we check presence of
242: * prefix..... ???? wrong prefix ( not NCName) ??? ( Qname
243: * doens't check correctness of prefix ).
244: */
245: assert false;
246: }
247:
248: // we have added namespace to context ,
249: // at least we set value of attribute
250: attributeValue = prefix + ":" + localPart;
251: }
252: return attributeValue;
253: }
254:
255: //==========================================================================
256:
257: private void setAttribute(Attribute attr, String str) {
258: getComponent().setAttribute(attr.getName(), attr, str);
259: }
260:
261: //==========================================================================
262: //
263: // Event framework.
264: //
265: //==========================================================================
266: //// private PropertyUpdateEvent preUpdateAttribute( String attrName,
267: //// Object oldValue, Object value ) throws VetoException
268: //// {
269: //// return preUpdateAttribute( getComponent() , attrName , oldValue , value );
270: //// }
271: ////
272: //// private PropertyUpdateEvent preUpdateAttribute( TMapComponentAbstract component,
273: //// String attrName, Object oldValue, Object value ) throws VetoException
274: //// {
275: //// component.checkDeleted();
276: //// PropertyUpdateEvent event = new PropertyUpdateEvent(component.getModel().
277: //// getSource(), component, attrName, oldValue, value);
278: //// component.getModel().preInnerEventNotify(event);
279: //// return event;
280: //// }
281: ////
282: //// private PropertyRemoveEvent preRemoveAttribute( String name, Object obj ) {
283: //// getComponent().checkDeleted();
284: //// PropertyRemoveEvent event = new PropertyRemoveEvent(getComponent().getModel()
285: //// .getSource(), getComponent(), name, obj);
286: ////
287: //// try {
288: //// getComponent().getModel().preInnerEventNotify(event);
289: //// return event;
290: //// }
291: //// catch (VetoException e) {
292: //// assert false;
293: //// }
294: //// return null;
295: //// }
296: ////
297: //==========================================================================
298:
299: private TMapComponentAbstract getComponent() {
300: return myComponent;
301: }
302:
303: }
|