KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > model > SchemaModelFactory


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
20 package org.netbeans.modules.xml.schema.model;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import javax.swing.text.BadLocationException JavaDoc;
24 import javax.swing.text.Document JavaDoc;
25 import org.netbeans.modules.xml.xam.ModelSource;
26 import org.netbeans.modules.xml.schema.model.impl.EmbeddedSchemaModelImpl;
27 import org.netbeans.modules.xml.schema.model.impl.SchemaModelImpl;
28 import org.netbeans.modules.xml.xam.AbstractModelFactory;
29 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
30 import org.netbeans.modules.xml.xam.dom.DocumentModel;
31 import org.openide.util.Lookup;
32 import org.openide.util.lookup.Lookups;
33 import org.w3c.dom.Element JavaDoc;
34
35 /**
36  *
37  * @author Vidhya Narayanan
38  */

39 public class SchemaModelFactory extends AbstractModelFactory<SchemaModel> {
40     
41     private static final SchemaModelFactory schemaModelFactory =
42         new SchemaModelFactory();
43     
44     private SchemaModel primitiveTypesSchema;
45     
46     /**
47      * Hidden constructor to create singleton SchemaModelFactory
48      */

49     private SchemaModelFactory() {
50     }
51     
52     public static SchemaModelFactory getDefault() {
53         return schemaModelFactory;
54     }
55      
56     public SchemaModel createEmbeddedSchemaModel(DocumentModel embeddingModel, Element schemaElement){
57         return new EmbeddedSchemaModelImpl(embeddingModel, schemaElement);
58     }
59     
60     public synchronized SchemaModel getPrimitiveTypesModel() {
61         if (primitiveTypesSchema == null) {
62             primitiveTypesSchema = createPrimitiveSchemaModel();
63         }
64         return primitiveTypesSchema;
65     }
66     
67     private SchemaModel createPrimitiveSchemaModel() {
68         javax.swing.text.Document JavaDoc d;
69         SchemaModel m;
70         try {
71             InputStream JavaDoc in = getClass().getResourceAsStream("primitiveTypesSchema.xsd"); //NOI18N
72
d = AbstractDocumentModel.getAccessProvider().loadSwingDocument(in);
73         ModelSource ms =
74         new ModelSource(Lookups.singleton(d), false);
75             m = new SchemaModelImpl(ms);
76             m.sync();
77         } catch (BadLocationException JavaDoc ex) {
78             throw new RuntimeException JavaDoc("writing into empty document failed",ex); //NOI18N
79
} catch (IOException JavaDoc ex) {
80             throw new RuntimeException JavaDoc("schema should be correct",ex); //NOI18N
81
}
82         return m;
83     }
84
85     /**
86      * Get model from given model source. Model source should at very least
87      * provide lookup for:
88      * 1. FileObject of the model source
89      * 2. DataObject represent the model
90      * 3. Swing Document buffer for in-memory text of the model source
91      */

92     public SchemaModel getModel(ModelSource modelSource) {
93         Lookup lookup = modelSource.getLookup();
94         assert lookup.lookup(Document JavaDoc.class) != null;
95         return super.getModel(modelSource);
96     }
97     
98     protected SchemaModel createModel(ModelSource modelSource) {
99         return new SchemaModelImpl(modelSource);
100     }
101 }
102
Popular Tags