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: package org.netbeans.modules.bpel.validation.reference;
042:
043: import java.util.ArrayList;
044: import java.util.Collection;
045: import java.util.Collections;
046: import java.util.HashSet;
047: import java.util.List;
048: import java.util.Set;
049:
050: import org.netbeans.modules.bpel.model.api.BpelContainer;
051: import org.netbeans.modules.bpel.model.api.BpelEntity;
052: import org.netbeans.modules.bpel.model.api.BpelModel;
053: import org.netbeans.modules.bpel.model.api.ContentElement;
054: import org.netbeans.modules.bpel.model.api.Process;
055: import org.netbeans.modules.bpel.model.api.Variable;
056: import org.netbeans.modules.bpel.model.api.VariableDeclaration;
057: import org.netbeans.modules.bpel.model.api.VariableDeclarationScope;
058: import org.netbeans.modules.bpel.model.api.references.BpelReference;
059: import org.netbeans.modules.bpel.model.api.references.ReferenceCollection;
060: import org.netbeans.modules.bpel.model.api.references.SchemaReference;
061: import org.netbeans.modules.bpel.model.api.references.WSDLReference;
062: import org.netbeans.modules.bpel.model.impl.references.MappedReference;
063: import org.netbeans.modules.bpel.model.impl.services.ExpressionUpdater;
064: import org.netbeans.modules.xml.xam.Component;
065: import org.netbeans.modules.xml.xam.Model;
066: import org.netbeans.modules.xml.xam.Reference;
067: import org.netbeans.modules.xml.xam.Referenceable;
068: import org.netbeans.modules.xml.xam.dom.Attribute;
069: import org.netbeans.modules.xml.xam.spi.ValidationResult;
070: import org.netbeans.modules.xml.xam.spi.Validation;
071: import org.netbeans.modules.xml.xam.spi.Validation.ValidationType;
072: import org.netbeans.modules.bpel.validation.core.BpelValidator;
073: import static org.netbeans.modules.soa.ui.util.UI.*;
074:
075: /**
076: * @author Vladimir Yaroslavskiy
077: * @version 2007.05.03
078: */
079: public final class Validator extends BpelValidator {
080:
081: @Override
082: public void visit(Process process) {
083: processEntity(process);
084: }
085:
086: private void processEntity(BpelEntity entity) {
087: checkReferenceCollection(entity);
088: checkExpressions(entity);
089: List<BpelEntity> children = entity.getChildren();
090:
091: for (BpelEntity child : children) {
092: processEntity(child);
093: }
094: }
095:
096: private void checkExpressions(BpelEntity entity) {
097: if (!(entity instanceof ContentElement)) {
098: return;
099: }
100: String expression = ((ContentElement) entity).getContent();
101: Collection<String> collection = ExpressionUpdater.getInstance()
102: .getUsedVariables(expression);
103: Set<String> set = new HashSet<String>(collection);
104: findDeclarationsAscendant(entity, set);
105:
106: if (set.size() == 0) {
107: return;
108: }
109: StringBuilder builder = new StringBuilder();
110:
111: for (String string : set) {
112: builder.append(string);
113: builder.append(", "); // NOI18N
114: }
115: String key;
116:
117: if (set.size() > 1) {
118: key = "FIX_Variables";
119: } else {
120: key = "FIX_Variable";
121: }
122: addError(key, entity, builder
123: .substring(0, builder.length() - 2), expression.trim());
124: }
125:
126: private void findDeclarationsAscendant(BpelEntity entity,
127: Set<String> set) {
128: if (set.size() == 0) {
129: return;
130: }
131: if (entity instanceof VariableDeclarationScope) {
132: findDeclarationsDescendant(entity, set);
133: }
134: // # 81027
135: BpelContainer parent = entity.getParent();
136:
137: if (parent != null) {
138: findDeclarationsAscendant(parent, set);
139: }
140: }
141:
142: private void findDeclarationsDescendant(BpelEntity entity,
143: Set<String> set) {
144: if (entity instanceof VariableDeclaration) {
145: String name = ((VariableDeclaration) entity)
146: .getVariableName();
147: set.remove(name);
148: }
149: List<Variable> list = entity.getChildren(Variable.class);
150:
151: if (list != null) {
152: for (Variable variable : list) {
153: String name = variable.getVariableName();
154: set.remove(name);
155: }
156: }
157: if (entity instanceof VariableDeclarationScope) {
158: List<VariableDeclarationScope> scopes = entity
159: .getChildren(VariableDeclarationScope.class);
160:
161: if (scopes == null) {
162: return;
163: }
164: for (VariableDeclarationScope scope : scopes) {
165: findDeclarationsDescendant(scope, set);
166: }
167: }
168: }
169:
170: @SuppressWarnings("unchecked")
171: private void checkReferenceCollection(BpelEntity entity) {
172: if (!(entity instanceof ReferenceCollection)) {
173: return;
174: }
175: ReferenceCollection collection = (ReferenceCollection) entity;
176: Reference[] refs = collection.getReferences();
177:
178: for (Reference reference : refs) {
179: if (reference == null) {
180: continue;
181: }
182: if (!reference.isBroken()) {
183: continue;
184: }
185: String tag = entity.getPeer().getLocalName();
186: String attr = ((MappedReference) reference).getAttribute()
187: .getName();
188:
189: addQuickFix("FIX_Reference", entity, tag, attr, QuickFix
190: .get(entity, (Reference<Referenceable>) reference));
191: }
192: }
193: }
|