01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.ideaplugin.bean;
20:
21: import javax.xml.transform.stream.StreamSource;
22: import javax.xml.transform.dom.DOMSource;
23: import javax.xml.validation.Schema;
24: import javax.xml.validation.SchemaFactory;
25: import javax.xml.parsers.DocumentBuilderFactory;
26: import javax.xml.parsers.DocumentBuilder;
27:
28: import org.xml.sax.SAXException;
29: import org.xml.sax.InputSource;
30: import org.w3c.dom.Document;
31: import org.apache.axis2.tools.component.WizardPanel;
32:
33: import java.io.StringReader;
34:
35: /**
36: * this calss used for check service xml validation
37: */
38: public class ValidateXMLFile {
39:
40: public final String W3C_XML_SCHEMA_NS_URI = "http://www.w3.org/2001/XMLSchema";
41:
42: public boolean Validate(String args) {
43: try {
44: // define the type of schema get validation driver:
45: SchemaFactory schemafactory = SchemaFactory
46: .newInstance(W3C_XML_SCHEMA_NS_URI);
47:
48: // create schema by reading it from an XSD file:
49: java.net.URL resource = WizardPanel.class
50: .getResource("/resources/service.xsd");
51: Schema schema = schemafactory.newSchema(new StreamSource(
52: resource.getPath()));
53:
54: DocumentBuilderFactory factory = DocumentBuilderFactory
55: .newInstance();
56: factory.setNamespaceAware(true);
57: DocumentBuilder docBuilder = factory.newDocumentBuilder();
58: Document doc = docBuilder.parse(new InputSource(
59: new StringReader(args)));
60:
61: schema.newValidator().validate(new DOMSource(doc));
62:
63: return true;
64: } catch (SAXException ex) {
65: // ex.printStackTrace();
66: return false;
67: } catch (Exception ex) {
68: // ex.printStackTrace();
69: return false;
70: }
71:
72: }
73: }
|