KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > model > extensions > bpel > validation > TestCatalogModel


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.wsdl.model.extensions.bpel.validation;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.net.URI JavaDoc;
25 import javax.swing.text.Document JavaDoc;
26 import org.netbeans.modules.xml.retriever.catalog.impl.CatalogWriteModelImpl;
27 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
28 import org.netbeans.modules.xml.wsdl.model.WSDLModelFactory;
29 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
30 import org.netbeans.modules.xml.xam.ModelSource;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.FileUtil;
33 import org.openide.util.Lookup;
34 import org.openide.util.lookup.Lookups;
35
36 /**
37  *
38  * @author girix
39  */

40
41 public class TestCatalogModel extends CatalogWriteModelImpl {
42     private TestCatalogModel(){
43     }
44     
45     static TestCatalogModel singletonCatMod = null;
46
47     public static TestCatalogModel getDefault(){
48         if (singletonCatMod == null){
49             singletonCatMod = new TestCatalogModel();
50         }
51         return singletonCatMod;
52     }
53     
54     public ModelSource getModelSource(URI JavaDoc locationURI) throws CatalogModelException {
55         File JavaDoc file = new File JavaDoc(locationURI);
56         FileObject thisFileObj = FileUtil.toFileObject(file);
57         return createModelSource(thisFileObj, true);
58     }
59
60     public ModelSource getModelSource(URI JavaDoc locationURI, ModelSource modelSourceOfSourceDocument) throws CatalogModelException {
61         if(locationURI == null) {
62             return null;
63         }
64         
65         URI JavaDoc resolvedURI = locationURI;
66         
67         if(modelSourceOfSourceDocument != null) {
68             FileObject sFileObj = (FileObject) modelSourceOfSourceDocument.getLookup().lookup(FileObject.class);
69             if(sFileObj != null) {
70                 File JavaDoc sFile = FileUtil.toFile(sFileObj);
71                 if(sFile != null) {
72                     URI JavaDoc sURI = sFile.toURI();
73                     resolvedURI = sURI.resolve(locationURI);
74                 }
75             }
76         }
77         
78         if(resolvedURI != null) {
79             return getModelSource(resolvedURI);
80         }
81         
82         return null;
83     }
84
85     
86     protected Document JavaDoc getDocument(FileObject fo){
87         Document JavaDoc result = null;
88         
89         if (result != null) return result;
90         try {
91
92             File JavaDoc file = FileUtil.toFile(fo);
93             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
94             byte buffer[] = new byte[fis.available()];
95                 result = new org.netbeans.editor.BaseDocument(
96                         org.netbeans.modules.xml.text.syntax.XMLKit.class, false);
97 // result = new javax.swing.text.PlainDocument();
98
result.remove(0, result.getLength());
99             fis.read(buffer);
100             fis.close();
101             String JavaDoc str = new String JavaDoc(buffer);
102             result.insertString(0,str,null);
103             
104         } catch (Exception JavaDoc dObjEx) {
105             return null;
106         }
107         
108         return result;
109     }
110     
111     public ModelSource createModelSource(FileObject thisFileObj, boolean readOnly) throws CatalogModelException{
112         File JavaDoc file = FileUtil.toFile(thisFileObj);
113         Lookup lookup = Lookups.fixed(new Object JavaDoc[]{
114             file,
115             thisFileObj,
116             getDocument(thisFileObj),
117             getDefault()
118         });
119         return new ModelSource(lookup, readOnly);
120     }
121     
122     
123     
124     public WSDLModel getWSDLModel(URI JavaDoc locationURI) throws Exception JavaDoc {
125         ModelSource source = getDefault().getModelSource(locationURI);
126         WSDLModel model = WSDLModelFactory.getDefault().getModel(source);
127         model.sync();
128         return model;
129     }
130
131     
132     public String JavaDoc toString(){
133         return "TestCatalogModel"+super.toString();
134     }
135 }
136
137
Popular Tags