1 19 20 package org.netbeans.modules.xml.schema.model; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import javax.swing.text.BadLocationException ; 24 import javax.swing.text.Document ; 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 ; 34 35 39 public class SchemaModelFactory extends AbstractModelFactory<SchemaModel> { 40 41 private static final SchemaModelFactory schemaModelFactory = 42 new SchemaModelFactory(); 43 44 private SchemaModel primitiveTypesSchema; 45 46 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 d; 69 SchemaModel m; 70 try { 71 InputStream in = getClass().getResourceAsStream("primitiveTypesSchema.xsd"); 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 ex) { 78 throw new RuntimeException ("writing into empty document failed",ex); } catch (IOException ex) { 80 throw new RuntimeException ("schema should be correct",ex); } 82 return m; 83 } 84 85 92 public SchemaModel getModel(ModelSource modelSource) { 93 Lookup lookup = modelSource.getLookup(); 94 assert lookup.lookup(Document .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 |