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.nodes.properties;
020:
021: import javax.xml.namespace.NamespaceContext;
022: import javax.xml.namespace.QName;
023: import org.netbeans.api.project.FileOwnerQuery;
024: import org.netbeans.api.project.Project;
025: import org.netbeans.api.project.ProjectUtils;
026: import org.netbeans.api.project.SourceGroup;
027: import org.netbeans.api.project.Sources;
028: import org.netbeans.modules.xml.schema.model.SchemaComponent;
029: import org.netbeans.modules.xml.schema.model.SchemaModel;
030: import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
031: import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
032: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
033: import org.openide.filesystems.FileObject;
034: import org.openide.util.Lookup;
035: import org.netbeans.modules.xml.xam.Component;
036: import org.netbeans.modules.xml.xam.Model;
037: import org.netbeans.modules.xml.xam.ModelSource;
038: import org.netbeans.modules.xml.xam.Named;
039: import org.netbeans.modules.xml.xam.Reference;
040: import org.netbeans.modules.xslt.tmap.model.api.TMapComponent;
041: import org.netbeans.modules.xslt.tmap.model.api.VariableDeclarator;
042: import org.netbeans.modules.xslt.tmap.model.api.VariableReference;
043: import org.openide.util.NbBundle;
044:
045: /**
046: * The utility class containing auxiliary methods to work with WSDL
047: *
048: * ATTENTION! Many methods contains the lookup as patamether.
049: * It is implied that it contains the BpelModel instance.
050: *
051: * @author Vitaly Bychkov
052: * @author nk160297
053: */
054: public final class ResolverUtility {
055:
056: /**
057: * Calculate a String to display from a QName.
058: */
059: public static String qName2DisplayText(QName qValue) {
060: return qName2DisplayText(qValue, null);
061: }
062:
063: /**
064: * Calculate a String to display from a QName.
065: * The relativeTo parameter is used to calculate the prefix if it isn't specified.
066: */
067: public static String qName2DisplayText(QName qValue,
068: TMapComponent relativeTo) {
069: if (qValue == null) {
070: return "";
071: }
072: //
073: String prefix = qValue.getPrefix();
074: String namespace = null;
075: //
076: if (prefix == null || prefix.length() == 0) {
077: namespace = qValue.getNamespaceURI();
078: if (relativeTo != null && namespace != null
079: && namespace.length() != 0) {
080: prefix = relativeTo.getNamespaceContext().getPrefix(
081: namespace);
082: }
083: }
084: //
085: if (prefix == null || prefix.length() == 0) {
086: if (namespace != null && namespace.length() != 0) {
087: String retValue = qValue.getLocalPart() + "{"
088: + namespace + "}"; // NOI18N
089: return retValue;
090: } else {
091: prefix = "";
092: }
093: } else {
094: prefix = prefix + ":"; // NOI18N
095: }
096: String retValue = prefix + qValue.getLocalPart();
097: return retValue;
098: }
099:
100: //
101: // public static String getDisplayName(Component comp) {
102: // return getDisplayName(comp, null);
103: // }
104: //
105: // public static String getDisplayName(Component comp, TMapComponent relativeTo) {
106: // String targetNamespace = null;
107: // String prefix = null;
108: // String compName = null;
109: // //
110: // if (comp instanceof TMapComponent) {
111: // targetNamespace = ((TMapComponent)comp).getModel().
112: // getTransformMap().getNamespaceContext().;
113: // if (comp instanceof NamedElement) {
114: // compName = ((NamedElement)comp).getName();
115: // }
116: // } else if (comp instanceof WSDLComponent) {
117: // targetNamespace = ((WSDLComponent)comp).getModel().
118: // getDefinitions().getTargetNamespace();
119: // if (comp instanceof Named) {
120: // compName = ((Named)comp).getName();
121: // }
122: // } else if (comp instanceof SchemaComponent) {
123: // targetNamespace = ((SchemaComponent)comp).getModel().
124: // getSchema().getTargetNamespace();
125: // if (comp instanceof Named) {
126: // compName = ((Named)comp).getName();
127: // }
128: // }
129: // //
130: // assert compName != null :
131: // "Impossible to calculate the name for a component which hasn't a name!"; // NOI18N
132: // //
133: // if (targetNamespace != null && targetNamespace.length() > 0) {
134: // if (relativeTo != null) {
135: // NamespaceContext nc = relativeTo.getNamespaceContext();
136: // prefix = nc.getPrefix(targetNamespace);
137: // }
138: // if (prefix == null || prefix.length() == 0) {
139: // return targetNamespace + Constants.COLON + compName;
140: // } else {
141: // return prefix + Constants.COLON + compName;
142: // }
143: // } else {
144: // return compName;
145: // }
146: // }
147: //
148: // /**
149: // * Returns projectSource related to the given bpelModel which is in lookup
150: // * Could return null
151: // */
152: // public static FileObject getProjectSource(Lookup lookup) {
153: // BpelModel bpelModel = lookup.lookup(BpelModel.class);
154: // if (bpelModel == null) {
155: // return null;
156: // }
157: // FileObject bpelFo = Util.getFileObjectByModel(bpelModel);
158: // if (bpelFo == null) {
159: // return null;
160: // }
161: // Sources sources = safeGetSources(safeGetProject(bpelModel));
162: // if (sources == null) {
163: // return null;
164: // }
165: //
166: // String bpelFoPath = bpelFo.getPath();
167: // SourceGroup[] sourceGroupArr = sources.getSourceGroups(
168: // ProjectConstants.SOURCES_TYPE_BPELPRO);
169: // for (SourceGroup srcGroup : sourceGroupArr) {
170: // String srcFolderName = srcGroup.getRootFolder().getPath();
171: // //
172: // if (bpelFoPath.startsWith(srcFolderName)) {
173: // return srcGroup.getRootFolder();
174: // }
175: // }
176: // return null;
177: // }
178: //
179: // public static String encodeLocation(String location){
180: // return location.replace(" ", "%20");
181: // }
182: //
183: // public static String decodeLocation(String location){
184: // return location.replace("%20", " ");
185: // }
186: //
187: // /**
188: // * Returns the FileObject which points to the folder where the specified
189: // * BPEL Process is located.
190: // *
191: // * This method can return null;
192: // */
193: // public static FileObject getBpelProcessFolder(BpelModel bpelModel) {
194: // if (bpelModel != null) {
195: // ModelSource bpelMS = bpelModel.getModelSource();
196: // if (bpelMS != null) {
197: // Lookup bpelLookup = bpelMS.getLookup();
198: // if (bpelLookup != null) {
199: // FileObject bpelFo = bpelLookup.lookup(FileObject.class);
200: // if (bpelFo != null) {
201: // FileObject bpelFolderFo = bpelFo.getParent();
202: // return bpelFolderFo;
203: // }
204: // }
205: // }
206: // }
207: // return null;
208: // }
209: //
210: // /**
211: // * Check if the specified model is imported to the current BPEL.
212: // */
213: // public static boolean isModelImported(Model model, Lookup lookup) {
214: // BpelModel bpelModel = lookup.lookup(BpelModel.class);
215: // return isModelImported(model, bpelModel);
216: // }
217: //
218: // /**
219: // * Check if the specified model is imported to the current BPEL.
220: // */
221: // public static boolean isModelImported(Model model, BpelModel bpelModel) {
222: // if (model == SchemaModelFactory.getDefault().getPrimitiveTypesModel()) {
223: // // the primitive types' model considered as imported implicitly.
224: // return true;
225: // }
226: //
227: // for (Import imp : bpelModel.getProcess().getImports()) {
228: // if (model == ImportHelper.getWsdlModel(imp, false)) {
229: // return true;
230: // }
231: // if (model == ImportHelper.getSchemaModel(imp, false)) {
232: // return true;
233: // }
234: // }
235: //
236: // return false;
237: // }
238: //
239: /**
240: * Tries to resolve the reference and take it's name.
241: */
242: public static String getNameByRef(Reference ref) {
243: if (ref == null) {
244: return null;
245: }
246: String result = null;
247: //
248: Object obj = null;
249: try {
250: obj = ref.get();
251: } catch (IllegalStateException ex) {
252: //This exception may happen if referenced object was removed from model
253: //A kind of workaround required to work with cached references.
254: }
255:
256: if (obj != null) {
257: if (obj instanceof VariableReference) {
258: result = ((VariableReference) obj)
259: .getReferencedVariable().getName();
260: } else if (obj instanceof Named) {
261: result = ((Named) obj).getName();
262: }
263: } else {
264: result = ref.getRefString();
265: }
266: //
267: return result;
268: }
269: //
270: // public static ModelSource getImportedModelSource(Import importObj) {
271: // if (Import.SCHEMA_IMPORT_TYPE.equals(importObj.getImportType())) {
272: // SchemaModel schemaModel = ImportHelper.getSchemaModel(importObj, false);
273: // if (schemaModel != null) {
274: // return schemaModel.getModelSource();
275: // }
276: // } else if (Import.WSDL_IMPORT_TYPE.equals(importObj.getImportType())) {
277: // WSDLModel wsdlModel = ImportHelper.getWsdlModel(importObj, false);
278: // if (wsdlModel != null) {
279: // return wsdlModel.getModelSource();
280: // }
281: // }
282: //
283: // return null;
284: // }
285: //
286: // public static FileObject getImportedFileObject(Import importObj) {
287: // ModelSource modelSource = getImportedModelSource(importObj);
288: //
289: // if (modelSource != null) {
290: // return modelSource.getLookup().lookup(FileObject.class);
291: // } else {
292: // return null;
293: // }
294: // }
295: //
296: // /*
297: // * Description is shown in e.g. TypeChooserPanel. It's either a path
298: // * to the imported file or a message that a file doesn't exist.
299: // */
300: // public static String getImportDescription(Import importObj) {
301: // FileObject fo = getImportedFileObject(importObj);
302: // if (fo != null && fo.isValid()) {
303: // return fo.getPath();
304: // } else {
305: // //No valid Model or ModelSource or FileObject found - return warning
306: // String importInfo = importObj.getLocation();
307: // if (importInfo == null || importInfo.length() == 0) {
308: // importInfo = importObj.getNamespace();
309: // }
310: // return NbBundle.getMessage(FormBundle.class,
311: // "ERR_IMPORT_FILE_DOESNT_EXIST", // NOI18N
312: // ResolverUtility.decodeLocation(importInfo), "");
313: // }
314: // }
315: //
316: // /**
317: // * Returns the project Sources for the specified bpel process.
318: // * Use the following line to access the SourceGroup array.
319: // * SourceGroup[] sourceGroup =
320: // * sources.getSourceGroups(ProjectConstants.SOURCES_TYPE_BPELPRO);
321: // */
322: // public static Sources safeGetSources(Project project) {
323: // if (project != null) {
324: // return ProjectUtils.getSources(project);
325: // } else {
326: // return null;
327: // }
328: // }
329: //
330: // public static Project safeGetProject(BpelModel bpelModel) {
331: // FileObject fo = Util.getFileObjectByModel(bpelModel);
332: // if (fo != null && fo.isValid()) {
333: // return FileOwnerQuery.getOwner(fo);
334: // } else {
335: // return null;
336: // }
337: // }
338: //
339: // /*
340: // * Returns the relative path of a given FileObject in a given Project.
341: // * Returns null if either of the parameters is null or given FileObject
342: // * is not in the given Project.
343: // */
344: // public static String safeGetRelativePath(FileObject fo, Project project) {
345: // if (fo == null || !fo.isValid() || project == null) {
346: // return null;
347: // }
348: //
349: // if (FileOwnerQuery.getOwner(fo) != project) {
350: // return null;
351: // }
352: //
353: // String targetFoPath = fo.getPath();
354: // //
355: // Sources sources = safeGetSources(project);
356: // if (sources != null) {
357: // SourceGroup[] sourceGroupArr = sources.getSourceGroups(
358: // ProjectConstants.SOURCES_TYPE_BPELPRO);
359: // //
360: // for (SourceGroup srcGroup : sourceGroupArr) {
361: // String srcFolderName = srcGroup.getRootFolder().getPath();
362: // //
363: // if (targetFoPath.startsWith(srcFolderName)) {
364: // return targetFoPath.substring(srcFolderName.length());
365: // }
366: // }
367: // }
368: // //TODO:it's strange that we are here since we have already checked that
369: // //our FileObject belongs to our Project, but still we couldn't calculate the
370: // //relative path. Should we assert?
371: // return null;
372: // }
373: }
|