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-2006 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: /*
043: * CatalogModelImpl.java
044: *
045: * Created on March 29, 2006, 6:03 PM
046: *
047: * To change this template, choose Tools | Template Manager
048: * and open the template in the editor.
049: */
050:
051: package org.netbeans.modules.xml.retriever.catalog.impl;
052:
053: import java.io.File;
054: import java.io.FileInputStream;
055: import java.io.FileNotFoundException;
056: import java.io.IOException;
057: import java.io.Reader;
058: import java.net.URI;
059: import java.net.URISyntaxException;
060: import java.util.ArrayList;
061: import java.util.Collection;
062: import java.util.List;
063: import java.util.logging.Level;
064: import java.util.logging.Logger;
065: import javax.swing.text.Document;
066: import javax.xml.parsers.DocumentBuilderFactory;
067: import javax.xml.parsers.ParserConfigurationException;
068: import org.apache.xml.resolver.Catalog;
069: import org.apache.xml.resolver.CatalogManager;
070: import org.apache.xml.resolver.helpers.Debug;
071: import org.apache.xml.resolver.tools.CatalogResolver;
072: import org.netbeans.api.project.FileOwnerQuery;
073: import org.netbeans.api.project.Project;
074: import org.netbeans.editor.BaseDocument;
075: import org.netbeans.modules.xml.retriever.XMLCatalogProvider;
076: import org.netbeans.modules.xml.retriever.catalog.ProjectCatalogSupport;
077: import org.netbeans.modules.xml.retriever.catalog.Utilities;
078: import org.netbeans.modules.xml.xam.locator.CatalogModel;
079: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
080: import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModel;
081: import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModelFactory;
082: import org.netbeans.modules.xml.retriever.catalog.ProjectCatalogSupport;
083: import org.netbeans.modules.xml.retriever.impl.Util;
084: import org.netbeans.modules.xml.xam.ModelSource;
085: import org.netbeans.spi.xml.cookies.DataObjectAdapters;
086: import org.openide.cookies.EditorCookie;
087: import org.openide.filesystems.FileObject;
088: import org.openide.filesystems.FileUtil;
089: import org.openide.loaders.DataObject;
090: import org.openide.loaders.DataObjectNotFoundException;
091: import org.openide.util.Lookup;
092: import org.w3c.dom.DOMImplementation;
093: import org.w3c.dom.ls.DOMImplementationLS;
094: import org.w3c.dom.ls.LSInput;
095: import org.xml.sax.InputSource;
096: import org.xml.sax.SAXException;
097:
098: /**
099: *
100: * @author girix
101: */
102: public class CatalogModelImpl implements CatalogModel {
103: protected FileObject catalogFileObject = null;
104: private static Logger logger = Logger
105: .getLogger(CatalogModelImpl.class.getName());
106:
107: /** Creates a new instance of CatalogModelImpl */
108: public CatalogModelImpl(Project myProject) throws IOException {
109: assert (myProject != null);
110: this .catalogFileObject = Util.getProjectCatalogFileObject(
111: myProject, false);
112: }
113:
114: /** Creates a new instance of CatalogModelImpl */
115: public CatalogModelImpl(FileObject catalogFileObject)
116: throws IOException {
117: assert (catalogFileObject != null);
118: this .catalogFileObject = catalogFileObject;
119: }
120:
121: public CatalogModelImpl() {
122: }
123:
124: /**
125: * This constructor is for unit testing purpose only
126: */
127: public CatalogModelImpl(File tempFolder) throws IOException {
128: tempFolder = FileUtil.normalizeFile(tempFolder);
129: FileObject fo = FileUtil.toFileObject(tempFolder);
130: String fileName = CatalogWriteModel.PUBLIC_CATALOG_FILE_NAME
131: + CatalogWriteModel.CATALOG_FILE_EXTENSION;
132: this .catalogFileObject = FileUtil.createData(fo, fileName);
133: }
134:
135: private boolean doFetch = true;
136: private boolean fetchSynchronous = false;
137:
138: public synchronized ModelSource getModelSourceSynchronous(
139: URI locationURI, ModelSource modelSourceOfSourceDocument,
140: boolean fetch) throws CatalogModelException {
141: ModelSource ms = null;
142: doFetch = fetch;
143: fetchSynchronous = true;
144: try {
145: ms = getModelSource(locationURI,
146: modelSourceOfSourceDocument);
147: } catch (CatalogModelException ex) {
148: throw ex;
149: } finally {
150: //reset flags
151: doFetch = true;
152: fetchSynchronous = false;
153: }
154: return ms;
155: }
156:
157: public synchronized ModelSource getModelSource(URI locationURI,
158: ModelSource modelSourceOfSourceDocument)
159: throws CatalogModelException {
160: logger.entering("CatalogModelImpl", "getModelSource",
161: locationURI);
162: Exception exn = null;
163: ModelSource result = null;
164: //selects the correct cataog for use.
165: useSuitableCatalogFile(modelSourceOfSourceDocument);
166: if (isOrphan() && isLocalFile(locationURI)) {
167: return tryOrphanResolution(locationURI,
168: modelSourceOfSourceDocument);
169: }
170: File absResourceFile = null;
171: FileObject fob = null;
172: if (modelSourceOfSourceDocument != null)
173: fob = (FileObject) modelSourceOfSourceDocument.getLookup()
174: .lookup(FileObject.class);
175: try {
176: //try to resolve using project wide catalog
177: absResourceFile = resolveUsingCatalog(locationURI, fob);
178: } catch (IOException ex) {
179: exn = ex;
180: } catch (CatalogModelException ex) {
181: exn = ex;
182: }
183: if ((absResourceFile == null) || (exn != null)) {
184: //means there was no entry found in catalog or relative path resolution
185: ModelSource rms = getModelSourceFromSystemWideCatalog(
186: locationURI, modelSourceOfSourceDocument);
187: if (rms != null) {
188: return rms;
189: }
190: try {
191: if (doFetch) {
192: //we did not get any matching entry by conventional way..So try retrieve and cache
193: absResourceFile = retrieveCacheAndLookup(
194: locationURI, fob);
195: }
196: } catch (IOException ex) {
197: throw new CatalogModelException(ex);
198: }
199: }
200: if (absResourceFile != null) {
201: logger.finer("Found abs file res:" + absResourceFile);
202: File normalizedFile = org.openide.filesystems.FileUtil
203: .normalizeFile(absResourceFile);
204: FileObject this FileObj = org.openide.filesystems.FileUtil
205: .toFileObject(normalizedFile);
206: boolean editable = isEditable(absResourceFile);
207: result = createModelSource(this FileObj, editable);
208: } else if (exn != null) {
209: throw new CatalogModelException(exn);
210: }
211: logger.exiting("CatalogModelImpl", "getModelSource", result);
212: return result;
213: }
214:
215: private void useSuitableCatalogFile(
216: ModelSource modelSourceOfSourceDocument) {
217: // if the modelSource's project has XMLCatalogProvider then use that to
218: // see which catalog file to use for this modelSource
219: if (modelSourceOfSourceDocument != null) {
220: FileObject msfo = (FileObject) modelSourceOfSourceDocument
221: .getLookup().lookup(FileObject.class);
222: if (msfo == null)
223: return;
224: Project prj = FileOwnerQuery.getOwner(msfo);
225: if (prj == null)
226: return;
227: XMLCatalogProvider catPovider = (XMLCatalogProvider) prj
228: .getLookup().lookup(XMLCatalogProvider.class);
229: if (catPovider == null)
230: return;
231: URI caturi = catPovider.getCatalog(msfo);
232: if (caturi == null)
233: return;
234: URI prjuri = FileUtil.toFile(prj.getProjectDirectory())
235: .toURI();
236: URI catFileURI = prjuri.resolve(caturi);
237: if (catFileURI == null)
238: return;
239: File catFile = new File(catFileURI);
240: if (!catFile.isFile()) {
241: try {
242: catFile.createNewFile();
243: } catch (IOException ex) {
244: return;
245: }
246: }
247: FileObject catFO = FileUtil.toFileObject(FileUtil
248: .normalizeFile(catFile));
249: if (catFO == null)
250: return;
251: //assign new catalog file that needs to be used for resolution
252: this .catalogFileObject = catFO;
253: }
254: }
255:
256: public ModelSource getModelSource(URI locationURI)
257: throws CatalogModelException {
258: if (isOrphan()) {
259: //the originating file does not belong to a project so dont use catalog lookup
260: //just use file resolution instead
261: return tryOrphanResolution(locationURI, null);
262: }
263: //just look in to the project catalog
264: return getModelSource(locationURI, null);
265: }
266:
267: /**
268: * This method must be overridden by the Unit testcase to return a special
269: * Document object for a FileObject.
270: */
271: private Document getDocument(FileObject modelSourceFileObject)
272: throws CatalogModelException {
273: Document result = null;
274: try {
275: DataObject dObject = DataObject.find(modelSourceFileObject);
276: EditorCookie ec = (EditorCookie) dObject
277: .getCookie(EditorCookie.class);
278: Document doc = ec.openDocument();
279: assert (doc instanceof BaseDocument);
280: result = doc;
281: } catch (Exception dObjEx) {
282: throw new CatalogModelException(dObjEx);
283: }
284: return result;
285: }
286:
287: /**
288: * This method could be overridden by the Unit testcase to return a special
289: * ModelSource object for a FileObject with custom impl of classes added to the lookup.
290: * This is optional if both getDocument(FO) and createCatalogModel(FO) are overridden.
291: */
292: protected ModelSource createModelSource(
293: final FileObject this FileObj, boolean editable)
294: throws CatalogModelException {
295: final ModelSource ms = Utilities.getModelSource(this FileObj,
296: editable);
297: return ms;
298: }
299:
300: protected CatalogModel createCatalogModel(FileObject fo)
301: throws CatalogModelException {
302: return new CatalogModelFactoryImpl().getCatalogModel(fo);
303: }
304:
305: private ModelSource tryOrphanResolution(URI locationURI,
306: ModelSource modelSource) {
307: logger.entering("CatalogModelImpl", "getModelSource",
308: locationURI);
309: if (catalogFileObject == null) {
310: try {
311: if (locationURI.isAbsolute()) {
312: //may be a local file URI so try creating a file
313: File file = new File(locationURI);
314: if (file.isFile()) {
315: file = FileUtil.normalizeFile(file);
316: FileObject fo = FileUtil.toFileObject(file);
317: return createModelSource(fo, isEditable(file));
318: }
319: } else {
320: //a relative URI, try resolving relative
321: if (modelSource != null) {
322: //source is needed for resolution
323: FileObject fo = (FileObject) modelSource
324: .getLookup().lookup(FileObject.class);
325: File file = resolveRelativeURI(locationURI, fo);
326: if (file != null) {
327: file = FileUtil.normalizeFile(file);
328: FileObject fobj = FileUtil
329: .toFileObject(file);
330: return createModelSource(fobj,
331: isEditable(file));
332: }
333: }
334: }
335: } catch (Exception e) {
336: return null;
337: }
338: }
339: return null;
340: }
341:
342: private boolean isOrphan() {
343: if (catalogFileObject == null)
344: return true;
345: return false;
346: }
347:
348: private boolean isLocalFile(URI locationURI) {
349: if (locationURI.isAbsolute() && locationURI.getScheme() != null
350: && "file".equals(locationURI.getScheme()))
351: return true;
352:
353: return false;
354: }
355:
356: protected File resolveUsingCatalog(URI locationURI,
357: FileObject sourceFileObject) throws CatalogModelException,
358: IOException {
359: logger.entering("CatalogModelImpl", "resolveUsingCatalog",
360: locationURI);
361: if (locationURI == null)
362: return null;
363: File result = null;
364: result = resolveUsingPublicCatalog(locationURI);
365: if (result != null)
366: return result;
367: if (sourceFileObject != null) {
368: result = resolveRelativeURI(locationURI, sourceFileObject);
369: }
370: if (result != null)
371: return result;
372: if ((locationURI.isAbsolute())
373: && locationURI.getScheme().equalsIgnoreCase("file")) {
374: //try to make a File
375: result = new File(locationURI);
376: if (result.isFile()) {
377: logger.exiting("CatalogModelImpl",
378: "resolveUsingCatalog", result);
379: return result;
380: } else
381: throw new FileNotFoundException(locationURI.toString()
382: + ": is absolute but "
383: + result.getAbsolutePath() + " Not Found.");
384: }
385: throw new CatalogModelException(
386: locationURI.toString()
387: + " : Entry is not a relative or absolute and catalog entry not found");
388: }
389:
390: private File retrieveCacheAndLookup(URI locationURI,
391: FileObject sourceFileObject) throws IOException,
392: CatalogModelException {
393: File result = null;
394: if ((locationURI.isAbsolute())
395: && locationURI.getScheme().toLowerCase().startsWith(
396: "http")
397: && !CatalogFileWrapperDOMImpl.TEST_ENVIRONMENT) {
398: // for all http and https absolute URI, just attempt downloading the
399: // file using the retriever API and store in the private cache.
400: //do not attempt this for a test environment.
401: boolean res = false;
402: try {
403: res = Util.retrieveAndCache(locationURI,
404: sourceFileObject, !fetchSynchronous);
405: } catch (Exception e) {//ignore all exceptions
406: }
407: if (res) {
408: //now attempt onec more
409: result = resolveUsingPublicCatalog(locationURI);
410: if (result != null)
411: return result;
412: }
413: }
414: return result;
415: }
416:
417: protected File resolveUsingPublicCatalog(URI locationURI)
418: throws IOException, CatalogModelException {
419: File result = null;
420: if (catalogFileObject != null) {
421: //look up in the catalog
422: File publicCatalogFile = FileUtil.toFile(catalogFileObject);
423: if (publicCatalogFile.isFile()) {
424: //return if the file content is empty or just start and end tags
425: if (publicCatalogFile.length() < 20)
426: return null;
427: URI strRes = resolveUsingApacheCatalog(
428: publicCatalogFile, locationURI.toString());
429: if (strRes != null) {
430: if (strRes.isAbsolute()) {
431: if (strRes.getScheme().equalsIgnoreCase("file")) {
432: result = new File(strRes);
433: if (result.isFile()) {
434: logger.exiting("CatalogModelImpl",
435: "resolveUsingCatalog", result);
436: return result;
437: } else
438: throw new FileNotFoundException(result
439: .getAbsolutePath()
440: + " Not Found.");
441: } else {
442: File res = resolveProjectProtocol(strRes);
443: if (res != null)
444: return res;
445: throw new CatalogModelException(
446: "Catalog contains non-file URI. Catalog Maps URI to a local file only.");
447: }
448: }
449: }
450: }
451: }
452: return null;
453: }
454:
455: protected File resolveRelativeURI(URI locationURI,
456: FileObject sourceFileObject) throws CatalogModelException,
457: FileNotFoundException {
458: File result = null;
459: if (!locationURI.isAbsolute()) {
460: //this might be a relative file location
461: if (sourceFileObject == null)
462: throw new CatalogModelException(
463: locationURI.toString()
464: + " : Entry is relative but base file now known. Pass base file to the factory");
465: File sourceFile = FileUtil.toFile(sourceFileObject);
466: //IZ 104753
467: //In case of layer.xml defined sourceFileObject, FileUtil.toFile returns null.
468:
469: if (sourceFile != null) {
470: URI sourceFileObjectURI = sourceFile.toURI();
471: URI resultURI = sourceFileObjectURI
472: .resolve(locationURI);
473: try {
474: result = new File(resultURI);
475: } catch (Exception e) {
476: throw new CatalogModelException(
477: locationURI.toString()
478: + " : Entry is relative but resolved entry is not absolute");
479: }
480: if (result.isFile()) {
481: logger.exiting("CatalogModelImpl",
482: "resolveUsingCatalog", result);
483: return result;
484: } else
485: throw new FileNotFoundException(result
486: .getAbsolutePath()
487: + " Not Found.");
488: }
489: }
490: return null;
491: }
492:
493: protected URI resolveUsingApacheCatalog(File catalogFile,
494: String locationURI) throws IOException,
495: CatalogModelException {
496: List<File> catalogFileList = new ArrayList<File>();
497: catalogFileList.add(catalogFile);
498: return resolveUsingApacheCatalog(catalogFileList, locationURI);
499: }
500:
501: CatalogResolver catalogResolver;
502: Catalog apacheCatalogResolverObj;
503:
504: protected URI resolveUsingApacheCatalog(List<File> catalogFileList,
505: String locationURI) throws CatalogModelException,
506: IOException {
507: if ((logger.getLevel() != null)
508: && (logger.getLevel().intValue() <= Level.FINER
509: .intValue())) {
510: Debug debug = CatalogManager.getStaticManager().debug;
511: debug.setDebug(logger.getLevel().intValue());
512: }
513:
514: CatalogManager manager = new CatalogManager(null);
515: manager.setUseStaticCatalog(false);
516: manager.setPreferPublic(false);
517: catalogResolver = new CatalogResolver(manager);
518: apacheCatalogResolverObj = catalogResolver.getCatalog();
519: for (File catFile : catalogFileList) {
520: apacheCatalogResolverObj.parseCatalog(catFile.toURL());
521: }
522:
523: String result = null;
524: try {
525: result = apacheCatalogResolverObj
526: .resolveSystem(locationURI);
527: } catch (Exception ex) {
528: result = "";
529: }
530: if (result == null) {
531: result = "";
532: } else {
533: try {
534: //This is a workaround for a bug in resolver module on windows.
535: //the String returned by resolver is not an URI style
536: result = Utilities.normalizeURI(result);
537: URI uri = new URI(result);
538: if (uri.isOpaque()) {
539: if (uri.getScheme().equalsIgnoreCase("file")) {
540: StringBuffer resBuff = new StringBuffer(result);
541: result = resBuff.insert("file:".length(), "/")
542: .toString();
543: }
544: }
545: } catch (URISyntaxException ex) {
546: return null;
547: }
548: }
549: if (result.length() > 0) {
550: try {
551: URI res = new URI(result);
552: return res;
553: } catch (URISyntaxException ex) {
554: }
555: }
556: return null;
557: }
558:
559: // long lastModTime = 0;
560: // protected boolean reparseRequired(List<File> catalogFileList){
561: // /* if((apacheCatalogResolverObj == null) || (lastModTime == 0)){
562: // //then parse always
563: // lastModTime = catalogFileList.get(0).lastModified(); //bother only public catalog for now
564: // //System.out.println("Parsing First time: "+lastModTime);
565: // return true;
566: // }
567: // if((apacheCatalogResolverObj != null) && (lastModTime != 0)){
568: // if(lastModTime < catalogFileList.get(0).lastModified()){
569: // //System.out.println("Parsing time diff Old: "+lastModTime+" New:"+catalogFileList.get(0).lastModified());
570: // lastModTime = catalogFileList.get(0).lastModified();
571: // return true;
572: // } else{
573: // //System.out.println("NOT Parsing time diff Old: "+lastModTime);
574: // return false;
575: // }
576: // }
577: // //System.out.println("Parsing Otherwise: "+lastModTime);*/
578: // return true;
579: // }
580: //
581: //
582: boolean isEditable(File absResourceFile) {
583: return true;
584: }
585:
586: public InputSource resolveEntity(String publicId, String systemId)
587: throws SAXException, IOException {
588: try {
589: return getInputSource(new URI(systemId));
590: } catch (CatalogModelException ex) {
591: throw new IOException(ex.getMessage());
592: } catch (URISyntaxException e) {
593: throw new IOException("SystemID not a URL");
594: }
595: }
596:
597: private InputSource getInputSource(URI locationURI)
598: throws CatalogModelException, IOException {
599: logger.entering("CatalogModelImpl", "getInputSource",
600: locationURI);
601: File absResourceFile = resolveUsingCatalog(locationURI, null);
602: logger.finer("Found abs file res:" + absResourceFile);
603: InputSource result = new InputSource(new FileInputStream(
604: absResourceFile));
605: result.setSystemId(locationURI.toString());
606: logger.exiting("CatalogModelImpl", "getInputSource", result);
607: return result;
608: }
609:
610: public LSInput resolveResource(String type, String namespaceURI,
611: String publicId, String systemId, String baseURIStr) {
612: //check for sanity of the systemID
613: if ((systemId == null) || (systemId.trim().length() <= 0))
614: return null;
615: URI systemIdURI = null;
616: try {
617: systemIdURI = new URI(systemId);
618: } catch (URISyntaxException ex) {
619: return null;
620: }
621: FileObject baseFO = null;
622: //get the resolver object
623: CatalogModel depRez = null;
624: try {
625: baseFO = getFileObject(baseURIStr);
626: depRez = getResolver(baseFO);
627: } catch (CatalogModelException ex) {
628: return null;
629: } catch (IOException ex) {
630: return null;
631: }
632: if (depRez == null)
633: return null;
634: ModelSource baseMS = null;
635: try {
636: baseMS = createModelSource(baseFO, false);
637: } catch (CatalogModelException ex) {
638: }
639: //get the model source from it
640: ModelSource resultMS = null;
641: try {
642: resultMS = depRez.getModelSource(systemIdURI, baseMS);
643: } catch (CatalogModelException ex) {
644: return null;
645: }
646: if (resultMS == null)
647: return null;
648: //get file object
649: FileObject resultFob = (FileObject) resultMS.getLookup()
650: .lookup(FileObject.class);
651: if (resultFob == null)
652: return null;
653: //get file
654: File resultFile = FileUtil.toFile(resultFob);
655: if (resultFile == null)
656: return null;
657: //get URI out of file
658: URI resultURI = resultFile.toURI();
659: //create LSInput object
660: DOMImplementation domImpl = null;
661: try {
662: domImpl = DocumentBuilderFactory.newInstance()
663: .newDocumentBuilder().getDOMImplementation();
664: } catch (ParserConfigurationException ex) {
665: return null;
666: }
667: DOMImplementationLS dols = (DOMImplementationLS) domImpl
668: .getFeature("LS", "3.0");
669: LSInput lsi = dols.createLSInput();
670: Reader is = getFileStreamFromDocument(resultFile);
671: if (is != null)
672: lsi.setCharacterStream(is);
673: lsi.setSystemId(resultURI.toString());
674: return lsi;
675: }
676:
677: private FileObject getFileObject(String baseURIStr)
678: throws IOException {
679: if (baseURIStr == null)
680: return null;
681: URI baseURI = null;
682: try {
683: baseURI = new URI(baseURIStr);
684: } catch (URISyntaxException ex) {
685: IOException ioe = new IOException();
686: ioe.initCause(ex);
687: throw ioe;
688: }
689: if (baseURI.isAbsolute()) {
690: if (baseURI.getScheme().equalsIgnoreCase("file")) { //NOI18N
691: File baseFile = null;
692: try {
693: baseFile = new File(baseURI);
694: } catch (Exception e) {
695: IOException ioe = new IOException();
696: ioe.initCause(e);
697: throw ioe;
698: }
699: baseFile = FileUtil.normalizeFile(baseFile);
700: FileObject baseFileObject = null;
701: try {
702: baseFileObject = FileUtil.toFileObject(baseFile);
703: } catch (Exception e) {
704: IOException ioe = new IOException();
705: ioe.initCause(e);
706: throw ioe;
707: }
708: return baseFileObject;
709: }
710: }
711: return null;
712: }
713:
714: private CatalogModel getResolver(FileObject baseFileObject)
715: throws CatalogModelException {
716: if (baseFileObject != null
717: && FileOwnerQuery.getOwner(baseFileObject) != null) {
718: return CatalogWriteModelFactory.getInstance()
719: .getCatalogWriteModelForProject(baseFileObject);
720: }
721: return this ;
722: }
723:
724: private Reader getFileStreamFromDocument(File resultFile) {
725: FileObject fo = FileUtil.toFileObject(FileUtil
726: .normalizeFile(resultFile));
727: if (fo != null) {
728: DataObject dobj = null;
729: try {
730: dobj = DataObject.find(fo);
731: } catch (DataObjectNotFoundException ex) {
732: return null;
733: }
734: if (dobj.isValid() && dobj.isModified()) {
735: // DataObjectAdapters does not implement getByteStream
736: // so calling this here will effectively return null
737: return DataObjectAdapters.inputSource(dobj)
738: .getCharacterStream();
739: } else {
740: //return null so that the validator will use normal file path to access doc
741: return null;
742: }
743: }
744: return null;
745: }
746:
747: protected File resolveProjectProtocol(URI strRes) {
748: File result = null;
749: Project prj = FileOwnerQuery.getOwner(this .catalogFileObject);
750: if (prj != null) {
751: ProjectCatalogSupport pcs = (ProjectCatalogSupport) prj
752: .getLookup().lookup(ProjectCatalogSupport.class);
753: if (pcs.isProjectProtocol(strRes)) {
754: FileObject resFO = pcs.resolveProjectProtocol(strRes);
755: if (resFO != null) {
756: return FileUtil.toFile(resFO);
757: }
758: }
759: }
760: return result;
761: }
762:
763: private ModelSource getModelSourceFromSystemWideCatalog(
764: URI locationURI, ModelSource modelSourceOfSourceDocument) {
765: if (locationURI == null)
766: return null;
767:
768: try {
769: Lookup.Template templ = new Lookup.Template(
770: CatalogModel.class);
771: Lookup.Result res = Lookup.getDefault().lookup(templ);
772: Collection impls = res.allInstances();
773: for (Object obj : impls) {
774: CatalogModel cm = (CatalogModel) obj;
775: return cm.getModelSource(locationURI,
776: modelSourceOfSourceDocument);
777: }
778: } catch (CatalogModelException ex) {
779: //return null
780: }
781:
782: return null;
783: }
784: }
|