0001: /*
0002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0003: *
0004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
0005: *
0006: * The contents of this file are subject to the terms of either the GNU
0007: * General Public License Version 2 only ("GPL") or the Common
0008: * Development and Distribution License("CDDL") (collectively, the
0009: * "License"). You may not use this file except in compliance with the
0010: * License. You can obtain a copy of the License at
0011: * http://www.netbeans.org/cddl-gplv2.html
0012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
0013: * specific language governing permissions and limitations under the
0014: * License. When distributing the software, include this License Header
0015: * Notice in each file and include the License file at
0016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
0017: * particular file as subject to the "Classpath" exception as provided
0018: * by Sun in the GPL Version 2 section of the License file that
0019: * accompanied this code. If applicable, add the following below the
0020: * License Header, with the fields enclosed by brackets [] replaced by
0021: * your own identifying information:
0022: * "Portions Copyrighted [year] [name of copyright owner]"
0023: *
0024: * Contributor(s):
0025: *
0026: * The Original Software is NetBeans. The Initial Developer of the Original
0027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
0028: * Microsystems, Inc. All Rights Reserved.
0029: *
0030: * If you wish your version of this file to be governed by only the CDDL
0031: * or only the GPL Version 2, indicate your decision by adding
0032: * "[Contributor] elects to include this software in this distribution
0033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
0034: * single choice of license, a recipient has the option to distribute
0035: * your version of this file under either the CDDL, the GPL Version 2 or
0036: * to extend the choice of license to its licensees as provided above.
0037: * However, if you add GPL Version 2 code and therefore, elected the GPL
0038: * Version 2 license, then the option applies only if the new code is
0039: * made subject to such option by the copyright holder.
0040: */
0041:
0042: package org.netbeans.modules.xml.wsdl.ui.netbeans.module;
0043:
0044: import java.awt.datatransfer.Transferable;
0045: import java.io.IOException;
0046: import java.net.URI;
0047: import java.net.URISyntaxException;
0048: import java.util.ArrayList;
0049: import java.util.Collection;
0050: import java.util.HashMap;
0051: import java.util.HashSet;
0052: import java.util.Iterator;
0053: import java.util.List;
0054: import java.util.Map;
0055: import java.util.Set;
0056:
0057: import javax.xml.XMLConstants;
0058: import javax.xml.namespace.QName;
0059:
0060: import org.netbeans.api.project.FileOwnerQuery;
0061: import org.netbeans.api.project.Project;
0062: import org.netbeans.modules.xml.catalogsupport.DefaultProjectCatalogSupport;
0063: import org.netbeans.modules.xml.schema.model.Element;
0064: import org.netbeans.modules.xml.schema.model.GlobalElement;
0065: import org.netbeans.modules.xml.schema.model.GlobalType;
0066: import org.netbeans.modules.xml.schema.model.ReferenceableSchemaComponent;
0067: import org.netbeans.modules.xml.schema.model.Schema;
0068: import org.netbeans.modules.xml.schema.model.SchemaComponent;
0069: import org.netbeans.modules.xml.schema.model.SchemaModel;
0070: import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
0071: import org.netbeans.modules.xml.schema.ui.basic.SchemaModelCookie;
0072: import org.netbeans.modules.xml.wsdl.model.Binding;
0073: import org.netbeans.modules.xml.wsdl.model.BindingOperation;
0074: import org.netbeans.modules.xml.wsdl.model.Definitions;
0075: import org.netbeans.modules.xml.wsdl.model.ExtensibilityElement;
0076: import org.netbeans.modules.xml.wsdl.model.Import;
0077: import org.netbeans.modules.xml.wsdl.model.Operation;
0078: import org.netbeans.modules.xml.wsdl.model.PortType;
0079: import org.netbeans.modules.xml.wsdl.model.Types;
0080: import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
0081: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
0082: import org.netbeans.modules.xml.wsdl.model.extensions.xsd.WSDLSchema;
0083: import org.netbeans.modules.xml.wsdl.ui.actions.NameGenerator;
0084: import org.netbeans.modules.xml.wsdl.ui.actions.schema.ExtensibilityElementCreatorVisitor;
0085: import org.netbeans.modules.xml.wsdl.ui.common.Constants;
0086: import org.netbeans.modules.xml.wsdl.ui.schema.visitor.OptionalAttributeFinderVisitor;
0087: import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.pastetype.SchemaImportPasteType;
0088: import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.pastetype.WSDLImportPasteType;
0089: import org.netbeans.modules.xml.wsdl.ui.wsdl.util.RelativePath;
0090: import org.netbeans.modules.xml.xam.AbstractComponent;
0091: import org.netbeans.modules.xml.xam.Model;
0092: import org.netbeans.modules.xml.xam.Named;
0093: import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
0094: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
0095: import org.netbeans.spi.palette.PaletteController;
0096: import org.openide.ErrorManager;
0097: import org.openide.explorer.view.TreeView;
0098: import org.openide.filesystems.FileObject;
0099: import org.openide.filesystems.FileUtil;
0100: import org.openide.loaders.DataObject;
0101: import org.openide.nodes.Children;
0102: import org.openide.nodes.Node;
0103: import org.openide.nodes.NodeTransfer;
0104: import org.openide.util.Lookup;
0105: import org.openide.util.datatransfer.PasteType;
0106:
0107: public class Utility {
0108:
0109: public static String getNamespacePrefix(String namespace,
0110: WSDLModel model) {
0111: if (model != null && namespace != null) {
0112: return ((AbstractDocumentComponent) model.getDefinitions())
0113: .lookupPrefix(namespace);
0114: }
0115: return null;
0116: }
0117:
0118: public static String getNamespacePrefix(String namespace,
0119: WSDLComponent element) {
0120: if (element != null && namespace != null) {
0121: return ((AbstractDocumentComponent) element)
0122: .lookupPrefix(namespace);
0123: }
0124: return null;
0125: }
0126:
0127: public static String getNamespaceURI(String prefix,
0128: WSDLComponent element) {
0129: if (element != null && prefix != null) {
0130: return ((AbstractDocumentComponent) element)
0131: .lookupNamespaceURI(prefix, true);
0132: }
0133: return null;
0134: }
0135:
0136: public static String getNamespaceURI(String prefix, WSDLModel model) {
0137: if (model != null && prefix != null) {
0138: return ((AbstractDocumentComponent) model.getDefinitions())
0139: .lookupNamespaceURI(prefix, true);
0140: }
0141: return null;
0142: }
0143:
0144: public static Import getImport(String namespace, WSDLModel model) {
0145: Collection imports = model.getDefinitions().getImports();
0146: if (imports != null) {
0147: Iterator iter = imports.iterator();
0148: for (; iter.hasNext();) {
0149: Import existingImport = (Import) iter.next();
0150: if (existingImport.getNamespace().equals(namespace)) {
0151: return existingImport;
0152: }
0153: }
0154: }
0155: return null;
0156: }
0157:
0158: public static Collection<WSDLModel> getImportedDocuments(
0159: WSDLModel model) {
0160: Collection<Import> imports = model.getDefinitions()
0161: .getImports();
0162: Collection<WSDLModel> returnImports = new ArrayList<WSDLModel>();
0163: if (imports != null) {
0164: Iterator iter = imports.iterator();
0165: for (; iter.hasNext();) {
0166: Import existingImport = (Import) iter.next();
0167: List<WSDLModel> impModels = model
0168: .findWSDLModel(existingImport.getNamespace());
0169: returnImports.addAll(impModels);
0170: }
0171: }
0172: return returnImports;
0173: }
0174:
0175: public static Map getNamespaces(Definitions def) {
0176: return ((AbstractDocumentComponent) def).getPrefixes();
0177: }
0178:
0179: public static GlobalElement findGlobalElement(WSDLModel model,
0180: QName elementQName) {
0181: String namespace = elementQName.getNamespaceURI();
0182: String localName = elementQName.getLocalPart();
0183:
0184: GlobalElement target = null;
0185: if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(namespace)) {
0186: SchemaModel primitiveModel = SchemaModelFactory
0187: .getDefault().getPrimitiveTypesModel();
0188: target = primitiveModel.resolve(namespace, localName,
0189: GlobalElement.class);
0190: } else {
0191: Types types = model.getDefinitions().getTypes();
0192: if (types != null) {
0193: for (Schema s : types.getSchemas()) {
0194: target = s.getModel().resolve(namespace, localName,
0195: GlobalElement.class);
0196: if (target != null) {
0197: break;
0198: }
0199: }
0200: }
0201: if (target == null) {
0202: for (Import i : model.getDefinitions().getImports()) {
0203: String location = i.getLocation();
0204: if (location.toLowerCase().endsWith(
0205: Constants.XSD_EXT)) {
0206: List<Schema> schemas = model.findSchemas(i
0207: .getNamespace());
0208:
0209: for (Schema schema : schemas) {
0210: target = schema.getModel().resolve(
0211: namespace, localName,
0212: GlobalElement.class);
0213: if (target != null) {
0214: break;
0215: }
0216: }
0217: }
0218: }
0219: }
0220: }
0221: return target;
0222: }
0223:
0224: public static GlobalType findGlobalType(WSDLModel model,
0225: QName typeQName) {
0226: String namespace = typeQName.getNamespaceURI();
0227: String localName = typeQName.getLocalPart();
0228:
0229: GlobalType target = null;
0230: if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(namespace)) {
0231: SchemaModel primitiveModel = SchemaModelFactory
0232: .getDefault().getPrimitiveTypesModel();
0233: target = primitiveModel.resolve(namespace, localName,
0234: GlobalType.class);
0235: } else {
0236: Types types = model.getDefinitions().getTypes();
0237: if (types != null) {
0238: for (Schema s : types.getSchemas()) {
0239: target = s.getModel().resolve(namespace, localName,
0240: GlobalType.class);
0241: if (target != null) {
0242: break;
0243: }
0244: }
0245: }
0246: if (target == null) {
0247: for (Import i : model.getDefinitions().getImports()) {
0248: String location = i.getLocation();
0249: if (location.toLowerCase().endsWith(
0250: Constants.XSD_EXT)) {
0251: List<Schema> schemas = model.findSchemas(i
0252: .getNamespace());
0253:
0254: for (Schema schema : schemas) {
0255: target = schema.getModel().resolve(
0256: namespace, localName,
0257: GlobalType.class);
0258: if (target != null) {
0259: break;
0260: }
0261: }
0262: }
0263: }
0264: }
0265: }
0266: return target;
0267: }
0268:
0269: public static String fromQNameToString(QName qname) {
0270: if (qname.getPrefix() != null
0271: && qname.getPrefix().trim().length() > 0) {
0272: return qname.getPrefix() + ":" + qname.getLocalPart();
0273: }
0274: return qname.getLocalPart();
0275: }
0276:
0277: public static String getNameAndDropPrefixIfInCurrentModel(
0278: String ns, String localPart, WSDLModel model) {
0279: if (ns == null || model == null)
0280: return localPart;
0281:
0282: String tns = model.getDefinitions().getTargetNamespace();
0283: if (tns != null && !tns.equals(ns)) {
0284: String prefix = getNamespacePrefix(ns, model);
0285: if (prefix != null)
0286: return prefix + ":" + localPart;
0287: }
0288:
0289: return localPart;
0290: }
0291:
0292: public static List<QName> getExtensionAttributes(WSDLComponent comp) {
0293: ArrayList<QName> result = new ArrayList<QName>();
0294: Map<QName, String> attrMap = comp.getAttributeMap();
0295: Set<QName> set = attrMap.keySet();
0296: if (set != null) {
0297: Iterator<QName> iter = set.iterator();
0298: while (iter.hasNext()) {
0299: QName name = iter.next();
0300: String ns = name.getNamespaceURI();
0301: if (ns != null
0302: && ns.trim().length() > 0
0303: && !ns
0304: .equals(((AbstractDocumentComponent) comp)
0305: .getQName().getNamespaceURI())) {
0306: result.add(name);
0307: }
0308: }
0309: }
0310: return result;
0311: }
0312:
0313: public static List<QName> getOptionalAttributes(WSDLComponent comp,
0314: Element elem) {
0315: ArrayList<QName> result = new ArrayList<QName>();
0316: Map<QName, String> attrMap = comp.getAttributeMap();
0317: Set<QName> set = attrMap.keySet();
0318: if (set != null) {
0319: Iterator<QName> iter = set.iterator();
0320: while (iter.hasNext()) {
0321: QName name = iter.next();
0322: String ns = name.getNamespaceURI();
0323: if (ns != null
0324: && ns.trim().length() > 0
0325: && !ns.equals(comp.getModel().getDefinitions()
0326: .getTargetNamespace())) {
0327: //extension attibute
0328: //do nothing
0329: } else {
0330: //not a extension attribute
0331: OptionalAttributeFinderVisitor visitor = new OptionalAttributeFinderVisitor(
0332: name.getLocalPart());
0333: elem.accept(visitor);
0334: if (visitor.isOptional()) {
0335: result.add(name);
0336: }
0337: }
0338: }
0339: }
0340: return result;
0341: }
0342:
0343: /**
0344: * Expands nodes on the treeview till given levels
0345: * @param tv the treeview object
0346: * @param level the level till which the nodes should be expanded. 0 means none.
0347: * @param rootNode the rootNode
0348: */
0349: public static void expandNodes(TreeView tv, int level, Node rootNode) {
0350: if (level == 0)
0351: return;
0352:
0353: Children children = rootNode.getChildren();
0354: if (children != null) {
0355: Node[] nodes = children.getNodes();
0356: if (nodes != null) {
0357: for (int i = 0; i < nodes.length; i++) {
0358: tv.expandNode(nodes[i]); //Expand node
0359: expandNodes(tv, level - 1, nodes[i]); //expand children
0360: }
0361: }
0362: }
0363: }
0364:
0365: public static void addNamespacePrefix(WSDLComponent comp,
0366: WSDLModel model, String prefix) {
0367: if (comp != null && model != null) {
0368: addNamespacePrefix(comp.getModel(), model, prefix);
0369: }
0370: }
0371:
0372: public static void addNamespacePrefix(WSDLModel imported,
0373: WSDLModel model, String prefix) {
0374: assert model != null;
0375: if (imported != null) {
0376: Definitions definitions = model.getDefinitions();
0377: String targetNamespace = imported.getDefinitions()
0378: .getTargetNamespace();
0379: String computedPrefix = null;
0380:
0381: if (targetNamespace != null) {
0382: if (Utility.getNamespacePrefix(targetNamespace, model) != null) {
0383: //already exists, doesnt need to be added
0384: return;
0385: }
0386: //Use the prefix (in parameter) or generate new one.
0387: if (prefix != null) {
0388: computedPrefix = prefix;
0389: } else {
0390: computedPrefix = NameGenerator.getInstance()
0391: .generateNamespacePrefix(null,
0392: model.getDefinitions());
0393: }
0394: boolean isAlreadyInTransaction = Utility
0395: .startTransaction(model);
0396: ((AbstractDocumentComponent) definitions).addPrefix(
0397: computedPrefix, targetNamespace);
0398:
0399: Utility.endTransaction(model, isAlreadyInTransaction);
0400:
0401: }
0402:
0403: }
0404:
0405: }
0406:
0407: public static void addNamespacePrefix(Element schemaElement,
0408: WSDLComponent element, String prefix) {
0409: if (schemaElement != null && element.getModel() != null) {
0410: addNamespacePrefix(schemaElement.getModel().getSchema(),
0411: element.getModel(), prefix);
0412: }
0413:
0414: }
0415:
0416: public static void addNamespacePrefix(Schema schema,
0417: WSDLModel model, String prefix) {
0418: assert model != null;
0419: if (schema != null) {
0420: Definitions definitions = model.getDefinitions();
0421: String targetNamespace = schema.getTargetNamespace();
0422: String computedPrefix = null;
0423:
0424: if (targetNamespace != null) {
0425: if (Utility.getNamespacePrefix(targetNamespace, model) != null) {
0426: //already exists, doesnt need to be added
0427: return;
0428: }
0429: //Use the prefix (in parameter) or generate new one.
0430: if (prefix != null) {
0431: computedPrefix = prefix;
0432: } else {
0433: computedPrefix = NameGenerator.getInstance()
0434: .generateNamespacePrefix(null,
0435: model.getDefinitions());
0436: }
0437: boolean isAlreadyInTransaction = Utility
0438: .startTransaction(model);
0439: ((AbstractDocumentComponent) definitions).addPrefix(
0440: computedPrefix, targetNamespace);
0441:
0442: Utility.endTransaction(model, isAlreadyInTransaction);
0443:
0444: }
0445:
0446: }
0447: }
0448:
0449: public static void addExtensibilityElement(WSDLComponent element,
0450: Element schemaElement, String prefix) {
0451: // Issue 93424, create a single transaction to encapsulate all changes.
0452: WSDLModel model = element.getModel();
0453: boolean in = startTransaction(model);
0454: Utility.addNamespacePrefix(schemaElement, element, prefix);
0455: ExtensibilityElementCreatorVisitor eeCreator = new ExtensibilityElementCreatorVisitor(
0456: element);
0457: schemaElement.accept(eeCreator);
0458: endTransaction(model, in);
0459: }
0460:
0461: public static boolean startTransaction(WSDLModel model) {
0462: if (model.isIntransaction())
0463: return true;
0464: model.startTransaction();
0465: return false;
0466: }
0467:
0468: public static void endTransaction(WSDLModel model,
0469: boolean isInTransaction) {
0470: if (isInTransaction)
0471: return;
0472: model.endTransaction();
0473: }
0474:
0475: /**
0476: * Get all operations that are not part of the given binding.
0477: *
0478: * @param portType
0479: * @param binding
0480: * @return list of operations, may be null.
0481: */
0482: public static Collection<Operation> getImplementableOperations(
0483: PortType portType, Binding binding) {
0484: if (portType == null || portType.getOperations() == null
0485: || portType.getOperations().size() == 0
0486: || binding == null) {
0487: return null;
0488: }
0489: List<Operation> listData = new ArrayList<Operation>(portType
0490: .getOperations().size());
0491: if (portType.getOperations().size() != binding
0492: .getBindingOperations().size()) {
0493:
0494: Set<Operation> bindingOperationsSet = new HashSet<Operation>();
0495: for (BindingOperation bo : binding.getBindingOperations()) {
0496: bindingOperationsSet.add(bo.getOperation().get());
0497: }
0498:
0499: for (Operation operation : portType.getOperations()) {
0500: if (!bindingOperationsSet.contains(operation)) {
0501: listData.add(operation);
0502: }
0503: }
0504: }
0505: return listData;
0506: }
0507:
0508: /**
0509: * Get all the operations that are overloaded in the port type.
0510: * Overloaded means: same operation name with same input/output names.
0511: * @param portType
0512: * @return non null list of operations.
0513: */
0514: public static List<Operation> getOverloadedOperations(
0515: PortType portType) {
0516: List<Operation> result = new ArrayList<Operation>();
0517: Set<String> operationNames = new HashSet<String>();
0518: Set<String> overloadedOperationNames = new HashSet<String>();
0519:
0520: for (Operation operation : portType.getOperations()) {
0521: String opName = operation.getName();
0522: if (operationNames.contains(opName)) {
0523: overloadedOperationNames.add(opName);
0524: } else {
0525: operationNames.add(opName);
0526: }
0527: }
0528:
0529: if (!overloadedOperationNames.isEmpty()) {
0530: result = new ArrayList<Operation>();
0531: for (Operation operation : portType.getOperations()) {
0532: String opName = operation.getName();
0533: if (overloadedOperationNames.contains(opName)) {
0534: result.add(operation);
0535: }
0536: }
0537: }
0538:
0539: return result;
0540: }
0541:
0542: public static String getOperationSignature(Operation operation) {
0543: StringBuilder builder = new StringBuilder(operation.getName()
0544: + "(");
0545: String inputName = "none";
0546: try {
0547: inputName = operation.getInput().getName();
0548: } catch (Exception e) {
0549: //ignore
0550: }
0551: builder.append("input=\"").append(inputName).append("\"");
0552: String outputName = "none";
0553: try {
0554: outputName = operation.getOutput().getName();
0555: } catch (Exception e) {
0556: //ignore
0557: }
0558: builder.append(", output=\"").append(outputName).append("\"");
0559: builder.append(")");
0560: return builder.toString();
0561: }
0562:
0563: @SuppressWarnings("unchecked")
0564: public static Map<String, String> getPrefixes(
0565: WSDLComponent wsdlComponent) {
0566: AbstractDocumentComponent comp = ((AbstractDocumentComponent) wsdlComponent);
0567: Map<String, String> prefixes = comp.getPrefixes();
0568: while (comp.getParent() != null) {
0569: comp = (AbstractDocumentComponent) comp.getParent();
0570: prefixes.putAll(comp.getPrefixes());
0571: }
0572:
0573: return prefixes;
0574: }
0575:
0576: public static void splitExtensibilityElements(
0577: List<ExtensibilityElement> list,
0578: Set<String> specialTargetNamespaces,
0579: List<ExtensibilityElement> specialExtensibilityElements,
0580: List<ExtensibilityElement> nonSpecialExtensibilityElements) {
0581: if (specialExtensibilityElements == null) {
0582: specialExtensibilityElements = new ArrayList<ExtensibilityElement>();
0583: }
0584: if (nonSpecialExtensibilityElements == null) {
0585: nonSpecialExtensibilityElements = new ArrayList<ExtensibilityElement>();
0586: }
0587:
0588: if (list != null) {
0589: for (ExtensibilityElement element : list) {
0590: if (specialTargetNamespaces.contains(element.getQName()
0591: .getNamespaceURI())) {
0592: specialExtensibilityElements.add(element);
0593: } else {
0594: nonSpecialExtensibilityElements.add(element);
0595: }
0596: }
0597: }
0598: }
0599:
0600: public static List<ExtensibilityElement> getSpecialExtensibilityElements(
0601: List<ExtensibilityElement> list, String specialNamespace) {
0602: List<ExtensibilityElement> specialList = new ArrayList<ExtensibilityElement>();
0603: if (list != null) {
0604: for (ExtensibilityElement element : list) {
0605: if (specialNamespace.equals(element.getQName()
0606: .getNamespaceURI())) {
0607: specialList.add(element);
0608: }
0609: }
0610: }
0611: return specialList;
0612: }
0613:
0614: /**
0615: * This method finds the absolute index in the definitions where the component needs to be inserted,
0616: * such that the component is at given index with respect to its kind.
0617: *
0618: * For example, There are 5 messages, and one needs to insert another at index 4. Then this method will insert
0619: * it at some index on Definitions, which will make it look like the 4th Message.
0620: *
0621: * it doesnt call startTransaction or endTransaction, so its the responsibility of the caller to do it.
0622: *
0623: * @param index
0624: * @param model
0625: * @param compToInsert
0626: * @param propertyName
0627: */
0628: public static void insertIntoDefinitionsAtIndex(int index,
0629: WSDLModel model, WSDLComponent compToInsert,
0630: String propertyName) {
0631: assert model.isIntransaction() : "Need to call startTransaction on this model, before calling this method";
0632: //find index among all definitions elements.
0633: //for inserting at index = 5, find index of the 4th PLT and insert after this index
0634: int defIndex = -1;
0635: int indexOfPreviousPLT = index - 1;
0636: List<WSDLComponent> comps = model.getDefinitions()
0637: .getChildren();
0638: for (int i = 0; i < comps.size(); i++) {
0639: WSDLComponent comp = comps.get(i);
0640: if (compToInsert.getClass().isAssignableFrom(
0641: comp.getClass())) {
0642: if (indexOfPreviousPLT > defIndex) {
0643: defIndex++;
0644: } else {
0645: ((AbstractComponent<WSDLComponent>) model
0646: .getDefinitions()).insertAtIndex(
0647: propertyName, compToInsert, i);
0648: break;
0649: }
0650: }
0651: }
0652: }
0653:
0654: /* Similiar logic can be found in SchemaImportsGenerator.processImports(). So if there are changes here, also change in SchemaImportsGenerator*/
0655: public static void addSchemaImport(SchemaComponent comp,
0656: WSDLModel wsdlModel) {
0657: if (comp != null && wsdlModel != null
0658: && comp.getModel() != null) {
0659:
0660: if (comp instanceof ReferenceableSchemaComponent) {
0661: ReferenceableSchemaComponent rsc = (ReferenceableSchemaComponent) comp;
0662: String localName = rsc.getName();
0663: String namespace = rsc.getModel().getSchema()
0664: .getTargetNamespace();
0665: if (namespace != null) {
0666: QName qname = new QName(namespace, localName);
0667: if (rsc instanceof GlobalElement) {
0668: GlobalElement element = findGlobalElement(
0669: wsdlModel, qname);
0670: if (element != null && element.equals(rsc)) {
0671: return;
0672: }
0673: } else if (rsc instanceof GlobalType) {
0674: GlobalType type = findGlobalType(wsdlModel,
0675: qname);
0676: if (type != null && type.equals(rsc)) {
0677: return;
0678: }
0679: }
0680: }
0681: }
0682:
0683: addSchemaImport(comp.getModel(), wsdlModel);
0684: }
0685: }
0686:
0687: /* Similiar logic can be found in SchemaImportsGenerator.processImports(). So if there are changes here, also change in SchemaImportsGenerator*/
0688: public static org.netbeans.modules.xml.schema.model.Import addSchemaImport(
0689: SchemaModel impSchemaModel, WSDLModel wsdlModel) {
0690: Map<String, String> existingLocationToNamespaceMap = new HashMap<String, String>();
0691:
0692: FileObject wsdlFileObj = wsdlModel.getModelSource().getLookup()
0693: .lookup(FileObject.class);
0694: URI wsdlFileURI = FileUtil.toFile(wsdlFileObj).toURI();
0695:
0696: Definitions def = wsdlModel.getDefinitions();
0697: Types types = def.getTypes();
0698: if (types == null) {
0699: types = wsdlModel.getFactory().createTypes();
0700: def.setTypes(types);
0701: }
0702:
0703: Schema defaultInlineSchema = null;
0704: String wsdlTNS = def.getTargetNamespace();
0705: if (wsdlTNS != null) {
0706: Collection<Schema> schmas = types.getSchemas();
0707: if (schmas != null) {
0708: for (Schema s : schmas) {
0709: if (s.getTargetNamespace() != null
0710: && s.getTargetNamespace().equals(wsdlTNS)) {
0711: defaultInlineSchema = s;
0712: break;
0713: }
0714: }
0715: }
0716: }
0717:
0718: WSDLSchema wsdlSchema = null;
0719: if (defaultInlineSchema == null) {
0720: wsdlSchema = wsdlModel.getFactory().createWSDLSchema();
0721: SchemaModel schemaModel = wsdlSchema.getSchemaModel();
0722: defaultInlineSchema = schemaModel.getSchema();
0723: defaultInlineSchema.setTargetNamespace(wsdlTNS);
0724: }
0725:
0726: //if any import with same namespace is present, dont import it.
0727: Collection<org.netbeans.modules.xml.schema.model.Import> imports = defaultInlineSchema
0728: .getImports();
0729: for (org.netbeans.modules.xml.schema.model.Import imp : imports) {
0730: existingLocationToNamespaceMap.put(imp.getSchemaLocation(),
0731: imp.getNamespace());
0732: }
0733:
0734: Collection<Schema> schemas = types.getSchemas();
0735: if (schemas != null) {
0736: for (Schema schema : schemas) {
0737: Collection<org.netbeans.modules.xml.schema.model.Import> schemaImports = schema
0738: .getImports();
0739: for (org.netbeans.modules.xml.schema.model.Import imp : schemaImports) {
0740: existingLocationToNamespaceMap.put(imp
0741: .getSchemaLocation(), imp.getNamespace());
0742: }
0743: }
0744: }
0745:
0746: if (impSchemaModel != null) {
0747:
0748: String schemaTNS = impSchemaModel.getSchema()
0749: .getTargetNamespace();
0750: if (schemaTNS != null
0751: && !schemaTNS
0752: .equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
0753:
0754: FileObject fo = impSchemaModel.getModelSource()
0755: .getLookup().lookup(FileObject.class);
0756:
0757: if (fo != null) {
0758: String path = null;
0759: //should be different files. in case of inline schemas.
0760: if (!FileUtil.toFile(fo).toURI()
0761: .equals(wsdlFileURI)) {
0762: DefaultProjectCatalogSupport catalogSupport = DefaultProjectCatalogSupport
0763: .getInstance(wsdlFileObj);
0764: if (catalogSupport.needsCatalogEntry(
0765: wsdlFileObj, fo)) {
0766: // Remove the previous catalog entry, then create new one.
0767: URI uri;
0768: try {
0769: uri = catalogSupport.getReferenceURI(
0770: wsdlFileObj, fo);
0771: catalogSupport.removeCatalogEntry(uri);
0772: catalogSupport.createCatalogEntry(
0773: wsdlFileObj, fo);
0774: path = catalogSupport.getReferenceURI(
0775: wsdlFileObj, fo).toString();
0776: } catch (URISyntaxException use) {
0777: ErrorManager.getDefault().notify(use);
0778: } catch (IOException ioe) {
0779: ErrorManager.getDefault().notify(ioe);
0780: } catch (CatalogModelException cme) {
0781: ErrorManager.getDefault().notify(cme);
0782: }
0783: } else {
0784: path = RelativePath.getRelativePath(
0785: FileUtil.toFile(wsdlFileObj)
0786: .getParentFile(), FileUtil
0787: .toFile(fo));
0788: }
0789: }
0790: if (path != null
0791: && (!existingLocationToNamespaceMap
0792: .containsKey(path)
0793: || existingLocationToNamespaceMap
0794: .get(path) == null || !existingLocationToNamespaceMap
0795: .get(path).equals(schemaTNS))) {
0796: org.netbeans.modules.xml.schema.model.Import schemaImport = defaultInlineSchema
0797: .getModel().getFactory().createImport();
0798: schemaImport.setNamespace(schemaTNS);
0799: schemaImport.setSchemaLocation(path);
0800: defaultInlineSchema
0801: .addExternalReference(schemaImport);
0802: if (wsdlSchema != null) {
0803: types.addExtensibilityElement(wsdlSchema);
0804: }
0805: return schemaImport;
0806: }
0807: }
0808: }
0809: }
0810: return null;
0811:
0812: }
0813:
0814: /**
0815: * Adds a import statement importing the wsdl comprising of the provided wsdl component.
0816: * Does not initiate or end transactions.
0817: *
0818: * @param comp the wsdl component
0819: * @param recievingModel importing wsdl model
0820: */
0821: public static void addWSDLImport(WSDLComponent comp,
0822: WSDLModel wsdlModel) {
0823: if (comp != null && wsdlModel != null
0824: && comp.getModel() != null) {
0825: addWSDLImport(comp.getModel(), wsdlModel);
0826: }
0827: }
0828:
0829: /**
0830: * Adds a import statement importing the provided wsdl model.
0831: * Does not initiate or end transactions.
0832: *
0833: * @param modelToBeImported wsdl model to be imported
0834: * @param recievingModel importing wsdl model
0835: * @return the newly create import.
0836: */
0837: public static Import addWSDLImport(WSDLModel modelToBeImported,
0838: WSDLModel recievingModel) {
0839: assert modelToBeImported != null;
0840: assert recievingModel != null;
0841:
0842: //cannot be same model
0843: if (modelToBeImported == recievingModel)
0844: return null;
0845:
0846: String importedWSDLTargetNamespace = modelToBeImported
0847: .getDefinitions().getTargetNamespace();
0848:
0849: if (importedWSDLTargetNamespace != null) {
0850: Import wsdlImport = recievingModel.getFactory()
0851: .createImport();
0852: wsdlImport.setNamespace(importedWSDLTargetNamespace);
0853:
0854: FileObject wsdlFileObj = recievingModel.getModelSource()
0855: .getLookup().lookup(FileObject.class);
0856: URI wsdlFileURI = FileUtil.toFile(wsdlFileObj).toURI();
0857:
0858: FileObject fo = modelToBeImported.getModelSource()
0859: .getLookup().lookup(FileObject.class);
0860: String path = null;
0861: if (!FileUtil.toFile(fo).toURI().equals(wsdlFileURI)) {
0862: DefaultProjectCatalogSupport catalogSupport = DefaultProjectCatalogSupport
0863: .getInstance(wsdlFileObj);
0864: if (catalogSupport.supportsCrossProject())
0865: if (catalogSupport.needsCatalogEntry(wsdlFileObj,
0866: fo)) {
0867: // Remove the previous catalog entry, then create new one.
0868: URI uri;
0869: try {
0870: uri = catalogSupport.getReferenceURI(
0871: wsdlFileObj, fo);
0872: catalogSupport.removeCatalogEntry(uri);
0873: catalogSupport.createCatalogEntry(
0874: wsdlFileObj, fo);
0875: path = catalogSupport.getReferenceURI(
0876: wsdlFileObj, fo).toString();
0877: } catch (URISyntaxException use) {
0878: ErrorManager.getDefault().notify(use);
0879: } catch (IOException ioe) {
0880: ErrorManager.getDefault().notify(ioe);
0881: } catch (CatalogModelException cme) {
0882: ErrorManager.getDefault().notify(cme);
0883: }
0884: } else {
0885: path = RelativePath.getRelativePath(FileUtil
0886: .toFile(wsdlFileObj).getParentFile(),
0887: FileUtil.toFile(fo));
0888: }
0889: }
0890:
0891: if (path != null) {
0892: wsdlImport.setLocation(path);
0893: Collection<Import> wsdlImports = recievingModel
0894: .getDefinitions().getImports();
0895: for (Import wsdlImp : wsdlImports) {
0896: String namespace = wsdlImp.getNamespace();
0897: if (namespace != null
0898: && namespace
0899: .equals(importedWSDLTargetNamespace)) {
0900: return wsdlImp; //already imported.
0901: }
0902: }
0903: }
0904: recievingModel.getDefinitions().addImport(wsdlImport);
0905: return wsdlImport;
0906: }
0907: return null;
0908:
0909: }
0910:
0911: public static boolean canImport(Model modelToBeImported,
0912: WSDLModel receivingModel) {
0913: assert modelToBeImported != null;
0914: assert receivingModel != null;
0915:
0916: //cannot be same model
0917: if (modelToBeImported == receivingModel)
0918: return false;
0919:
0920: FileObject wsdlFileObj = receivingModel.getModelSource()
0921: .getLookup().lookup(FileObject.class);
0922: URI wsdlFileURI = FileUtil.toFile(wsdlFileObj).toURI();
0923:
0924: FileObject fo = modelToBeImported.getModelSource().getLookup()
0925: .lookup(FileObject.class);
0926: if (!FileUtil.toFile(fo).toURI().equals(wsdlFileURI)) {
0927:
0928: Project receivingProject = FileOwnerQuery
0929: .getOwner(wsdlFileObj);
0930: Project toBeImportedProject = FileOwnerQuery.getOwner(fo);
0931:
0932: if (receivingProject.getProjectDirectory().equals(
0933: toBeImportedProject.getProjectDirectory()))
0934: return true;
0935:
0936: DefaultProjectCatalogSupport rCatalogSupport = DefaultProjectCatalogSupport
0937: .getInstance(wsdlFileObj);
0938:
0939: if (!rCatalogSupport.supportsCrossProject())
0940: return false;
0941:
0942: Set projectRefs = rCatalogSupport.getProjectReferences();
0943: if (projectRefs == null || projectRefs.isEmpty())
0944: return false;
0945:
0946: Iterator projectRefsIter = projectRefs.iterator();
0947: while (projectRefsIter.hasNext()) {
0948: Project proj = (Project) projectRefsIter.next();
0949: if (proj.getProjectDirectory().equals(
0950: toBeImportedProject.getProjectDirectory()))
0951: return true;
0952: }
0953:
0954: /*DefaultProjectCatalogSupport tCatalogSupport = DefaultProjectCatalogSupport.getInstance(fo);
0955:
0956: if (!tCatalogSupport.supportsCrossProject()) return false;*/
0957:
0958: }
0959:
0960: return false;
0961: }
0962:
0963: /**
0964: * Does basic escaping of html characters. and is not complete.
0965: * replaces & => &
0966: * < => <
0967: * > => >
0968: * and all spaces with
0969: *
0970: * The best solution is probably to use commons lang, StringEscapeUtils.escapeHtml();
0971: * @param str
0972: * @return the escaped string.
0973: */
0974: public static String escapeHtml(String str) {
0975: if (str == null || str.length() == 0)
0976: return str;
0977: return str.replaceAll("&", "&").replaceAll("<", "<")
0978: .replaceAll(">", ">").replaceAll("\\s", " ");
0979: }
0980:
0981: /**
0982: * Gets all the nodes from the transferable.
0983: *
0984: * @param t the Transferable
0985: * @return all the nodes for action NodeTransfer.COPY and NodeTransfer.MOVE
0986: */
0987: public static Node[] getNodes(Transferable t) {
0988: Node[] nodes = NodeTransfer.nodes(t, NodeTransfer.COPY
0989: | NodeTransfer.MOVE);
0990: if (nodes == null) {
0991: nodes = new Node[0];
0992: }
0993: return nodes;
0994: }
0995:
0996: /**
0997: * Get the palette item node from the transferable
0998: *
0999: * @param t the transferable
1000: * @return the palette item node if any. otherwise null
1001: *
1002: */
1003: public static Node getPaletteNode(Transferable t) {
1004: if (t.isDataFlavorSupported(PaletteController.ITEM_DATA_FLAVOR)) {
1005: Lookup lookup;
1006: try {
1007: lookup = (Lookup) t
1008: .getTransferData(PaletteController.ITEM_DATA_FLAVOR);
1009: return lookup.lookup(Node.class);
1010: } catch (Exception e) {
1011: //ignore
1012: }
1013: }
1014: return null;
1015: }
1016:
1017: public static String getNamespace(WSDLComponent comp) {
1018: return comp.getModel().getDefinitions().getTargetNamespace();
1019: }
1020:
1021: public static String getNamespacePrefix(WSDLComponent comp,
1022: WSDLModel model) {
1023: String ns = getNamespace(comp);
1024: return getNamespacePrefix(ns, model);
1025: }
1026:
1027: /**
1028: * Creates a QName for the given wsdl component. The WSDLComponent needs to be Named.
1029: * The model can be different from the wsdl component's model.
1030: *
1031: * @param comp given wsdl component
1032: * @param model given model
1033: * @return QName for the wsdl component
1034: */
1035: public static QName getQNameForWSDLComponent(WSDLComponent comp,
1036: WSDLModel model) {
1037: if (!Named.class.isInstance(comp))
1038: return null;
1039: String localPart = ((Named) comp).getName();
1040: String namespace = getNamespace(comp);
1041: String prefix = getNamespacePrefix(namespace, model);
1042: if (localPart != null) {
1043: if (namespace != null) {
1044: if (prefix != null) {
1045: return new QName(namespace, localPart, prefix);
1046: }
1047: return new QName(namespace, localPart);
1048: }
1049: return new QName(localPart);
1050: }
1051:
1052: return null;
1053: }
1054:
1055: /**
1056: * Utility method to get WSDL and/or Schema Paste type.
1057: *
1058: * @param dObj DataObjcet to be imported
1059: * @param currModel the importing wsdl model
1060: * @param schema whether schema paste type needs to be generated.
1061: * @param wsdl whether wsdl paste type needs to generated.
1062: * @return the paste type.
1063: */
1064: public static PasteType getWSDLOrSchemaPasteType(DataObject dObj,
1065: WSDLModel currModel, boolean schema, boolean wsdl) {
1066: String mimeType = FileUtil.getMIMEType(dObj.getPrimaryFile());
1067: if (mimeType == null)
1068: return null;
1069: if (wsdl && mimeType.contains("wsdl")) {
1070: WSDLModelCookie wmCookie = dObj
1071: .getCookie(WSDLModelCookie.class);
1072: if (wmCookie != null) {
1073: WSDLModel impModel;
1074: try {
1075: impModel = wmCookie.getModel();
1076: if (!impModel.equals(currModel)) {
1077: return new WSDLImportPasteType(currModel,
1078: impModel);
1079: }
1080: } catch (IOException e) {
1081: //ignore
1082: }
1083: }
1084: } else if (schema && mimeType.contains("schema")) {
1085: SchemaModelCookie smCookie = dObj
1086: .getCookie(SchemaModelCookie.class);
1087: if (smCookie != null) {
1088: SchemaModel impModel = null;
1089: try {
1090: impModel = smCookie.getModel();
1091: return new SchemaImportPasteType(currModel,
1092: impModel);
1093: } catch (IOException e) {
1094: //ignore
1095: }
1096:
1097: }
1098: }
1099: return null;
1100: }
1101:
1102: }
|