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.util;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.util.ArrayList;
024: import java.util.Collection;
025: import java.util.Enumeration;
026: import java.util.List;
027: import java.util.StringTokenizer;
028: import javax.swing.text.BadLocationException;
029: import javax.swing.text.Document;
030: import javax.xml.namespace.QName;
031: import org.netbeans.api.project.FileOwnerQuery;
032: import org.netbeans.api.project.Project;
033: import org.netbeans.api.project.ProjectUtils;
034: import org.netbeans.api.project.SourceGroup;
035: import org.netbeans.api.project.Sources;
036: import org.netbeans.modules.xml.api.EncodingUtil;
037: import org.netbeans.modules.xml.catalogsupport.ProjectConstants;
038: import org.netbeans.modules.xml.retriever.catalog.Utilities;
039: import org.netbeans.modules.xml.wsdl.model.Definitions;
040: import org.netbeans.modules.xml.wsdl.model.Operation;
041: import org.netbeans.modules.xml.wsdl.model.OperationParameter;
042: import org.netbeans.modules.xml.wsdl.model.PortType;
043: import org.netbeans.modules.xml.wsdl.model.ReferenceableWSDLComponent;
044: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
045: import org.netbeans.modules.xml.wsdl.model.WSDLModelFactory;
046: import org.netbeans.modules.xml.wsdl.model.extensions.bpel.PartnerLinkType;
047: import org.netbeans.modules.xml.wsdl.model.extensions.bpel.Role;
048: import org.netbeans.modules.xml.wsdl.model.Message;
049: import org.netbeans.modules.xml.xam.ModelSource;
050: import org.netbeans.modules.xml.xam.Reference;
051: import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
052: import org.netbeans.modules.xslt.tmap.model.api.TMapComponent;
053: import org.netbeans.modules.xslt.tmap.model.api.TMapModel;
054: import org.netbeans.modules.xslt.tmap.model.api.WSDLReference;
055: import org.netbeans.modules.xslt.tmap.model.spi.TMapModelFactory;
056: import org.netbeans.modules.xslt.tmap.model.xsltmap.TransformationDesc;
057: import org.netbeans.modules.xslt.tmap.model.xsltmap.XmlUtil;
058: import org.netbeans.modules.xslt.tmap.model.xsltmap.XsltMapConst;
059: import org.netbeans.modules.xslt.tmap.nodes.TMapComponentNode;
060: import org.openide.ErrorManager;
061: import org.openide.cookies.EditCookie;
062: import org.openide.cookies.EditorCookie;
063: import org.openide.cookies.SaveCookie;
064: import org.openide.filesystems.FileObject;
065: import org.openide.filesystems.FileUtil;
066: import org.openide.filesystems.Repository;
067: import org.openide.loaders.DataFolder;
068: import org.openide.loaders.DataObject;
069: import org.openide.util.NbBundle;
070: import org.netbeans.modules.soa.ui.SoaUiUtil;
071:
072: /**
073: *
074: * @author Vitaly Bychkov
075: * @version 1.0
076: */
077: public class Util {
078: public static final String FORWARD_SLASH = "/"; // NOI18N
079: public static final String UP_REL_FOLDER = "../"; // NOI18N
080: public static final String CUR_REL_FOLDER = "./"; // NOI18N
081: public static final String WSDL = "wsdl"; // NOI18N
082: // TODO m
083: public static final String SRC = "src"; // NOI18N
084:
085: private Util() {
086: }
087:
088: /**
089: * TODO m - looks like general utility method for all modules
090: */
091: public static FileObject getRelativeFO(FileObject startPoint,
092: String relLocation) {
093: if (startPoint == null || relLocation == null) {
094: return null;
095: }
096:
097: if (!startPoint.isFolder()) {
098: startPoint = startPoint.getParent();
099: }
100:
101: if (relLocation.startsWith(UP_REL_FOLDER)) {
102: int upRelLength = UP_REL_FOLDER.length();
103: while (relLocation.startsWith(UP_REL_FOLDER)) {
104: startPoint = startPoint.getParent();
105: relLocation = relLocation.substring(upRelLength);
106: }
107:
108: } else if (relLocation.startsWith(CUR_REL_FOLDER)) {
109: relLocation = relLocation
110: .substring(CUR_REL_FOLDER.length());
111: }
112: return startPoint.getFileObject(relLocation);
113: }
114:
115: /**
116: * TODO m - looks like general utility method for all modules
117: */
118: public static String getRelativePath(FileObject fromFo,
119: FileObject toFo) {
120: String relativePath = FileUtil.getRelativePath(fromFo, toFo);
121: if (relativePath != null) {
122: return relativePath;
123: }
124:
125: if (!fromFo.isFolder()) {
126: fromFo = fromFo.getParent();
127: }
128:
129: StringTokenizer fromPath = new StringTokenizer(
130: fromFo.getPath(), FORWARD_SLASH);
131: StringTokenizer toPath = new StringTokenizer(toFo.getPath(),
132: FORWARD_SLASH);
133: String tmpFromFolder = null;
134: String tmpToFolder = null;
135: while (fromPath.hasMoreTokens()) {
136: tmpFromFolder = fromPath.nextToken();
137: tmpToFolder = toPath.hasMoreTokens() ? toPath.nextToken()
138: : null;
139: if (!(tmpFromFolder.equals(tmpToFolder))) {
140: break;
141: }
142: }
143: if (tmpToFolder == null) {
144: return null;
145: }
146:
147: StringBuffer fromRelativePathPart = new StringBuffer(
148: UP_REL_FOLDER);
149: while (fromPath.hasMoreTokens()) {
150: fromPath.nextToken();
151: fromRelativePathPart.append(UP_REL_FOLDER);
152: }
153:
154: StringBuffer toRelativePathPart = new StringBuffer(tmpToFolder);
155: while (toPath.hasMoreTokens()) {
156: toRelativePathPart.append(FORWARD_SLASH).append(
157: toPath.nextToken());
158: }
159:
160: return fromRelativePathPart.append(toRelativePathPart)
161: .toString();
162: }
163:
164: public static Project getProject(FileObject projectFo) {
165: FileObject projectRoot = null;
166: return projectFo == null ? null : FileOwnerQuery
167: .getOwner(projectFo);
168: }
169:
170: public static FileObject getProjectRoot(FileObject projectFo) {
171: FileObject projectRoot = null;
172: Project project = FileOwnerQuery.getOwner(projectFo);
173: if (project != null) {
174: projectRoot = project.getProjectDirectory();
175: }
176: return projectRoot;
177: }
178:
179: public static WSDLModel[] getAllProjectWsdls(FileObject projectRoot) {
180: assert projectRoot != null;
181: List<FileObject> fileObjects = new ArrayList<FileObject>();
182: Enumeration projectFolders = projectRoot.getFolders(true);
183: while (projectFolders.hasMoreElements()) {
184: FileObject folder = (FileObject) projectFolders
185: .nextElement();
186: FileObject[] childrenFo = folder.getChildren();
187: for (FileObject elem : childrenFo) {
188: if (!elem.isFolder() && WSDL.equals(elem.getExt())) {
189: fileObjects.add(elem);
190: }
191: }
192: }
193:
194: WSDLModel[] wsdlModels = null;
195: if (fileObjects != null && fileObjects.size() > 0) {
196: wsdlModels = new WSDLModel[fileObjects.size()];
197: }
198:
199: WSDLModelFactory factory = WSDLModelFactory.getDefault();
200: for (int i = 0; i < wsdlModels.length; i++) {
201: ModelSource wsdlModelSource = Utilities.getModelSource(
202: fileObjects.get(i), true);
203: wsdlModels[i] = factory.getModel(wsdlModelSource);
204: }
205:
206: return wsdlModels;
207: }
208:
209: public static FileObject[] getProjectSources(Project project) {
210: List<FileObject> projectSources = new ArrayList<FileObject>();
211: if (project == null) {
212: return null; // sometimes project couldn't be founded for nb development project
213: // throw new IllegalArgumentException("project shouldn't be null");
214: }
215: Sources sources = ProjectUtils.getSources(project);
216: SourceGroup[] sourceGroups = sources
217: .getSourceGroups(ProjectConstants.SOURCES_TYPE_XML);
218: if (sourceGroups != null) {
219: for (SourceGroup sourceGroup : sourceGroups) {
220: projectSources.add(sourceGroup.getRootFolder());
221: }
222: }
223:
224: return projectSources.toArray(new FileObject[projectSources
225: .size()]);
226: }
227:
228: public static FileObject getProjectSource(Project project) {
229: FileObject projectSource = null;
230: if (project == null) {
231: return null; // sometimes project couldn't be founded for nb development project
232: // throw new IllegalArgumentException("project shouldn't be null");
233: }
234: Sources sources = ProjectUtils.getSources(project);
235: SourceGroup[] sourceGroups = sources
236: .getSourceGroups(ProjectConstants.SOURCES_TYPE_XML);
237: if (sourceGroups != null && sourceGroups.length > 0) {
238: projectSource = sourceGroups[0].getRootFolder();
239: }
240:
241: return projectSource;
242: }
243:
244: public static FileObject getTMapFo(Project project) {
245: return getProjectSource(project).getFileObject(
246: "transformmap.xml");
247: }
248:
249: public static File getTransformationDescriptor(Project project) {
250: FileObject fo = getProjectSource(project).getFileObject(
251: "transformmap.xml");
252: if (fo == null) {
253: fo = getProjectSource(project).getFileObject("xsltmap.xml");
254: }
255: return fo == null ? null : FileUtil.toFile(fo);
256: }
257:
258: public static FileObject getXsltMapFo(Project project) {
259: if (project == null) {
260: throw new IllegalArgumentException(
261: "project shouldn't be null");
262: }
263: FileObject xsltMapFo = null;
264: FileObject projectSource = getProjectSource(project);
265: assert projectSource != null;
266:
267: xsltMapFo = projectSource.getFileObject(XsltMapConst.XSLTMAP
268: + "." + XsltMapConst.XML);
269: return xsltMapFo;
270: }
271:
272: public static FileObject getXsltMapFo(FileObject xsltFo) {
273: FileObject xsltMapFo = null;
274: if (xsltFo == null) {
275: return null;
276: }
277: // FileObject projectRoot = getProjectRoot(xsltFo);
278: // if (projectRoot != null) {
279: xsltMapFo = xsltFo.getParent().getFileObject(
280: XsltMapConst.XSLTMAP + "." + XsltMapConst.XML);
281:
282: // xsltMapFo = projectRoot.getFileObject(XsltMapObject.XSLTMAP+"."+XsltMapObject.XML);
283: // }
284: return xsltMapFo;
285: }
286:
287: public static FileObject getXsltMapFo(File projectFile) {
288: if (projectFile == null) {
289: return null;
290: }
291:
292: FileObject projectFo = FileUtil.toFileObject(projectFile);
293: Project project = getProject(projectFo);
294:
295: return project == null ? null : getXsltMapFo(project);
296: }
297:
298: public static TMapModel getTMapModel(FileObject tmapFo) {
299: TMapModel model = null;
300: if (tmapFo != null) {
301: ModelSource modelSource = Utilities.getModelSource(tmapFo,
302: true);
303: model = TMapModelFactory.TMapModelFactoryAccess
304: .getFactory().getModel(modelSource);
305: }
306:
307: return model;
308: }
309:
310: public static FileObject createDefaultTransformmap(Project project) {
311: assert project != null;
312: FileObject tMapFo = null;
313: FileObject projectSource = Util.getProjectSource(project);
314: if (projectSource == null) {
315: return null;
316: }
317:
318: try {
319: tMapFo = FileUtil.copyFile(Repository.getDefault()
320: .getDefaultFileSystem().findResource(
321: "org-netbeans-xsltpro/transformmap.xml"), //NOI18N
322: projectSource, "transformmap"); //NOI18N
323:
324: if (tMapFo != null) {
325: SoaUiUtil.fixEncoding(DataObject.find(tMapFo),
326: projectSource);
327: }
328:
329: } catch (IOException ex) {
330: ErrorManager.getDefault().notify(ex);
331: return null;
332: }
333: return tMapFo;
334: }
335:
336: public static File getXsltMapFile(File projectFile) {
337: return FileUtil.toFile(getXsltMapFo(projectFile));
338: }
339:
340: // TODO m
341:
342: public static PartnerLinkType findPartnerLinkType(
343: FileObject projectRoot, String partnerLinkTypeName) {
344: if (projectRoot == null || partnerLinkTypeName == null) {
345:
346: return null;
347: }
348:
349: PartnerLinkType wsdlPlt = null;
350: WSDLModel[] models = getAllProjectWsdls(projectRoot);
351: for (WSDLModel elem : models) {
352: wsdlPlt = getPartnerLinkType(elem, partnerLinkTypeName);
353:
354: if (wsdlPlt != null) {
355: break;
356: }
357: }
358:
359: return wsdlPlt;
360: }
361:
362: public static Operation findWsdlOperation(FileObject projectRoot,
363: String partnerLinkTypeName, String roleName,
364: String portType, String operation) {
365: if (partnerLinkTypeName == null || roleName == null
366: || projectRoot == null || portType == null
367: || operation == null) {
368: return null;
369: }
370:
371: // TODO m
372:
373: Operation wsdlOperation = null;
374: PartnerLinkType plt = findPartnerLinkType(projectRoot,
375: partnerLinkTypeName);
376:
377: Role role = null;
378: if (plt != null) {
379: role = plt.getRole1();
380: role = role != null && !roleName.equals(role.getName()) ? null
381: : role;
382: if (role == null) {
383: role = plt.getRole2();
384: role = role != null && !roleName.equals(role.getName()) ? null
385: : role;
386: }
387: }
388:
389: PortType portTypeElem = null;
390: if (role != null) {
391: NamedComponentReference<PortType> portTypeRef = role
392: .getPortType();
393: portTypeElem = portTypeRef == null ? null : portTypeRef
394: .get();
395:
396: QName portTypeQname = portTypeRef == null ? null
397: : portTypeRef.getQName();
398: QName xsltMapPortTypeQName = QName.valueOf(portType);
399: if (portTypeElem != null && xsltMapPortTypeQName != null
400: && !xsltMapPortTypeQName.equals(portTypeQname)) {
401: portTypeElem = null;
402: }
403: }
404:
405: if (portTypeElem != null) {
406: Collection<Operation> operations = portTypeElem
407: .getOperations();
408: if (operations != null) {
409: for (Operation elem : operations) {
410: if (operation.equals(elem.getName())) {
411: wsdlOperation = elem;
412: break;
413: }
414: }
415: }
416: }
417:
418: return wsdlOperation;
419: }
420:
421: // TODO m
422: public static Operation findWsdlOperation(FileObject projectRoot,
423: TransformationDesc transformDesc) {
424: if (projectRoot == null || transformDesc == null) {
425: return null;
426: }
427:
428: return findWsdlOperation(projectRoot, transformDesc
429: .getPartnerLink(), transformDesc.getRoleName(),
430: transformDesc.getPortType(), transformDesc
431: .getOperation());
432: ////
433: //// Operation wsdlOperation = null;
434: //// WSDLModel[] models = getAllProjectWsdls(projectRoot);
435: //// for (WSDLModel elem : models) {
436: //// wsdlOperation = getOperation(elem, transformDesc.getPortType(), transformDesc.getOperation());
437: ////
438: //// if (wsdlOperation != null) {
439: //// break;
440: //// }
441: //// }
442: ////
443: //// return wsdlOperation;
444: }
445:
446: // TODO m
447: public static PartnerLinkType getPartnerLinkType(
448: WSDLModel wsdlModel, String partnerLinkTypeName) {
449: if (wsdlModel == null || partnerLinkTypeName == null) {
450: return null;
451: }
452: PartnerLinkType wsdlPlt = null;
453:
454: Definitions defs = wsdlModel.getDefinitions();
455: if (defs == null) {
456: return wsdlPlt;
457: }
458:
459: List<PartnerLinkType> wsdlPlts = defs
460: .getExtensibilityElements(PartnerLinkType.class);
461: for (PartnerLinkType tmpPlt : wsdlPlts) {
462: String pltNameLocalPart = tmpPlt.getName();
463: String pltNamespace = wsdlModel.getDefinitions()
464: .getTargetNamespace();
465: // String pltNamespace = pltQname.getNamespaceURI();
466: if (partnerLinkTypeName.equals("{" + pltNamespace + "}"
467: + pltNameLocalPart)) {
468: wsdlPlt = tmpPlt;
469: break;
470: }
471: }
472: return wsdlPlt;
473: }
474:
475: public static Operation findWsdlOperation(File projectRootFile,
476: TransformationDesc transformDesc) {
477: return findWsdlOperation(
478: FileUtil.toFileObject(projectRootFile), transformDesc);
479: }
480:
481: // TODO m
482: public static Operation getOperation(WSDLModel wsdlModel,
483: String portType, String operation) {
484: if (wsdlModel == null || portType == null || operation == null) {
485: return null;
486: }
487: Operation resultOp = null;
488:
489: Definitions defs = wsdlModel.getDefinitions();
490: if (defs == null) {
491: return resultOp;
492: }
493:
494: Collection<PortType> portTypes = defs.getPortTypes();
495: PortType modelPortType = null;
496: for (PortType tmpPortType : portTypes) {
497: NamedComponentReference<PortType> tmpPortTypeRef = tmpPortType
498: .createReferenceTo(tmpPortType, PortType.class);
499:
500: QName tmpPortTypeQname = tmpPortTypeRef.getQName();
501: String tmpPortTypeNs = tmpPortTypeQname.getNamespaceURI();
502: String tmpPortTypeLocalPart = tmpPortTypeQname
503: .getLocalPart();
504:
505: if (portType.equals("{" + tmpPortTypeNs + "}"
506: + tmpPortTypeLocalPart)) {
507: modelPortType = tmpPortType;
508: break;
509: }
510: }
511:
512: if (modelPortType != null) {
513: Collection<Operation> operations = modelPortType
514: .getOperations();
515: for (Operation tmpOperation : operations) {
516: if (operation.equals(tmpOperation.getName())) {
517: resultOp = tmpOperation;
518: break;
519: }
520: }
521: }
522:
523: return resultOp;
524: }
525:
526: /**
527: * @param if qnamedElement has structure like this: {namespaceURI}localName
528: * @return if localName qnamedElement has structure like this:
529: * {namespaceURI}localName then return localName
530: *
531: */
532: private String getLocalPart(String qnamedElement) {
533:
534: QName qnamedElementQName = QName.valueOf(qnamedElement);
535: return qnamedElementQName.getLocalPart();
536: }
537:
538: public static String getNamespace(
539: ReferenceableWSDLComponent wsdlComp) {
540: if (wsdlComp == null) {
541: return null;
542: }
543:
544: String namespace = null;
545:
546: WSDLModel model = wsdlComp.getModel();
547: Definitions defs = null;
548:
549: if (model != null) {
550: defs = model.getDefinitions();
551: }
552:
553: if (defs != null) {
554: namespace = defs.getTargetNamespace();
555: }
556:
557: return namespace;
558: }
559:
560: public static String getMessageType(Operation operation,
561: boolean isInput) {
562: if (operation == null) {
563: return null;
564: }
565:
566: String messageType = null;
567: NamedComponentReference<Message> messageRef = null;
568:
569: OperationParameter opParam = isInput ? operation.getInput()
570: : operation.getOutput();
571:
572: if (opParam != null) {
573: messageRef = opParam.getMessage();
574: }
575:
576: Message message = null;
577: if (messageRef != null) {
578: message = messageRef.get();
579: }
580:
581: if (message != null) {
582: String namespace = Util.getNamespace(message);
583: namespace = namespace != null ? "{" + namespace + "}" : ""; // NOI18N
584: messageType = namespace + message.getName();
585: }
586:
587: return messageType;
588: }
589:
590: public static String getGrayString(String message) {
591: return getGrayString("", message);
592: }
593:
594: public static String getGrayString(String nonGrayPrefix,
595: String message) {
596: return message == null ? nonGrayPrefix : "<html>"
597: + getCorrectedHtmlRenderedString(nonGrayPrefix) // NOI18N
598: + "<font color='" + GRAY_COLOR + "'>"
599: + getCorrectedHtmlRenderedString(message)
600: + "</font></html>";// NOI18N
601: }
602:
603: public static String getGrayString(String nonGrayPrefix,
604: String message, String nonGraySuffix) {
605: return getGrayString(nonGrayPrefix, message, nonGraySuffix,
606: true);
607: }
608:
609: public static String getGrayString(String nonGrayPrefix,
610: String message, String nonGraySuffix,
611: boolean isSetHtmlHeader) {
612: String htmlHeader = isSetHtmlHeader ? "<html>" : ""; // NOI18N
613: String htmlFooter = isSetHtmlHeader ? "</html>" : ""; // NOI18N
614: return message == null ? nonGrayPrefix
615: : htmlHeader
616: + getCorrectedHtmlRenderedString(nonGrayPrefix)
617: + "<font color='"
618: + GRAY_COLOR
619: + "'>" // NOI18N
620: + getCorrectedHtmlRenderedString(message)
621: + "</font>" // NOI18N
622: + (nonGraySuffix == null ? ""
623: : getCorrectedHtmlRenderedString(nonGraySuffix))
624: + htmlFooter;// NOI18N
625: }
626:
627: public static final String getCorrectedHtmlRenderedString(
628: String htmlString) {
629: if (htmlString == null) {
630: return null;
631: }
632: htmlString = htmlString.replaceAll("&", "&"); // NOI18n
633: htmlString = htmlString.replaceAll(">", ">;"); // NOI18n
634: htmlString = htmlString.replaceAll("<", "<"); // NOI18n
635:
636: htmlString = htmlString.replaceAll("&", "&"); // NOI18n
637: htmlString = htmlString.replaceAll(">", ">"); // NOI18n
638: htmlString = htmlString.replaceAll("<", "<"); // NOI18n
639: return htmlString;
640: }
641:
642: public static FileObject getSrcFolder(Project project) {
643: return project.getProjectDirectory().getFileObject("src");
644: }
645:
646: public static String getReferenceLocalName(WSDLReference wsdlRef) {
647: if (wsdlRef == null) {
648: return null;
649: }
650:
651: QName refQname = wsdlRef.getQName();
652: return refQname == null ? null : refQname.getLocalPart();
653: }
654:
655: public static String getReferenceLocalName(Reference ref) {
656: if (ref == null) {
657: return null;
658: }
659:
660: return ref == null ? null : ref.getRefString();
661: }
662:
663: public static String getLocalizedAttribute(Reference attributeRef,
664: String attributeName) {
665: if (attributeRef == null) {
666: return TMapComponentNode.EMPTY_STRING;
667: }
668:
669: attributeName = attributeName == null ? "" : attributeName;
670: return NbBundle.getMessage(TMapComponentNode.class,
671: "LBL_ATTRIBUTE_HTML_TEMPLATE", // NOI18N
672: attributeName, attributeRef.getRefString());
673: }
674:
675: public static String getLocalizedAttribute(String attributeValue,
676: String attributeName) {
677: if (attributeValue == null) {
678: return TMapComponentNode.EMPTY_STRING;
679: }
680:
681: attributeName = attributeName == null ? TMapComponentNode.EMPTY_STRING
682: : attributeName;
683: attributeValue = attributeValue == null ? TMapComponentNode.EMPTY_STRING
684: : attributeValue;
685: return NbBundle.getMessage(TMapComponentNode.class,
686: "LBL_ATTRIBUTE_HTML_TEMPLATE", // NOI18N
687: attributeName, attributeValue);
688: }
689:
690: private static String GRAY_COLOR = "#999999";
691:
692: }
|