001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.iep.model.common;
020:
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.net.URI;
024: import java.util.logging.Logger;
025:
026: import org.netbeans.editor.BaseDocument;
027: import org.netbeans.modules.iep.model.IEPModel;
028: import org.netbeans.modules.iep.model.IEPModelFactory;
029: import org.netbeans.modules.xml.retriever.catalog.impl.CatalogWriteModelImpl;
030: import org.netbeans.modules.xml.xam.ModelSource;
031: import org.netbeans.modules.xml.xam.locator.CatalogModel;
032: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
033: import org.openide.filesystems.FileObject;
034: import org.openide.filesystems.FileUtil;
035: import org.openide.loaders.DataObject;
036: import org.openide.loaders.DataObjectNotFoundException;
037: import org.openide.util.Lookup;
038: import org.openide.util.lookup.Lookups;
039:
040: /**
041: *
042: * @author radval
043: */
044: public class IEPModelProviderInsideIde extends CatalogWriteModelImpl
045: implements IEPModelProvider {
046:
047: /** Creates a new instance of BPELModelProviderInsideIde */
048: public IEPModelProviderInsideIde() {
049: }
050:
051: public IEPModel getWLMModel(URI locationURI) throws Exception {
052: System.out.println(toString() + " :" + locationURI);
053: ModelSource source = getModelSource(locationURI);
054: IEPModel model = IEPModelFactory.getDefault().getModel(source);
055: // model.sync();
056: return model;
057: }
058:
059: public ModelSource getModelSource(URI locationURI)
060: throws CatalogModelException {
061: File file = new File(locationURI);
062: FileObject this FileObj = FileUtil.toFileObject(file);
063: return createModelSource(this FileObj, true);
064: }
065:
066: public ModelSource getModelSource(URI locationURI,
067: ModelSource modelSourceOfSourceDocument)
068: throws CatalogModelException {
069: if (locationURI == null) {
070: return null;
071: }
072:
073: URI resolvedURI = locationURI;
074:
075: if (modelSourceOfSourceDocument != null) {
076: FileObject sFileObj = (FileObject) modelSourceOfSourceDocument
077: .getLookup().lookup(FileObject.class);
078: if (sFileObj != null) {
079: File sFile = FileUtil.toFile(sFileObj);
080: if (sFile != null) {
081: URI sURI = sFile.toURI();
082: resolvedURI = sURI.resolve(locationURI);
083: }
084: }
085: }
086:
087: if (resolvedURI != null) {
088: return getModelSource(resolvedURI);
089: }
090:
091: return null;
092: }
093:
094: protected BaseDocument getDocument(FileObject fo) {
095: BaseDocument result = null;
096:
097: try {
098:
099: File file = FileUtil.toFile(fo);
100: FileInputStream fis = new FileInputStream(file);
101: byte buffer[] = new byte[fis.available()];
102: result = new org.netbeans.editor.BaseDocument(
103: org.netbeans.modules.xml.text.syntax.XMLKit.class,
104: false);
105: // result = new javax.swing.text.PlainDocument();
106: result.remove(0, result.getLength());
107: fis.read(buffer);
108: fis.close();
109: String str = new String(buffer);
110: result.insertString(0, str, null);
111:
112: } catch (Exception dObjEx) {
113: return null;
114: }
115:
116: return result;
117: }
118:
119: protected CatalogModel createCatalogModel(FileObject fo)
120: throws CatalogModelException {
121: return this ;
122: }
123:
124: public ModelSource createModelSource(FileObject this FileObj,
125: boolean editable) throws CatalogModelException {
126: assert this FileObj != null : "Null file object.";
127: final DataObject dobj;
128: try {
129: dobj = DataObject.find(this FileObj);
130: } catch (DataObjectNotFoundException ex) {
131: throw new CatalogModelException(ex);
132: }
133: Lookup proxyLookup = Lookups.proxy(new Lookup.Provider() {
134: public Lookup getLookup() {
135: Logger l = Logger.getLogger(getClass().getName());
136: BaseDocument document = getDocument(dobj
137: .getPrimaryFile());
138: return Lookups.fixed(new Object[] {
139:
140: dobj.getPrimaryFile(), document, dobj,
141: IEPModelProviderInsideIde.this
142:
143: });
144: }
145: }
146:
147: );
148: return new ModelSource(proxyLookup, editable);
149: }
150:
151: }
|