KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > model > validation > SchemaXsdBasedValidator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.schema.model.validation;
20
21 import java.net.URI JavaDoc;
22 import java.net.URISyntaxException JavaDoc;
23 import java.util.logging.Level JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25 import javax.xml.XMLConstants JavaDoc;
26 import javax.xml.transform.Source JavaDoc;
27 import javax.xml.validation.Schema JavaDoc;
28 import javax.xml.validation.SchemaFactory JavaDoc;
29 import org.netbeans.modules.xml.schema.model.SchemaModel;
30 import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
31 import org.netbeans.modules.xml.xam.Model;
32 import org.netbeans.modules.xml.xam.ModelSource;
33 import org.netbeans.modules.xml.xam.dom.DocumentModel;
34 import org.netbeans.modules.xml.xam.locator.CatalogModel;
35 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
36 import org.netbeans.modules.xml.xam.locator.CatalogModelFactory;
37 import org.netbeans.modules.xml.xam.spi.Validator;
38 import org.netbeans.modules.xml.xam.spi.XsdBasedValidator;
39 import org.openide.util.NbBundle;
40 import org.w3c.dom.ls.LSInput JavaDoc;
41 import org.w3c.dom.ls.LSResourceResolver JavaDoc;
42 import org.xml.sax.SAXException JavaDoc;
43
44 /**
45  *
46  * @author Nam Nguyen
47  */

48 public class SchemaXsdBasedValidator extends XsdBasedValidator {
49     
50     private static Schema JavaDoc schema;
51     
52     protected Schema JavaDoc getSchema(Model model) {
53         if (! (model instanceof SchemaModel)) {
54             return null;
55         }
56         
57         // This will not be used as validate(.....) method is being overridden here.
58
// So just return a schema returned by newSchema().
59
if(schema == null) {
60             try {
61                 schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema();
62             } catch(SAXException JavaDoc ex) {
63                 assert false: "Error while creating compiled schema for"; //NOI18N
64
}
65         }
66         return schema;
67     }
68     
69     public String JavaDoc getName() {
70         return NbBundle.getMessage(SchemaXsdBasedValidator.class, "LBL_Schema_Validator");
71     }
72     
73     @Override JavaDoc
74     protected void validate(Model model, Schema JavaDoc schema, XsdBasedValidator.Handler handler) {
75         try {
76             SchemaFactory JavaDoc sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
77             CatalogModel cm = (CatalogModel) model.getModelSource().getLookup()
78         .lookup(CatalogModel.class);
79         if (cm != null) {
80                 sf.setResourceResolver(cm);
81             }
82             sf.setErrorHandler(handler);
83             Source JavaDoc saxSource = getSource(model, handler);
84             if (saxSource == null) {
85                 return;
86             }
87             sf.newSchema(saxSource);
88         } catch(SAXException JavaDoc sax) {
89             //already processed by handler
90
} catch(Exception JavaDoc ex) {
91             handler.logValidationErrors(Validator.ResultType.ERROR, ex.getMessage());
92         }
93     }
94     
95     public DocumentModel resolveResource(String JavaDoc systemId, Model currentModel) {
96         
97         try {
98             CatalogModel cm = (CatalogModel) currentModel.getModelSource().getLookup()
99             .lookup(CatalogModel.class);
100             ModelSource ms = cm.getModelSource(new URI JavaDoc(systemId));
101             if (ms != null) {
102                 return SchemaModelFactory.getDefault().getModel(ms);
103             }
104         } catch(URISyntaxException JavaDoc ex) {
105             Logger.getLogger(getClass().getName()).log(Level.FINE, "resolveResource", ex); //NOI18N
106
} catch(CatalogModelException ex) {
107             Logger.getLogger(getClass().getName()).log(Level.FINE, "resolveResource", ex); //NOI18N
108
}
109         return null;
110     }
111     
112 }
113
Popular Tags