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.schema;
042:
043: import java.io.InputStream;
044: import javax.xml.parsers.DocumentBuilderFactory;
045: import javax.xml.parsers.ParserConfigurationException;
046: import javax.xml.validation.Schema;
047: import org.w3c.dom.DOMImplementation;
048: import org.w3c.dom.ls.DOMImplementationLS;
049: import org.w3c.dom.ls.LSInput;
050: import org.w3c.dom.ls.LSResourceResolver;
051:
052: import org.netbeans.modules.bpel.model.api.AnotherVersionBpelProcess;
053: import org.netbeans.modules.bpel.model.api.BpelEntity;
054: import org.netbeans.modules.bpel.model.api.BpelModel;
055: import org.netbeans.modules.bpel.model.api.resources.ResourcePackageMarker;
056: import org.netbeans.modules.bpel.model.ext.logging.api.Trace;
057: import org.netbeans.modules.xml.xam.Model;
058: import org.netbeans.modules.xml.xam.spi.Validation;
059: import org.netbeans.modules.xml.xam.spi.ValidationResult;
060: import org.netbeans.modules.xml.xam.spi.XsdBasedValidator;
061: import org.netbeans.modules.xml.xam.spi.Validation.ValidationType;
062: import static org.netbeans.modules.soa.ui.util.UI.*;
063:
064: public final class Validator extends XsdBasedValidator {
065:
066: @Override
067: public ValidationResult validate(Model model,
068: final Validation validation,
069: final Validation.ValidationType validationType) {
070: if (!(model instanceof BpelModel)) {
071: return null;
072: }
073: startTime();
074: ValidationResult result = Validator.super .validate(
075: (BpelModel) model, validation, validationType);
076: endTime("Validator " + getName() + " "); // NOI18N
077:
078: return result;
079: }
080:
081: public String getName() {
082: return getClass().getName();
083: }
084:
085: protected Schema getSchema(Model model) {
086: if (!(model instanceof BpelModel)) {
087: return null;
088: }
089: // # 90585
090: AnotherVersionBpelProcess process = ((BpelModel) model)
091: .getAnotherVersionProcess();
092:
093: if (process != null) {
094: String ns = process.getNamespaceUri();
095: // we have BPEL 1.1 process file, validate against his schema.
096: if (BpelEntity.BUSINESS_PROCESS_1_1_NS_URI.equals(ns)) {
097: return getBpel11Schema();
098: }
099: }
100: return getBpel20Schema();
101: }
102:
103: private Schema getBpel20Schema() {
104: if (compiledBPELSchema == null) {
105: compiledBPELSchema = getCompiledSchema(
106: new InputStream[] { Validator.class
107: .getResourceAsStream(BPEL_XSD_URL) },
108: new BPELEntityResolver());
109: }
110: return compiledBPELSchema;
111: }
112:
113: private Schema getBpel11Schema() {
114: if (BPEL_1_1_SCHEMA == null) {
115: BPEL_1_1_SCHEMA = getCompiledSchema(
116: new InputStream[] { Validator.class
117: .getResourceAsStream(BPEL_1_1_XSD_URL) },
118: new BPELEntityResolver());
119: }
120: return BPEL_1_1_SCHEMA;
121: }
122:
123: private class BPELEntityResolver implements LSResourceResolver {
124:
125: public BPELEntityResolver() {
126: }
127:
128: public LSInput resolveResource(String type,
129: String namespaceURI, String publicId, String systemId,
130: String baseURI) {
131: InputStream inputStream = null;
132:
133: if (systemId.equals(XML_XSD_SYSTEMID)) {
134: inputStream = Validator.class
135: .getResourceAsStream(XML_XSD_URL);
136: } else if (systemId.equals(XML_WSDL_SYSTEMID)) {
137: inputStream = Validator.class
138: .getResourceAsStream(XML_WSDL_URL);
139: } else if (systemId.equals(Trace.LOGGING_NAMESPACE_URI)) {
140: inputStream = Validator.class
141: .getResourceAsStream(TRACE_2_0_XSD_URL);
142: }
143: if (inputStream != null) {
144: DOMImplementation domImpl = null;
145: try {
146: domImpl = DocumentBuilderFactory.newInstance()
147: .newDocumentBuilder()
148: .getDOMImplementation();
149: } catch (ParserConfigurationException ex) {
150: return null;
151: }
152: DOMImplementationLS dols = (DOMImplementationLS) domImpl
153: .getFeature("LS", "3.0"); // NOI18N
154: LSInput lsi = dols.createLSInput();
155: lsi.setByteStream(inputStream);
156: return lsi;
157: }
158: return null;
159: }
160: }
161:
162: private static final String BPEL_XSD_URL = "/"
163: + ResourcePackageMarker.getPackage() + "/"
164: + ResourcePackageMarker.WS_BPEL_SCHEMA;
165: private static final String BPEL_1_1_XSD_URL = "/"
166: + ResourcePackageMarker.getPackage() + "/"
167: + ResourcePackageMarker.WS_BPEL_1_1_SCHEMA;
168: private static final String XML_XSD_URL = "/"
169: + ResourcePackageMarker.getPackage() + "/"
170: + ResourcePackageMarker.XSD_SCHEMA;
171: private static final String TRACE_2_0_XSD_URL = "/"
172: + ResourcePackageMarker.getPackage() + "/"
173: + ResourcePackageMarker.TRACE_SCHEMA;
174: private static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; // NOI18N
175: private static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; // NOI18N
176: private static final String XML_WSDL_URL = "nbres:/org/netbeans/modules/xml/wsdl/validator/resources/wsdl-2004-08-24.xsd"; // NOI18N
177: private static final String XML_XSD_SYSTEMID = "http://www.w3.org/2001/xml.xsd"; // NOI18N
178: private static final String XML_WSDL_SYSTEMID = "http://schemas.xmlsoap.org/wsdl/"; // NOI18N
179:
180: private static Schema compiledBPELSchema;
181: private static Schema BPEL_1_1_SCHEMA;
182: }
|