KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > retriever > catalog > impl > CatalogModelImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * CatalogModelImpl.java
22  *
23  * Created on March 29, 2006, 6:03 PM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.xml.retriever.catalog.impl;
30
31 import java.io.File JavaDoc;
32 import java.io.FileInputStream JavaDoc;
33 import java.io.FileNotFoundException JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.Reader JavaDoc;
36 import java.net.MalformedURLException JavaDoc;
37 import java.net.URI JavaDoc;
38 import java.net.URISyntaxException JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.Collection JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.logging.Level JavaDoc;
43 import java.util.logging.Logger JavaDoc;
44 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
45 import javax.xml.parsers.ParserConfigurationException JavaDoc;
46 import org.apache.xml.resolver.Catalog;
47 import org.apache.xml.resolver.CatalogManager;
48 import org.apache.xml.resolver.helpers.Debug;
49 import org.apache.xml.resolver.tools.CatalogResolver;
50 import org.netbeans.api.project.FileOwnerQuery;
51 import org.netbeans.api.project.Project;
52 import org.netbeans.modules.xml.retriever.XMLCatalogProvider;
53 import org.netbeans.modules.xml.retriever.catalog.Utilities;
54 import org.netbeans.modules.xml.xam.locator.CatalogModel;
55 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
56 import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModel;
57 import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModelFactory;
58 import org.netbeans.modules.xml.retriever.catalog.ProjectCatalogSupport;
59 import org.netbeans.modules.xml.xam.ModelSource;
60 import org.netbeans.spi.xml.cookies.DataObjectAdapters;
61 import org.openide.filesystems.FileObject;
62 import org.openide.filesystems.FileUtil;
63 import org.openide.loaders.DataObject;
64 import org.openide.loaders.DataObjectNotFoundException;
65 import org.openide.util.Lookup;
66 import org.w3c.dom.DOMImplementation JavaDoc;
67 import org.w3c.dom.ls.DOMImplementationLS JavaDoc;
68 import org.w3c.dom.ls.LSInput JavaDoc;
69 import org.xml.sax.InputSource JavaDoc;
70 import org.xml.sax.SAXException JavaDoc;
71
72 /**
73  *
74  * @author girix
75  */

