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.core;
042:
043: import java.util.ArrayList;
044: import java.util.Collections;
045: import java.util.HashSet;
046: import java.util.List;
047: import java.util.Set;
048:
049: import org.netbeans.modules.xml.xam.Component;
050: import org.netbeans.modules.xml.xam.Model;
051: import org.netbeans.modules.xml.xam.Named;
052: import org.netbeans.modules.xml.xam.dom.DocumentComponent;
053: import org.netbeans.modules.xml.xam.spi.Validation;
054: import org.netbeans.modules.xml.xam.spi.Validation.ValidationType;
055: import org.netbeans.modules.xml.xam.spi.ValidationResult;
056: import org.netbeans.modules.xml.xam.spi.Validator;
057: import org.netbeans.modules.xml.xam.spi.Validator.ResultItem;
058: import org.netbeans.modules.xml.xam.spi.Validator.ResultType;
059: import org.netbeans.modules.xml.schema.model.Schema;
060: import org.netbeans.modules.xml.schema.model.visitor.DeepSchemaVisitor;
061: import org.netbeans.modules.xml.schema.model.SchemaComponent;
062: import org.netbeans.modules.xml.schema.model.GlobalElement;
063: import org.netbeans.modules.xml.schema.model.GlobalType;
064: import org.netbeans.modules.xml.schema.model.SimpleType;
065: import org.netbeans.modules.xml.schema.model.TypeContainer;
066: import org.netbeans.modules.xml.schema.model.SchemaModel;
067: import org.netbeans.modules.xml.schema.model.GlobalComplexType;
068: import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
069: import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
070: import java.util.Collection;
071:
072: import org.netbeans.modules.bpel.model.api.BpelModel;
073: import org.netbeans.modules.bpel.model.api.Process;
074: import org.netbeans.modules.bpel.model.api.support.SimpleBpelModelVisitorAdaptor;
075: import static org.netbeans.modules.soa.ui.util.UI.*;
076:
077: /**
078: * @author Vladimir Yaroslavskiy
079: * @version 2007.05.03
080: */
081: public abstract class CoreValidator extends
082: SimpleBpelModelVisitorAdaptor implements Validator {
083:
084: public abstract ValidationResult validate(Model model,
085: Validation validation, ValidationType type);
086:
087: protected void init() {
088: }
089:
090: protected final String getDisplayName() {
091: String name = getName();
092: StringBuffer spaces = new StringBuffer();
093:
094: for (int i = name.length(); i < 57; i++) {
095: spaces.append(" "); // NOI18N
096: }
097: return "Validator " + name + spaces; // NOI18N
098: }
099:
100: public String getName() {
101: return getClass().getName();
102: }
103:
104: public Set<ResultItem> getResultItems() {
105: return myResultItems;
106: }
107:
108: protected final void validate(Model model) {
109: myValidation.validate(model, myType);
110: }
111:
112: protected final void init(Validation validation, ValidationType type) {
113: myType = type;
114: myValidation = validation;
115: myResultItems = new HashSet<ResultItem>();
116: init();
117: }
118:
119: protected final void addError(String key, Component component) {
120: //out("add error: " + key + " " + component);
121: addMessage(i18n(getClass(), key), ResultType.ERROR, component);
122: }
123:
124: protected final void addError(String key, Component component,
125: String param) {
126: addMessage(i18n(getClass(), key, param), ResultType.ERROR,
127: component);
128: }
129:
130: protected final void addError(String key, Component component,
131: String param1, String param2) {
132: //out("add error: " + key + " " + param1 + " " + param2);
133: //out(" msg: " + i18n(getClass(), key, param1, param2));
134: addMessage(i18n(getClass(), key, param1, param2),
135: ResultType.ERROR, component);
136: }
137:
138: protected final void addWarning(String key, Component component) {
139: addMessage(i18n(getClass(), key), ResultType.WARNING, component);
140: }
141:
142: protected final void addWarning(String key, Component component,
143: String param) {
144: addMessage(i18n(getClass(), key, param), ResultType.WARNING,
145: component);
146: }
147:
148: protected final void addMessage(String message, ResultType type,
149: Component component) {
150: addQuickFixable(component, type, message, null);
151: }
152:
153: protected final void addQuickFix(String key, Component component,
154: String param1, String param2, QuickFix quickFix) {
155: addQuickFixable(component, ResultType.ERROR, i18n(getClass(),
156: key, param1, param2), quickFix);
157: }
158:
159: private void addQuickFixable(Component component, ResultType type,
160: String message, QuickFix quickFix) {
161: myResultItems.add(new QuickFixable(this , type, component,
162: message, quickFix));
163: }
164:
165: protected final boolean isValidationComplete() {
166: return myType == ValidationType.COMPLETE;
167: }
168:
169: protected final String getTypeName(Component component) {
170: if (component == null) {
171: return "n/a"; // NOI18N
172: }
173: if (component instanceof Named) {
174: return ((Named) component).getName();
175: }
176: return component.toString();
177: }
178:
179: protected final String getName(Object component) {
180: if (component == null) {
181: return null;
182: }
183: if (component instanceof Named) {
184: String name = ((Named) component).getName();
185:
186: if (name != null) {
187: return name;
188: }
189: }
190: return component.getClass().getName();
191: }
192:
193: protected final Component getTypeOfElement(Component component) {
194: //out();
195: //out("GET TYPE: " + component);
196: GlobalType type = null;
197:
198: if (component instanceof TypeContainer) {
199: //out("1");
200: NamedComponentReference<? extends GlobalType> ref = ((TypeContainer) component)
201: .getType();
202:
203: if (ref != null) {
204: type = ref.get();
205:
206: if (type != null) {
207: //out("2");
208: return type;
209: }
210: }
211: }
212: //out("3");
213: if (component instanceof DocumentComponent
214: && component instanceof SchemaComponent) {
215: DocumentComponent document = (DocumentComponent) component;
216: String typeName = document.getPeer().getAttribute("type");
217: typeName = removePrefix(typeName);
218: type = findType(typeName, (SchemaComponent) component);
219: }
220: if (type != null) {
221: return type;
222: }
223: //out("4");
224: return component;
225: }
226:
227: private GlobalType findType(String typeName,
228: SchemaComponent component) {
229: //out("= findType: " + typeName);
230: if (typeName == null || typeName.equals("")) {
231: return null;
232: }
233: SchemaModel model = component.getModel();
234: Collection<Schema> schemas = model
235: .findSchemas("http://www.w3.org/2001/XMLSchema");
236: GlobalType type = null;
237:
238: for (Schema schema : schemas) {
239: type = findType(typeName, schema);
240:
241: if (type != null) {
242: return type;
243: }
244: }
245: return findType(typeName, model.getSchema());
246: }
247:
248: private GlobalType findType(final String typeName, Schema schema) {
249: //out();
250: //out("= in schema: " + schema.getTargetNamespace());
251: myGlobalType = null;
252:
253: schema.accept(new DeepSchemaVisitor() {
254:
255: @Override
256: public void visit(GlobalSimpleType type) {
257: //out(" see GLOBAL Simple TYPE : " + type.getName());
258: if (typeName.equals(type.getName())) {
259: //out("!!!=== FOUND GLOBAL Simple TYPE ==== : " + type.getName());
260: myGlobalType = type;
261: }
262: }
263:
264: @Override
265: public void visit(GlobalComplexType type) {
266: //out(" see GLOBAL Complex TYPE : " + type.getName());
267: if (typeName.equals(type.getName())) {
268: //out("!!!=== FOUND GLOBAL Complex TYPE ==== : " + type.getName());
269: myGlobalType = type;
270: }
271: }
272: });
273: return myGlobalType;
274: }
275:
276: private String removePrefix(String value) {
277: if (value == null) {
278: return null;
279: }
280: int k = value.indexOf(":");
281:
282: if (k == -1) {
283: return value;
284: }
285: return value.substring(k + 1);
286: }
287:
288: private void out() {
289: System.out.println();
290: }
291:
292: private void out(Object object) {
293: System.out.println("*** " + object); // NOI18N
294: }
295:
296: private ValidationType myType;
297: private Validation myValidation;
298: private GlobalType myGlobalType;
299: private Set<ResultItem> myResultItems;
300: }
|