001: package org.netbeans.modules.iep.model.common;
002:
003: /*
004: * TestCatalogModel.java
005: *
006: * Created on April 2, 2006, 10:41 AM
007: *
008: * To change this template, choose Tools | Template Manager
009: * and open the template in the editor.
010: */
011:
012: import java.io.File;
013: import java.io.FileInputStream;
014: import java.io.IOException;
015: import java.net.URI;
016: import java.util.HashMap;
017: import java.util.Map;
018: import java.util.logging.Logger;
019: import javax.swing.text.Document;
020: import org.netbeans.modules.xml.retriever.catalog.impl.CatalogFileWrapperDOMImpl;
021: import org.netbeans.modules.xml.retriever.catalog.impl.CatalogWriteModelImpl;
022: import org.netbeans.modules.xml.schema.model.SchemaModel;
023: import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
024: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
025: import org.netbeans.modules.xml.wsdl.model.WSDLModelFactory;
026: import org.netbeans.modules.xml.xam.locator.CatalogModel;
027: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
028: import org.netbeans.modules.xml.xam.ModelSource;
029: import org.openide.filesystems.FileObject;
030: import org.openide.filesystems.FileUtil;
031: import org.openide.loaders.DataObject;
032: import org.openide.loaders.DataObjectNotFoundException;
033: import org.openide.util.Lookup;
034: import org.openide.util.lookup.Lookups;
035:
036: /**
037: *
038: * @author girix
039: */
040:
041: public class TestCatalogModel extends CatalogWriteModelImpl {
042: private TestCatalogModel(File file) throws IOException {
043: super (file);
044: }
045:
046: static TestCatalogModel singletonCatMod = null;
047:
048: public static TestCatalogModel getDefault() {
049: if (singletonCatMod == null) {
050: CatalogFileWrapperDOMImpl.TEST_ENVIRONMENT = true;
051: try {
052: singletonCatMod = new TestCatalogModel(new File(System
053: .getProperty("java.io.tmpdir")));
054: FileObject catalogFO = singletonCatMod
055: .getCatalogFileObject();
056: File catFile = FileUtil.toFile(catalogFO);
057: catFile.deleteOnExit();
058: initCatalogFile();
059: } catch (Exception ex) {
060: ex.printStackTrace();
061: return null;
062: }
063: }
064: return singletonCatMod;
065: }
066:
067: /**
068: * This method could be overridden by the Unit testcase to return a special
069: * ModelSource object for a FileObject with custom impl of classes added to the lookup.
070: * This is optional if both getDocument(FO) and createCatalogModel(FO) are overridden.
071: */
072: protected ModelSource createModelSource(
073: final FileObject this FileObj, boolean editable)
074: throws CatalogModelException {
075: assert this FileObj != null : "Null file object.";
076: final CatalogModel catalogModel = createCatalogModel(this FileObj);
077: final DataObject dobj;
078: try {
079: dobj = DataObject.find(this FileObj);
080: } catch (DataObjectNotFoundException ex) {
081: throw new CatalogModelException(ex);
082: }
083: Lookup proxyLookup = Lookups.proxy(new Lookup.Provider() {
084: public Lookup getLookup() {
085: Document document = null;
086: Logger l = Logger.getLogger(getClass().getName());
087: document = getDocument(this FileObj);
088: return Lookups.fixed(new Object[] { this FileObj,
089: document, dobj, catalogModel });
090: }
091: });
092: return new ModelSource(proxyLookup, editable);
093: }
094:
095: private Document getDocument(FileObject fo) {
096: Document result = null;
097: if (documentPooling) {
098: result = documentPool().get(fo);
099: }
100: if (result != null)
101: return result;
102: try {
103:
104: File file = FileUtil.toFile(fo);
105: FileInputStream fis = new FileInputStream(file);
106: byte buffer[] = new byte[fis.available()];
107: result = new org.netbeans.editor.BaseDocument(
108: org.netbeans.modules.xml.text.syntax.XMLKit.class,
109: false);
110: result.remove(0, result.getLength());
111: fis.read(buffer);
112: fis.close();
113: String str = new String(buffer);
114: result.insertString(0, str, null);
115:
116: } catch (Exception dObjEx) {
117: return null;
118: }
119: if (documentPooling) {
120: documentPool().put(fo, result);
121: }
122: return result;
123: }
124:
125: protected CatalogModel createCatalogModel(FileObject fo)
126: throws CatalogModelException {
127: return getDefault();
128: }
129:
130: public ModelSource createTestModelSource(FileObject fo,
131: boolean editable) throws CatalogModelException {
132: final DataObject dobj;
133: final CatalogModel catalogModel = createCatalogModel(fo);
134: try {
135: dobj = DataObject.find(fo);
136: } catch (DataObjectNotFoundException ex) {
137: throw new CatalogModelException(ex);
138: }
139: Lookup lookup = Lookups.proxy(new Lookup.Provider() {
140: public Lookup getLookup() {
141: return Lookups.fixed(new Object[] {
142: dobj.getPrimaryFile(),
143: getDocument(dobj.getPrimaryFile()), dobj,
144: catalogModel });
145:
146: }
147: });
148: return new ModelSource(lookup, editable);
149: }
150:
151: public void addNamespace(NamespaceLocation nl) throws Exception {
152: this .addURI(nl.getLocationURI(), nl.getResourceURI());
153: }
154:
155: public SchemaModel getSchemaModel(NamespaceLocation nl)
156: throws Exception {
157: return getSchemaModel(nl.getLocationURI());
158: }
159:
160: public SchemaModel getSchemaModel(URI lcationURI) throws Exception {
161: SchemaModel model = SchemaModelFactory.getDefault().getModel(
162: singletonCatMod.getModelSource(lcationURI));
163: return model;
164: }
165:
166: private Map<FileObject, Document> fileToDocumentMap;
167:
168: private Map<FileObject, Document> documentPool() {
169: if (fileToDocumentMap == null) {
170: fileToDocumentMap = new HashMap<FileObject, Document>();
171: }
172: return fileToDocumentMap;
173: }
174:
175: private boolean documentPooling = true;
176:
177: public void setDocumentPooling(boolean v) {
178: documentPooling = v;
179: if (!documentPooling) {
180: clearDocumentPool();
181: }
182: }
183:
184: public void clearDocumentPool() {
185: fileToDocumentMap = null;
186: }
187:
188: private static void initCatalogFile() throws Exception {
189: TestCatalogModel instance = singletonCatMod;
190: for (NamespaceLocation nl : NamespaceLocation.values()) {
191: instance.addNamespace(nl);
192: }
193: }
194:
195: public WSDLModel getWSDLModel(URI locationURI) throws Exception {
196: ModelSource source = getDefault().getModelSource(locationURI);
197: WSDLModel model = WSDLModelFactory.getDefault()
198: .getModel(source);
199: return model;
200: }
201:
202: public WSDLModel getWSDLModel(NamespaceLocation nl)
203: throws Exception {
204: nl.refreshResourceFile();
205: return getWSDLModel(nl.getLocationURI());
206: }
207:
208: public String toString() {
209: return "TestCatalogModel" + super.toString();
210: }
211: }
|