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-2006 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:
042: package org.netbeans.modules.xml.wsdl.model.visitor;
043:
044: import org.netbeans.modules.xml.wsdl.model.Binding;
045: import org.netbeans.modules.xml.wsdl.model.Definitions;
046: import org.netbeans.modules.xml.wsdl.model.ExtensibilityElement;
047: import org.netbeans.modules.xml.wsdl.model.Message;
048: import org.netbeans.modules.xml.wsdl.model.NotificationOperation;
049: import org.netbeans.modules.xml.wsdl.model.OneWayOperation;
050: import org.netbeans.modules.xml.wsdl.model.Part;
051: import org.netbeans.modules.xml.wsdl.model.Port;
052: import org.netbeans.modules.xml.wsdl.model.PortType;
053: import org.netbeans.modules.xml.wsdl.model.ReferenceableWSDLComponent;
054: import org.netbeans.modules.xml.wsdl.model.RequestResponseOperation;
055: import org.netbeans.modules.xml.wsdl.model.Service;
056: import org.netbeans.modules.xml.wsdl.model.SolicitResponseOperation;
057: import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
058:
059: /**
060: *
061: * @author Nam Nguyen
062: */
063: public class FindReferencedVisitor<T extends ReferenceableWSDLComponent>
064: extends DefaultVisitor {
065: private Class<T> type;
066: private String localName;
067: private T referenced = null;
068: private Definitions root;
069:
070: /** Creates a new instance of FindReferencedVisitor */
071: public FindReferencedVisitor(Definitions root) {
072: this .root = root;
073: }
074:
075: public T find(String localName, Class<T> type) {
076: this .type = type;
077: this .localName = localName;
078: visitChildren(root);
079: return referenced;
080: }
081:
082: public void visit(Binding c) {
083: checkReference(c, true); //extensible
084: }
085:
086: public void visit(Message c) { //extensible
087: checkReference(c, true);
088: }
089:
090: public void visit(PortType c) { //extensible
091: checkReference(c, true);
092: }
093:
094: public void visit(RequestResponseOperation op) {
095: checkReference(op, true);
096: }
097:
098: public void visit(OneWayOperation op) {
099: checkReference(op, true);
100: }
101:
102: public void visit(NotificationOperation op) {
103: checkReference(op, true);
104: }
105:
106: public void visit(SolicitResponseOperation op) {
107: checkReference(op, true);
108: }
109:
110: public void visit(Service s) {
111: checkReference(s, true);
112: }
113:
114: public void visit(Port p) {
115: checkReference(p, true);
116: }
117:
118: public void visit(Part part) {
119: checkReference(part, true);
120: }
121:
122: public void visit(ExtensibilityElement c) {
123: if (c instanceof ReferenceableWSDLComponent) {
124: checkReference(ReferenceableWSDLComponent.class.cast(c),
125: true);
126: }
127: visitChildren(c);
128: }
129:
130: private void checkReference(ReferenceableWSDLComponent c,
131: boolean checkChildren) {
132: if (type.isAssignableFrom(c.getClass()) && c.getName() != null
133: && c.getName().equals(localName)) {
134: referenced = type.cast(c);
135: return;
136: } else if (checkChildren) {
137: visitChildren(c);
138: }
139: }
140:
141: private void visitChildren(WSDLComponent c) {
142: for (WSDLComponent child : c.getChildren()) {
143: if (referenced != null) { // before start each visit
144: return;
145: }
146: child.accept(this);
147: }
148: }
149:
150: }
|