76 public class CatalogModelImpl implements CatalogModel {
77     protected FileObject catalogFileObject = null;
78     private static final Logger JavaDoc logger = Utilities.getLogger();
79     /** Creates a new instance of CatalogModelImpl */
80     public CatalogModelImpl(Project myProject) throws IOException JavaDoc{
81         assert(myProject != null);
82         this.catalogFileObject = Utilities.getProjectCatalogFileObject(myProject);
83     }
84     
85     
86     /** Creates a new instance of CatalogModelImpl */
87     public CatalogModelImpl(FileObject catalogFileObject) throws IOException JavaDoc{
88         assert(catalogFileObject != null);
89         this.catalogFileObject = catalogFileObject;
90     }
91     
92     
93     public CatalogModelImpl(){
94     }
95     
96     
97     /**
98      * This constructor is for unit testing purpose only
99      */

100     public CatalogModelImpl(File JavaDoc tempFolder) throws IOException JavaDoc{
101         tempFolder = FileUtil.normalizeFile(tempFolder);
102         FileObject fo = FileUtil.toFileObject(tempFolder);
103         String JavaDoc fileName = CatalogWriteModel.PUBLIC_CATALOG_FILE_NAME+
104                 CatalogWriteModel.CATALOG_FILE_EXTENSION;
105         this.catalogFileObject = FileUtil.createData(fo, fileName);
106     }
107     
108     public synchronized ModelSource getModelSource(URI JavaDoc locationURI,
109             ModelSource modelSourceOfSourceDocument) throws CatalogModelException {
110         logger.entering("CatalogModelImpl", "getModelSource", locationURI);
111         Exception JavaDoc exn = null;
112         ModelSource result = null;
113         //selects the correct cataog for use.
114
useSuitableCatalogFile(modelSourceOfSourceDocument);
115         if(isOrphan())
116             return tryOrphanResolution(locationURI, modelSourceOfSourceDocument);
117         File JavaDoc absResourceFile = null;
118         FileObject fob = null;
119         if(modelSourceOfSourceDocument != null)
120             fob = (FileObject) modelSourceOfSourceDocument.getLookup().lookup(FileObject.class);
121         try {
122             //try to resolve using project wide catalog
123
absResourceFile = resolveUsingCatalog(locationURI, fob);
124         } catch (IOException JavaDoc ex) {
125             exn = ex;
126         } catch(CatalogModelException ex){
127             exn = ex;
128         }
129         if( (absResourceFile == null) || (exn != null) ){
130             //means there was no entry found in catalog or relative path resolution
131

132             //check in the system wide catalog (Runtime tab), if entry found, return that
133
ModelSource rms = getModelSourceFromSystemWideCatalog(locationURI, modelSourceOfSourceDocument);
134             if(rms != null)
135                 return rms;
136             try {
137                 //we did not get any matching entry by conventional way..So try retrieve and cache
138
absResourceFile = retrieveCacheAndLookup(locationURI, fob);
139             } catch (IOException JavaDoc ex) {
140                 throw new CatalogModelException(ex);
141             }
142         }
143         if(absResourceFile != null){
144             logger.finer("Found abs file res:"+absResourceFile);
145             File JavaDoc normalizedFile = org.openide.filesystems.FileUtil.normalizeFile(absResourceFile);
146             FileObject thisFileObj = org.openide.filesystems.FileUtil.toFileObject(normalizedFile);
147             boolean editable = isEditable(absResourceFile);
148             result = createModelSource(thisFileObj, editable);
149         }else if(exn!= null) {
150             throw new CatalogModelException(exn);
151         }
152         logger.exiting("CatalogModelImpl", "getModelSource", result);
153         return result;
154     }
155     
156     private void useSuitableCatalogFile(ModelSource modelSourceOfSourceDocument) {
157         // if the modelSource's project has XMLCatalogProvider then use that to
158
// see which catalog file to use for this modelSource
159
if(modelSourceOfSourceDocument != null){
160             FileObject msfo = (FileObject) modelSourceOfSourceDocument.getLookup().
161                     lookup(FileObject.class);
162             if(msfo == null)
163                 return;
164             Project prj = FileOwnerQuery.getOwner(msfo);
165             if(prj == null)
166                 return;
167             XMLCatalogProvider catPovider = (XMLCatalogProvider) prj.getLookup().
168                     lookup(XMLCatalogProvider.class);
169             if(catPovider == null)
170                 return;
171             URI JavaDoc caturi = catPovider.getCatalog(msfo);
172             if(caturi == null)
173                 return;
174             URI JavaDoc prjuri = FileUtil.toFile(prj.getProjectDirectory()).toURI();
175             URI JavaDoc catFileURI = prjuri.resolve(caturi);
176             if(catFileURI == null)
177                 return;
178             File JavaDoc catFile = new File JavaDoc(catFileURI);
179             if(!catFile.isFile()){
180                 try {
181                     catFile.createNewFile();
182                 } catch (IOException JavaDoc ex) {
183                     return;
184                 }
185             }
186             FileObject catFO = FileUtil.toFileObject(FileUtil.normalizeFile(catFile));
187             if(catFO == null)
188                 return;
189             //assign new catalog file that needs to be used for resolution
190
this.catalogFileObject = catFO;
191         }
192     }
193     
194     
195     public ModelSource getModelSource(URI JavaDoc locationURI) throws CatalogModelException{
196         if(isOrphan()){
197             //the originating file does not belong to a project so dont use catalog lookup
198
//just use file resolution instead
199
return tryOrphanResolution(locationURI, null);
200         }
201         //just look in to the project catalog
202
return getModelSource(locationURI, null);
203     }
204     
205     
206     /**
207      * This method could be overridden by the Unit testcase to return a special
208      * ModelSource object for a FileObject with custom impl of classes added to the lookup.
209      * This is optional if both getDocument(FO) and createCatalogModel(FO) are overridden.
210      */

211     protected ModelSource createModelSource(final FileObject thisFileObj, boolean editable) throws CatalogModelException{
212         final ModelSource ms = Utilities.getModelSource(thisFileObj,editable);
213         return ms;
214     }
215     
216     
217     protected CatalogModel createCatalogModel(FileObject fo) throws CatalogModelException{
218         return new CatalogModelFactoryImpl().getCatalogModel(fo);
219     }
220     
221     
222     private ModelSource tryOrphanResolution(URI JavaDoc locationURI, ModelSource modelSource){
223         logger.entering("CatalogModelImpl", "getModelSource", locationURI);
224         if(catalogFileObject == null){
225             try{
226                 if(locationURI.isAbsolute()){
227                     //may be a local file URI so try creating a file
228
File JavaDoc file = new File JavaDoc(locationURI);
229                     if(file.isFile()){
230                         file = FileUtil.normalizeFile(file);
231                         FileObject fo = FileUtil.toFileObject(file);
232                         return createModelSource(fo, isEditable(file));
233                     }
234                 } else {
235                     //a relative URI, try resolving relative
236
if(modelSource != null){
237                         //source is needed for resolution
238
FileObject fo = (FileObject) modelSource.getLookup().lookup(FileObject.class);
239                         File JavaDoc file = resolveRelativeURI(locationURI, fo);
240                         if(file != null){
241                             file = FileUtil.normalizeFile(file);
242                             FileObject fobj = FileUtil.toFileObject(file);
243                             return createModelSource(fobj, isEditable(file));
244                         }
245                     }
246                 }
247             }catch (Exception JavaDoc e){
248                 return null;
249             }
250         }
251         return null;
252     }
253     
254     
255     private boolean isOrphan(){
256         if(catalogFileObject == null)
257             return true;
258         return false;
259     }
260     
261     
262     protected File JavaDoc resolveUsingCatalog(URI JavaDoc locationURI, FileObject sourceFileObject
263             ) throws CatalogModelException, IOException JavaDoc {
264         logger.entering("CatalogModelImpl", "resolveUsingCatalog", locationURI);
265         if(locationURI == null)
266             return null;
267         File JavaDoc result = null;
268         result = resolveUsingPublicCatalog(locationURI);
269         if(result != null)
270             return result;
271         if(sourceFileObject != null){
272             result = resolveRelativeURI(locationURI, sourceFileObject);
273         }
274         if(result != null)
275             return result;
276         if( (locationURI.isAbsolute()) && locationURI.getScheme().equalsIgnoreCase("file")){
277             //try to make a File
278
result = new File JavaDoc(locationURI);
279             if(result.isFile()){
280                 logger.exiting("CatalogModelImpl", "resolveUsingCatalog",result);
281                 return result;
282             } else
283                 throw new FileNotFoundException JavaDoc(locationURI.toString()+": is absolute but "+result.getAbsolutePath()+" Not Found.");
284         }
285         throw new CatalogModelException(locationURI.toString()+" : Entry is not a relative or absolute and catalog entry not found");
286     }
287     
288     
289     private File JavaDoc retrieveCacheAndLookup(URI JavaDoc locationURI, FileObject sourceFileObject) throws IOException JavaDoc, CatalogModelException{
290         File JavaDoc result = null;
291         if((locationURI.isAbsolute()) && locationURI.getScheme().toLowerCase().
292                 startsWith("http") && !CatalogFileWrapperDOMImpl.TEST_ENVIRONMENT){
293             // for all http and https absolute URI, just attempt downloading the
294
// file using the retriever API and store in the private cache.
295
//do not attempt this for a test environment.
296
boolean res = false;
297             try{
298                 res = Utilities.retrieveAndCache(locationURI, sourceFileObject);
299             }catch (Exception JavaDoc e){//ignore all exceptions
300
}
301             if(res){
302                 //now attempt onec more
303
result = resolveUsingPublicCatalog(locationURI);
304                 if(result != null)
305                     return result;
306             }
307         }
308         return result;
309     }
310     
311     protected File JavaDoc resolveUsingPublicCatalog(URI JavaDoc locationURI) throws IOException JavaDoc, CatalogModelException{
312         File JavaDoc result = null;
313         if(catalogFileObject != null){
314             //look up in the catalog
315
File JavaDoc publicCatalogFile = FileUtil.toFile(catalogFileObject);
316             if(publicCatalogFile.isFile()){
317                 //return if the file content is empty or just start and end tags
318
if(publicCatalogFile.length() < 20)
319                     return null;
320                 URI JavaDoc strRes = resolveUsingApacheCatalog(publicCatalogFile, locationURI.toString());
321                 if(strRes != null){
322                     if(strRes.isAbsolute()){
323                         if(strRes.getScheme().equalsIgnoreCase("file")){
324                             result = new File JavaDoc(strRes);
325                             if(result.isFile()){
326                                 logger.exiting("CatalogModelImpl", "resolveUsingCatalog",result);
327                                 return result;
328                             } else
329                                 throw new FileNotFoundException JavaDoc(result.getAbsolutePath()+" Not Found.");
330                         }else{
331                             throw new CatalogModelException("Catalog contains non-file URI. Catalog Maps URI to a local file only.");
332                         }
333                     }
334                 }
335             }
336         }
337         return null;
338     }
339     
340     
341     protected File JavaDoc resolveRelativeURI(URI JavaDoc locationURI, FileObject sourceFileObject) throws CatalogModelException, FileNotFoundException JavaDoc{
342         File JavaDoc result = null;
343         if(!locationURI.isAbsolute()){
344             //this might be a relative file location
345
if(sourceFileObject == null)
346                 throw new CatalogModelException(locationURI.toString()+" : Entry is relative but base file now known. Pass base file to the factory");
347             URI JavaDoc sourceFileObjectURI = FileUtil.toFile(sourceFileObject).toURI();
348             URI JavaDoc resultURI = sourceFileObjectURI.resolve(locationURI);
349             try{
350                 result = new File JavaDoc(resultURI);
351             } catch(Exception JavaDoc e){
352                 throw new CatalogModelException(locationURI.toString()+" : Entry is relative but resolved entry is not absolute");
353             }
354             if(result.isFile()){
355                 logger.exiting("CatalogModelImpl", "resolveUsingCatalog",result);
356                 return result;
357             } else
358                 throw new FileNotFoundException JavaDoc(result.getAbsolutePath()+" Not Found.");
359         }
360         return null;
361     }
362     
363     
364     protected URI JavaDoc resolveUsingApacheCatalog(File JavaDoc catalogFile, String JavaDoc locationURI) throws IOException JavaDoc, CatalogModelException{
365         List JavaDoc<File JavaDoc> catalogFileList = new ArrayList JavaDoc<File JavaDoc>();
366         catalogFileList.add(catalogFile);
367         return resolveUsingApacheCatalog(catalogFileList, locationURI);
368     }
369     
370     
371     CatalogResolver catalogResolver;
372     Catalog apacheCatalogResolverObj;
373     protected URI JavaDoc resolveUsingApacheCatalog(List JavaDoc<File JavaDoc> catalogFileList, String JavaDoc locationURI) throws CatalogModelException, IOException JavaDoc {
374         if((logger.getLevel() != null) && (logger.getLevel().intValue() <= Level.FINER.intValue())){
375             Debug debug = CatalogManager.getStaticManager().debug;
376             debug.setDebug(logger.getLevel().intValue());
377         }
378         //Debug debug = CatalogManager.getStaticManager().debug;
379
//debug.setDebug(4);
380
/*CatalogManager cm = CatalogManager.getStaticManager();
381        cm.setUseStaticCatalog(false);*/

382         //parse catalog file if its required
383
if(reparseRequired(catalogFileList)){
384             CatalogManager manager = new CatalogManager(null);
385             manager.setUseStaticCatalog(false);
386             manager.setPreferPublic(false);
387             catalogResolver = new CatalogResolver(manager);
388             //catalogResolver = new CatalogResolver(true);
389
apacheCatalogResolverObj = catalogResolver.getCatalog();
390             for(File JavaDoc catFile : catalogFileList){
391                 try {
392                     apacheCatalogResolverObj.parseCatalog(catFile.getAbsolutePath());
393                 } catch (MalformedURLException JavaDoc ex) {
394                     throw new CatalogModelException(ex);
395                 }
396             }
397         }
398         String JavaDoc result = null;
399         try {
400             result = apacheCatalogResolverObj.resolveSystem(locationURI);
401         } catch (MalformedURLException JavaDoc ex) {
402             result = "";
403         } catch (IOException JavaDoc ex) {
404             result = "";
405         }
406         if(result == null){
407             result = "";
408         }else{
409             try {
410                 //This is a workaround for a bug in resolver module on windows.
411
//the String returned by resolver is not an URI style
412
result = Utilities.normalizeURI(result);
413                 URI JavaDoc uri = new URI JavaDoc(result);
414                 if(uri.isOpaque()){
415                     if(uri.getScheme().equalsIgnoreCase("file")){
416                         StringBuffer JavaDoc resBuff = new StringBuffer JavaDoc(result);
417                         result = resBuff.insert("file:".length(), "/").toString();
418                     }
419                 }
420             } catch (URISyntaxException JavaDoc ex) {
421                 return null;
422             }
423         }
424         if(result.length() > 0 ){
425             try {
426                 URI JavaDoc res = new URI JavaDoc(result);
427                 return res;
428             } catch (URISyntaxException JavaDoc ex) {
429             }
430         }
431         return null;
432     }
433     
434     
435     long lastModTime = 0;
436     protected boolean reparseRequired(List JavaDoc<File JavaDoc> catalogFileList){
437       /* if((apacheCatalogResolverObj == null) || (lastModTime == 0)){
438            //then parse always
439            lastModTime = catalogFileList.get(0).lastModified(); //bother only public catalog for now
440            //System.out.println("Parsing First time: "+lastModTime);
441            return true;
442        }
443        if((apacheCatalogResolverObj != null) && (lastModTime != 0)){
444            if(lastModTime < catalogFileList.get(0).lastModified()){
445                //System.out.println("Parsing time diff Old: "+lastModTime+" New:"+catalogFileList.get(0).lastModified());
446                lastModTime = catalogFileList.get(0).lastModified();
447                return true;
448            } else{
449                //System.out.println("NOT Parsing time diff Old: "+lastModTime);
450                return false;
451            }
452        }
453        //System.out.println("Parsing Otherwise: "+lastModTime);*/

454         return true;
455     }
456     
457     
458     boolean isEditable(File JavaDoc absResourceFile) {
459         return true;
460     }
461     
462     
463     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc, IOException JavaDoc {
464         try {
465             return getInputSource(new URI JavaDoc(systemId));
466         } catch (CatalogModelException ex) {
467             throw new IOException JavaDoc(ex.getMessage());
468         } catch (URISyntaxException JavaDoc e){
469             throw new IOException JavaDoc("SystemID not a URL");
470         }
471     }
472     
473     
474     private InputSource JavaDoc getInputSource(URI JavaDoc locationURI) throws CatalogModelException, IOException JavaDoc {
475         logger.entering("CatalogModelImpl", "getInputSource", locationURI);
476         File JavaDoc absResourceFile = resolveUsingCatalog(locationURI, null);
477         logger.finer("Found abs file res:"+absResourceFile);
478         InputSource JavaDoc result = new InputSource JavaDoc(new FileInputStream JavaDoc(absResourceFile));
479         result.setSystemId(locationURI.toString());
480         logger.exiting("CatalogModelImpl", "getInputSource", result);
481         return result;
482     }
483     
484     
485     public LSInput JavaDoc resolveResource(String JavaDoc type, String JavaDoc namespaceURI, String JavaDoc publicId, String JavaDoc systemId, String JavaDoc baseURIStr) {
486         //check for sanity of the systemID
487
if((systemId == null) || (systemId.trim().length() <=0 ))
488             return null;
489         URI JavaDoc systemIdURI = null;
490         try {
491             systemIdURI = new URI JavaDoc(systemId);
492         } catch (URISyntaxException JavaDoc ex) {
493             return null;
494         }
495         FileObject baseFO = null;
496         //get the resolver object
497
CatalogModel depRez = null;
498         try {
499             baseFO = getFileObject(baseURIStr);
500             depRez = getResolver(baseFO);
501         } catch (CatalogModelException ex) {
502             return null;
503         } catch (IOException JavaDoc ex) {
504             return null;
505         }
506         if(depRez == null)
507             return null;
508         ModelSource baseMS = null;
509         try {
510             baseMS = createModelSource(baseFO, false);
511         } catch (CatalogModelException ex) {
512         }
513         //get the model source from it
514
ModelSource resultMS = null;
515         try {
516             resultMS = depRez.getModelSource(systemIdURI, baseMS);
517         } catch (CatalogModelException ex) {
518             return null;
519         }
520         if(resultMS == null)
521             return null;
522         //get file object
523
FileObject resultFob = (FileObject) resultMS.getLookup().lookup(FileObject.class);
524         if(resultFob == null)
525             return null;
526         //get file
527
File JavaDoc resultFile = FileUtil.toFile(resultFob);
528         if(resultFile == null)
529             return null;
530         //get URI out of file
531
URI JavaDoc resultURI = resultFile.toURI();
532         //create LSInput object
533
DOMImplementation JavaDoc domImpl = null;
534         try {
535             domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
536         } catch (ParserConfigurationException JavaDoc ex) {
537             return null;
538         }
539         DOMImplementationLS JavaDoc dols = (DOMImplementationLS JavaDoc) domImpl.getFeature("LS","3.0");
540         LSInput JavaDoc lsi = dols.createLSInput();
541         Reader JavaDoc is = getFileStreamFromDocument(resultFile);
542         if(is != null)
543             lsi.setCharacterStream(is);
544         lsi.setSystemId(resultURI.toString());
545         return lsi;
546     }
547     
548     
549     private FileObject getFileObject(String JavaDoc baseURIStr) throws IOException JavaDoc{
550         if(baseURIStr == null)
551             return null;
552         URI JavaDoc baseURI = null;
553         try {
554             baseURI = new URI JavaDoc(baseURIStr);
555         } catch (URISyntaxException JavaDoc ex) {
556             IOException JavaDoc ioe = new IOException JavaDoc();
557             ioe.initCause(ex);
558             throw ioe;
559         }
560         if(baseURI.isAbsolute()){
561             if(baseURI.getScheme().equalsIgnoreCase("file")){ //NOI18N
562
File JavaDoc baseFile = null;
563                 try{
564                     baseFile = new File JavaDoc(baseURI);
565                 } catch(Exception JavaDoc e){
566                     IOException JavaDoc ioe = new IOException JavaDoc();
567                     ioe.initCause(e);
568                     throw ioe;
569                 }
570                 baseFile = FileUtil.normalizeFile(baseFile);
571                 FileObject baseFileObject = null;
572                 try{
573                     baseFileObject = FileUtil.toFileObject(baseFile);
574                 }catch(Exception JavaDoc e){
575                     IOException JavaDoc ioe = new IOException JavaDoc();
576                     ioe.initCause(e);
577                     throw ioe;
578                 }
579                 return baseFileObject;
580             }
581         }
582         return null;
583     }
584     
585     
586     private CatalogModel getResolver(FileObject baseFileObject) throws CatalogModelException{
587         if(baseFileObject != null && FileOwnerQuery.getOwner(baseFileObject) != null) {
588             return CatalogWriteModelFactory.getInstance().getCatalogWriteModelForProject(baseFileObject);
589         }
590         return this;
591     }
592     
593     
594     private Reader JavaDoc getFileStreamFromDocument(File JavaDoc resultFile) {
595         FileObject fo = FileUtil.toFileObject(FileUtil.normalizeFile(resultFile));
596         if(fo != null){
597             DataObject dobj = null;
598             try {
599                 dobj = DataObject.find(fo);
600             } catch (DataObjectNotFoundException ex) {
601                 return null;
602             }
603             if(dobj.isValid() && dobj.isModified()){
604         // DataObjectAdapters does not implement getByteStream
605
// so calling this here will effectively return null
606
return DataObjectAdapters.inputSource(dobj).getCharacterStream();
607             } else{
608                 //return null so that the validator will use normal file path to access doc
609
return null;
610             }
611         }
612         return null;
613     }
614     
615  
616     protected File JavaDoc resolveProjectProtocol(URI JavaDoc strRes) {
617         File JavaDoc result = null;
618         Project prj = FileOwnerQuery.getOwner(this.catalogFileObject);
619         if(prj != null){
620             ProjectCatalogSupport pcs = (ProjectCatalogSupport) prj.getLookup().lookup(ProjectCatalogSupport.class);
621             if(pcs.isProjectProtocol(strRes)){
622                 FileObject resFO = pcs.resolveProjectProtocol(strRes);
623                 if(resFO != null){
624                     return FileUtil.toFile(resFO);
625                 }
626             }
627         }
628         return result;
629     }
630     
631     
632     private ModelSource getModelSourceFromSystemWideCatalog(URI JavaDoc locationURI,
633             ModelSource modelSourceOfSourceDocument) {
634         if( locationURI == null)
635             return null;
636         
637         try {
638             Lookup.Template templ = new Lookup.Template(CatalogModel.class);
639             Lookup.Result res = Lookup.getDefault().lookup(templ);
640             Collection JavaDoc impls = res.allInstances();
641             for(Object JavaDoc obj : impls){
642                 CatalogModel cm = (CatalogModel) obj;
643                 return cm.getModelSource(locationURI,
644                         modelSourceOfSourceDocument);
645             }
646         } catch (CatalogModelException ex) {
647             //return null
648
}
649         
650         return null;
651     }
652 }
653
654
Popular Tags