Revision 5945
Added by Jing Tao almost 14 years ago
src/edu/ucsb/nceas/metacat/SchemaLocationResolver.java | ||
---|---|---|
29 | 29 |
|
30 | 30 |
|
31 | 31 |
import java.sql.*; |
32 |
import java.util.Vector; |
|
32 | 33 |
import java.io.File; |
33 | 34 |
import java.io.BufferedInputStream; |
34 | 35 |
import java.io.FileWriter; |
... | ... | |
37 | 38 |
import java.io.IOException; |
38 | 39 |
|
39 | 40 |
import org.apache.log4j.Logger; |
41 |
import org.xml.sax.SAXException; |
|
40 | 42 |
|
41 | 43 |
import edu.ucsb.nceas.metacat.database.DBConnection; |
42 | 44 |
import edu.ucsb.nceas.metacat.database.DBConnectionPool; |
43 | 45 |
import edu.ucsb.nceas.metacat.service.ServiceService; |
46 |
import edu.ucsb.nceas.metacat.service.XMLSchemaParser; |
|
44 | 47 |
import edu.ucsb.nceas.metacat.service.XMLSchemaService; |
45 | 48 |
import edu.ucsb.nceas.metacat.util.SystemUtil; |
49 |
import edu.ucsb.nceas.utilities.PropertyNotFoundException; |
|
46 | 50 |
|
47 | 51 |
/** |
48 | 52 |
* A database aware Class to handle schema location. If namespace is in the |
... | ... | |
104 | 108 |
{ |
105 | 109 |
|
106 | 110 |
// upload schema into metacat |
107 |
InputStream in = DBEntityResolver.checkURLConnection(schemaLocation); |
|
108 |
|
|
109 |
String newURLInMetacat = uploadSchemaFromURL(in); |
|
111 |
String newURLInMetacat = uploadSchemaFromURL(schemaLocation); |
|
110 | 112 |
ServiceService.refreshService("XMLSchemaService"); |
111 | 113 |
|
112 | 114 |
// check the name space list again. It may not have appeared the first time |
... | ... | |
118 | 120 |
ServiceService.refreshService("XMLSchemaService"); |
119 | 121 |
} |
120 | 122 |
downloadNewSchema = true; |
121 |
|
|
123 |
//handle included schema in above schema |
|
124 |
String externalBaseURL = XMLSchemaService.getBaseUrlFromSchemaURL(schemaLocation); |
|
125 |
//System.out.println("==========the externalBaseURL is "+externalBaseURL); |
|
126 |
handleIncludedSchema(newURLInMetacat, externalBaseURL); |
|
122 | 127 |
} |
123 | 128 |
catch(Exception e) |
124 | 129 |
{ |
... | ... | |
129 | 134 |
}//if |
130 | 135 |
|
131 | 136 |
} |
137 |
|
|
138 |
/* |
|
139 |
* Some schemas (parent schema)also include other schemas (children) in |
|
140 |
* its definition. The syntax is: <xsd:include schemaLocation="product.xsd"/> |
|
141 |
* The included schemas need to be downloaded as well. |
|
142 |
* The included schemas always have the same target namespace. So there |
|
143 |
* is no need to register the schema into xml_catalog table. |
|
144 |
* Note: the included schemas may have included schemas (grandchildren) as well. |
|
145 |
*/ |
|
146 |
private void handleIncludedSchema(String parentSchemaNewURLInMetacat, |
|
147 |
String externalBaseURL) |
|
148 |
throws SAXException, IOException,PropertyNotFoundException |
|
149 |
{ |
|
150 |
//Gets the included schema information from parent schema |
|
151 |
//System.out.println("the parent schema new url in metacat ============="+parentSchemaNewURLInMetacat); |
|
152 |
if(externalBaseURL != null) |
|
153 |
{ |
|
154 |
InputStream in = DBEntityResolver.checkURLConnection(SystemUtil.getContextURL()+parentSchemaNewURLInMetacat); |
|
155 |
XMLSchemaParser parser = new XMLSchemaParser(in); |
|
156 |
parser.parse(); |
|
157 |
Vector<String> includedSchemaPaths = parser.getIncludedSchemaFilePathes(); |
|
158 |
//System.out.println("the include schema paths is ============ "+includedSchemaPaths); |
|
159 |
if(includedSchemaPaths!= null) |
|
160 |
{ |
|
161 |
for(int i=0; i<includedSchemaPaths.size(); i++) |
|
162 |
{ |
|
163 |
String schemaFilePath = includedSchemaPaths.elementAt(i); |
|
164 |
//System.out.println("start to handle the included path ========="+schemaFilePath); |
|
165 |
try |
|
166 |
{ |
|
167 |
String newShemaURLInMetacat = |
|
168 |
uploadSchemaFromURL(externalBaseURL+schemaFilePath); |
|
169 |
//System.out.println("success download the included schema and new url is ========="+newShemaURLInMetacat); |
|
170 |
//recursively download the included schema |
|
171 |
handleIncludedSchema(newShemaURLInMetacat, externalBaseURL); |
|
172 |
} |
|
173 |
catch(Exception e) |
|
174 |
{ |
|
175 |
logMetacat.warn("Warning on SchemaLocationResolver.handleIncludedSchema"+e.getMessage()); |
|
176 |
} |
|
177 |
|
|
178 |
} |
|
179 |
} |
|
180 |
} |
|
181 |
|
|
182 |
} |
|
132 | 183 |
|
133 | 184 |
/** |
134 | 185 |
* Upload new Schema located at outside URL to Metacat file system |
135 | 186 |
*/ |
136 |
private String uploadSchemaFromURL(InputStream istream) throws Exception
|
|
187 |
private String uploadSchemaFromURL(String schemaLocationURL) throws Exception
|
|
137 | 188 |
{ |
138 | 189 |
|
139 |
String relativeSchemaPath = XMLSchemaService.SCHEMA_DIR; |
|
190 |
String relativeSchemaPath = XMLSchemaService.SCHEMA_DIR;
|
|
140 | 191 |
String fullSchemaPath = SystemUtil.getContextDir() + relativeSchemaPath; |
141 | 192 |
String schemaURL = SystemUtil.getContextURL() + relativeSchemaPath; |
142 | 193 |
|
143 | 194 |
// get filename from systemId |
144 |
String filename = XMLSchemaService.getSchemaFileNameFromUri(schemaLocation); |
|
195 |
String filename = XMLSchemaService.getSchemaFileNameFromUri(schemaLocationURL);
|
|
145 | 196 |
|
146 | 197 |
if (filename != null && !(filename.trim()).equals("")) |
147 | 198 |
{ |
... | ... | |
157 | 208 |
} |
158 | 209 |
// writing schema text on Metacat file system as filename |
159 | 210 |
try { |
211 |
InputStream istream = DBEntityResolver.checkURLConnection(schemaLocationURL); |
|
160 | 212 |
// create a buffering character-input stream |
161 | 213 |
// that uses a default-sized input buffer |
162 | 214 |
BufferedInputStream in = new BufferedInputStream(istream); |
Also available in: Unified diff
Add code to download the included schema.