001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.wsdl.model.extensions;
043:
044: import java.io.File;
045: import java.io.FileInputStream;
046: import java.io.IOException;
047: import java.net.URI;
048: import java.util.HashMap;
049: import java.util.Map;
050: import java.util.logging.Logger;
051: import javax.swing.text.Document;
052: import org.netbeans.modules.xml.retriever.catalog.impl.CatalogFileWrapperDOMImpl;
053: import org.netbeans.modules.xml.retriever.catalog.impl.CatalogWriteModelImpl;
054: import org.netbeans.modules.xml.schema.model.SchemaModel;
055: import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
056: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
057: import org.netbeans.modules.xml.wsdl.model.WSDLModelFactory;
058: import org.netbeans.modules.xml.xam.locator.CatalogModel;
059: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
060: import org.netbeans.modules.xml.xam.ModelSource;
061: import org.openide.filesystems.FileObject;
062: import org.openide.filesystems.FileUtil;
063: import org.openide.loaders.DataObject;
064: import org.openide.loaders.DataObjectNotFoundException;
065: import org.openide.util.Lookup;
066: import org.openide.util.lookup.Lookups;
067:
068: /**
069: *
070: * @author girix
071: */
072:
073: public class TestCatalogModel extends CatalogWriteModelImpl {
074: private TestCatalogModel(File file) throws IOException {
075: super (file);
076: }
077:
078: static TestCatalogModel singletonCatMod = null;
079:
080: public static TestCatalogModel getDefault() {
081: if (singletonCatMod == null) {
082: CatalogFileWrapperDOMImpl.TEST_ENVIRONMENT = true;
083: try {
084: singletonCatMod = new TestCatalogModel(new File(System
085: .getProperty("java.io.tmpdir")));
086: FileObject catalogFO = singletonCatMod
087: .getCatalogFileObject();
088: File catFile = FileUtil.toFile(catalogFO);
089: catFile.deleteOnExit();
090: initCatalogFile();
091: } catch (Exception ex) {
092: ex.printStackTrace();
093: return null;
094: }
095: }
096: return singletonCatMod;
097: }
098:
099: /**
100: * This method could be overridden by the Unit testcase to return a special
101: * ModelSource object for a FileObject with custom impl of classes added to the lookup.
102: * This is optional if both getDocument(FO) and createCatalogModel(FO) are overridden.
103: */
104: protected ModelSource createModelSource(
105: final FileObject this FileObj, boolean editable)
106: throws CatalogModelException {
107: assert this FileObj != null : "Null file object.";
108: final CatalogModel catalogModel = createCatalogModel(this FileObj);
109: final DataObject dobj;
110: try {
111: dobj = DataObject.find(this FileObj);
112: } catch (DataObjectNotFoundException ex) {
113: throw new CatalogModelException(ex);
114: }
115: Lookup proxyLookup = Lookups.proxy(new Lookup.Provider() {
116: public Lookup getLookup() {
117: Document document = null;
118: Logger l = Logger.getLogger(getClass().getName());
119: document = getDocument(this FileObj);
120: return Lookups.fixed(new Object[] { this FileObj,
121: document, dobj, catalogModel });
122: }
123: });
124: return new ModelSource(proxyLookup, editable);
125: }
126:
127: public void clearDocumentPool() {
128: fileToDocumentMap = null;
129: }
130:
131: private Document getDocument(FileObject fo) {
132: Document result = null;
133: if (documentPooling) {
134: result = documentPool().get(fo);
135: }
136: if (result != null)
137: return result;
138:
139: FileInputStream fis = null;
140: try {
141: File file = FileUtil.toFile(fo);
142: fis = new FileInputStream(file);
143: byte buffer[] = new byte[fis.available()];
144: result = new org.netbeans.editor.BaseDocument(
145: org.netbeans.modules.xml.text.syntax.XMLKit.class,
146: false);
147: result.remove(0, result.getLength());
148: fis.read(buffer);
149: String str = new String(buffer);
150: result.insertString(0, str, null);
151:
152: } catch (Exception dObjEx) {
153: return null;
154: } finally {
155: try {
156: if (fis != null)
157: fis.close();
158: } catch (IOException ioe) {
159: }
160: }
161: if (documentPooling) {
162: documentPool().put(fo, result);
163: }
164: return result;
165: }
166:
167: protected CatalogModel createCatalogModel(FileObject fo)
168: throws CatalogModelException {
169: return getDefault();
170: }
171:
172: public ModelSource createTestModelSource(FileObject this FileObj,
173: boolean readOnly) throws CatalogModelException {
174: Lookup lookup = Lookups.fixed(new Object[] { this FileObj,
175: getDocument(this FileObj),
176: createCatalogModel(this FileObj) });
177: return new ModelSource(lookup, readOnly);
178: }
179:
180: public void addNamespace(NamespaceLocation nl) throws Exception {
181: this .addURI(nl.getLocationURI(), nl.getResourceURI());
182: }
183:
184: public SchemaModel getSchemaModel(NamespaceLocation nl)
185: throws Exception {
186: nl.refreshResourceFile();
187: return getSchemaModel(nl.getLocationURI());
188: }
189:
190: public SchemaModel getSchemaModel(URI lcationURI) throws Exception {
191: SchemaModel model = SchemaModelFactory.getDefault().getModel(
192: singletonCatMod.getModelSource(lcationURI));
193: if (!documentPooling) {
194: model.sync(); // resync to restored to origin content
195: }
196: return model;
197: }
198:
199: private Map<FileObject, Document> fileToDocumentMap;
200:
201: private Map<FileObject, Document> documentPool() {
202: if (fileToDocumentMap == null) {
203: fileToDocumentMap = new HashMap<FileObject, Document>();
204: }
205: return fileToDocumentMap;
206: }
207:
208: private boolean documentPooling = false;
209:
210: public void setDocumentPooling(boolean v) {
211: documentPooling = v;
212: if (!documentPooling) {
213: fileToDocumentMap = null;
214: }
215: }
216:
217: private static void initCatalogFile() throws Exception {
218: TestCatalogModel instance = singletonCatMod;
219: for (NamespaceLocation nl : NamespaceLocation.values()) {
220: instance.addNamespace(nl);
221: }
222: }
223:
224: public WSDLModel getWSDLModel(URI locationURI) throws Exception {
225: ModelSource source = getDefault().getModelSource(locationURI);
226: WSDLModel model = WSDLModelFactory.getDefault()
227: .getModel(source);
228: return model;
229: }
230:
231: public WSDLModel getWSDLModel(NamespaceLocation nl)
232: throws Exception {
233: nl.refreshResourceFile();
234: return getWSDLModel(nl.getLocationURI());
235: }
236:
237: public String toString() {
238: return "TestCatalogModel" + super.toString();
239: }
240: }
|