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.xslt.project;
020:
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.io.IOException;
024:
025: import java.net.MalformedURLException;
026: import java.net.URI;
027: import java.net.URISyntaxException;
028:
029: import java.util.ArrayList;
030: import java.util.List;
031:
032: import javax.swing.text.Document;
033:
034: import org.apache.xml.resolver.Catalog;
035: import org.apache.xml.resolver.CatalogManager;
036: import org.apache.xml.resolver.tools.CatalogResolver;
037:
038: import org.w3c.dom.ls.LSInput;
039: import org.xml.sax.InputSource;
040: import org.xml.sax.SAXException;
041:
042: import org.netbeans.modules.xml.retriever.catalog.Utilities;
043: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
044: import org.netbeans.modules.xml.wsdl.model.WSDLModelFactory;
045: import org.netbeans.modules.xml.xam.ModelSource;
046: import org.netbeans.modules.xml.xam.locator.CatalogModel;
047: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
048: import org.netbeans.modules.xslt.tmap.model.api.TMapModel;
049: import org.netbeans.modules.xslt.tmap.model.spi.TMapModelFactory;
050:
051: import org.openide.util.lookup.Lookups;
052: import org.openide.util.Lookup;
053:
054: /**
055: *
056: * This class helps to obtain TMapModel from transformmap file URI
057: * @author Vitaly Bychkov
058: * @version 1.0
059: */
060: public class CommandlineTransformmapCatalogModel implements
061: CatalogModel {
062:
063: static CommandlineTransformmapCatalogModel singletonCatMod = null;
064: static File projectCatalogFileLocation = null;
065:
066: public CommandlineTransformmapCatalogModel() {
067: URI catalogFileLocPath = CommandlineXsltProjectXmlCatalogProvider
068: .getInstance().getProjectWideCatalogForWizard();
069: if (catalogFileLocPath != null) {
070: projectCatalogFileLocation = new File(catalogFileLocPath);
071: }
072: }
073:
074: /**
075: * Gets the instance of this class internal API
076: * @return current class instance
077: */
078: public static CommandlineTransformmapCatalogModel getDefault() {
079: if (singletonCatMod == null) {
080: singletonCatMod = new CommandlineTransformmapCatalogModel();
081: }
082: return singletonCatMod;
083: }
084:
085: private File getProjectCatalogXML() {
086: if (projectCatalogFileLocation != null
087: && projectCatalogFileLocation.exists()) {
088: return projectCatalogFileLocation;
089: } else {
090: return null;
091: }
092: }
093:
094: /**
095: * Gets the Model source for Transformmap URI
096: * @param locationURI URI location of the Transformmap File
097: * @return ModelSource return ModelSource
098: * @throws org.netbeans.modules.xml.xam.locator.CatalogModelException
099: */
100: public ModelSource getModelSource(URI locationURI)
101: throws CatalogModelException {
102: List<File> catalogFileList = new ArrayList<File>();
103: File file = null;
104: File projectCatalogXML = getProjectCatalogXML();
105: if (projectCatalogXML != null) {
106: catalogFileList.add(projectCatalogXML);
107: }
108: if (catalogFileList.size() > 0) {
109: URI uri = null;
110:
111: try {
112: uri = resolveUsingApacheCatalog(catalogFileList,
113: locationURI.toString());
114: } catch (IOException ioe) {
115:
116: }
117:
118: if (uri != null) {
119: file = new File(uri);
120: } else {
121: try {
122: file = new File(locationURI);
123: } catch (IllegalArgumentException ie) {
124: throw new CatalogModelException("Invalid URI"
125: + locationURI.toString());
126: }
127: }
128: } else {
129: try {
130: file = new File(locationURI);
131: } catch (IllegalArgumentException ie) {
132: throw new CatalogModelException("Invalid URI"
133: + locationURI.toString());
134: }
135: }
136: return createModelSource(file, true);
137: }
138:
139: /**
140: * Implementation of CatalogModel
141: * @param locationURI
142: * @param modelSourceOfSourceDocument
143: * @throws org.netbeans.modules.xml.xam.locator.CatalogModelException
144: * @return
145: */
146: public ModelSource getModelSource(URI locationURI,
147: ModelSource modelSourceOfSourceDocument)
148: throws CatalogModelException {
149: if (locationURI == null) {
150: return null;
151: }
152:
153: URI resolvedURI = locationURI;
154:
155: if (modelSourceOfSourceDocument != null) {
156: File sFile = (File) modelSourceOfSourceDocument.getLookup()
157: .lookup(File.class);
158:
159: if (sFile != null) {
160: URI sURI = sFile.toURI();
161: resolvedURI = sURI.resolve(locationURI);
162: }
163:
164: }
165:
166: if (resolvedURI != null) {
167: return getModelSource(resolvedURI);
168: }
169:
170: return null;
171: }
172:
173: /**
174: * Implementation of CatalogModel
175: * @param file
176: * @return
177: */
178: protected Document getDocument(File file)
179: throws CatalogModelException {
180: Document result = null;
181:
182: if (result != null)
183: return result;
184: try {
185:
186: FileInputStream fis = new FileInputStream(file);
187: byte buffer[] = new byte[fis.available()];
188: // result = new org.netbeans.editor.BaseDocument(
189: // org.netbeans.modules.xml.text.syntax.XMLKit.class, false);
190: result = new javax.swing.text.PlainDocument();
191: result.remove(0, result.getLength());
192: fis.read(buffer);
193: fis.close();
194: String str = new String(buffer);
195: result.insertString(0, str, null);
196:
197: } catch (Exception dObjEx) {
198: throw new CatalogModelException(file.getAbsolutePath()
199: + " File Not Found");
200: }
201:
202: return result;
203: }
204:
205: private TMapModelFactory getModelFactory() {
206: TMapModelFactory factory = null;
207: try {
208: factory = TMapModelFactory.TMapModelFactoryAccess
209: .getFactory();
210: } catch (Exception cnfe) {
211: throw new RuntimeException(cnfe);
212: }
213: return factory;
214: }
215:
216: /**
217: * Implementation of CatalogModel
218: * @param file
219: * @param readOnly
220: * @throws org.netbeans.modules.xml.xam.locator.CatalogModelException
221: * @return
222: */
223: public ModelSource createModelSource(File file, boolean readOnly)
224: throws CatalogModelException {
225:
226: Lookup lookup = Lookups.fixed(new Object[] { file,
227: getDocument(file), getDefault(),
228: // new StreamSource(file)
229: file });
230:
231: return new ModelSource(lookup, readOnly);
232: }
233:
234: /**
235: * Creates Transformmap Model from file URI
236: * @param locationURI
237: * @throws java.lang.Exception
238: * @return
239: */
240: public TMapModel getTMapModel(URI locationURI) throws Exception {
241: ModelSource source = getDefault().getModelSource(locationURI);
242: TMapModel model = getModelFactory().getModel(source);
243: model.sync();
244: return model;
245: }
246:
247: /**
248: * Creates WSDL Model from file URI
249: * @param locationURI
250: * @throws java.lang.Exception
251: * @return
252: */
253: public WSDLModel getWsdlModel(URI locationURI) throws Exception {
254: ModelSource source = getDefault().getModelSource(locationURI);
255: if (source == null) {
256: return null;
257: }
258: WSDLModel model = WSDLModelFactory.getDefault()
259: .getModel(source);
260: return model;
261: }
262:
263: protected URI resolveUsingApacheCatalog(List<File> catalogFileList,
264: String locationURI) throws CatalogModelException,
265: IOException {
266: CatalogResolver catalogResolver;
267: Catalog apacheCatalogResolverObj;
268:
269: CatalogManager manager = new CatalogManager(null);
270: manager.setUseStaticCatalog(false);
271: manager.setPreferPublic(false);
272: catalogResolver = new CatalogResolver(manager);
273: //catalogResolver = new CatalogResolver(true);
274: apacheCatalogResolverObj = catalogResolver.getCatalog();
275:
276: for (File catFile : catalogFileList) {
277: if (catFile.length() > 0) {
278: try {
279: apacheCatalogResolverObj.parseCatalog(catFile
280: .getAbsolutePath());
281: } catch (Throwable ex) {
282: throw new CatalogModelException(ex);
283: }
284:
285: String result = null;
286: try {
287: result = apacheCatalogResolverObj
288: .resolveSystem(locationURI);
289: } catch (MalformedURLException ex) {
290: result = "";
291: } catch (IOException ex) {
292: result = "";
293: }
294: if (result == null) {
295: result = "";
296: } else {
297: try {
298: //This is a workaround for a bug in resolver module on windows.
299: //the String returned by resolver is not an URI style
300: result = Utilities.normalizeURI(result);
301: URI uri = new URI(result);
302: if (uri.isOpaque()) {
303: if (uri.getScheme()
304: .equalsIgnoreCase("file")) {
305: StringBuffer resBuff = new StringBuffer(
306: result);
307: result = resBuff.insert(
308: "file:".length(), "/")
309: .toString();
310: }
311: }
312: } catch (URISyntaxException ex) {
313: return null;
314: }
315: }
316: if (result.length() > 0) {
317: try {
318: URI res = new URI(result);
319: return res;
320: } catch (URISyntaxException ex) {
321: }
322: }
323: }
324: }
325: return null;
326: }
327:
328: public InputSource resolveEntity(String publicId, String systemId)
329: throws SAXException, IOException {
330: //TODO implement this method.
331: return null;
332: }
333:
334: public LSInput resolveResource(String type, String namespaceURI,
335: String publicId, String systemId, String baseURI) {
336: //TODO implement this method.
337: return null;
338: }
339: }
|