001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.bpel.refactoring;
042:
043: import java.util.List;
044: import javax.xml.namespace.QName;
045:
046: import org.netbeans.modules.xml.schema.model.GlobalElement;
047: import org.netbeans.modules.xml.schema.model.GlobalType;
048: import org.netbeans.modules.xml.schema.model.SchemaModel;
049:
050: import org.netbeans.modules.xml.xam.Component;
051: import org.netbeans.modules.xml.xam.Model;
052: import org.netbeans.modules.xml.xam.Named;
053: import org.netbeans.modules.xml.xam.Referenceable;
054: import org.netbeans.modules.xml.xam.Reference;
055: import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
056:
057: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
058: import org.netbeans.modules.xml.wsdl.model.extensions.bpel.Query;
059: import static org.netbeans.modules.soa.ui.util.UI.*;
060:
061: /**
062: * @author Vladimir Yaroslavskiy
063: * @version 2006.06.27
064: */
065: final class Util {
066:
067: private Util() {
068: }
069:
070: public static void visit(NamedComponentReference<GlobalType> type,
071: NamedComponentReference<GlobalElement> element,
072: Referenceable target, Component component,
073: List<Component> usage) {
074: visit(type, target, component, usage);
075: visit(element, target, component, usage);
076: }
077:
078: public static void visit(Reference reference, Referenceable target,
079: Component component, List<Component> usage) {
080: if (reference == null || reference.get() == null) {
081: return;
082: }
083: //out();
084: //out(" Visit: " + getName(reference.get()));
085: //out(" target: " + target.getName());
086: if (target.equals(reference.get())) {
087: //out();
088: //out("AdD: " + getName(component));
089: usage.add(component);
090: }
091: }
092:
093: public static void visit(QName qName, Referenceable target,
094: Component component, List<Component> usage) {
095: //out();
096: //out("VISIT: " + qName);
097: if (target instanceof Named && contains(qName, (Named) target)) {
098: //out();
099: //out("ADd: " + getName(component));
100: usage.add(component);
101: }
102: }
103:
104: private static boolean contains(QName qName, Named target) {
105: if (qName == null) {
106: //out("qName is null");
107: return false;
108: }
109: String part = qName.getLocalPart();
110:
111: if (!part.equals(target.getName())) {
112: //out("Target name != part");
113: //out(" part: " + part);
114: //out(" Target name: " + target.getName());
115: return false;
116: }
117: return qName.getNamespaceURI().equals(
118: getNamespace(target.getModel()));
119: }
120:
121: private static String getNamespace(Model model) {
122: if (model instanceof SchemaModel) {
123: return ((SchemaModel) model).getSchema()
124: .getTargetNamespace();
125: }
126: if (model instanceof WSDLModel) {
127: return ((WSDLModel) model).getDefinitions()
128: .getTargetNamespace();
129: }
130: return null;
131: }
132:
133: public static String getName(Object component) {
134: if (component == null) {
135: return null;
136: }
137: if (component instanceof Named) {
138: String name = ((Named) component).getName();
139:
140: if (name != null) {
141: return name;
142: }
143: }
144: return component.getClass().getName();
145: }
146:
147: public static int checkQuery(Query query, String name) {
148: if (name == null || name.length() == 0) {
149: return -1;
150: }
151: if (query == null) {
152: return -1;
153: }
154: String path = query.getContent();
155:
156: if (path == null) {
157: return -1;
158: }
159: return checkQuery(path, name);
160: }
161:
162: private static int checkQuery(String path, String name) {
163: //out();
164: //out("path: " + path);
165: //out("name: " + name);
166: if (path.startsWith(name + "/")) {
167: return 0;
168: }
169: if (path.startsWith("/" + name + "/")) {
170: return 1;
171: }
172: int k = path.indexOf("/" + name + "/");
173:
174: if (k != -1) {
175: return k + 1;
176: }
177: k = path.indexOf(":" + name + "/");
178:
179: if (k != -1) {
180: return k + 1;
181: }
182: if (path.endsWith("/" + name)) {
183: return path.length() - name.length();
184: }
185: if (path.endsWith(":" + name)) {
186: return path.length() - name.length();
187: }
188: return -1;
189: }
190: }
|