1 |
4430
|
daigle
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
|
|
* Purpose: A Class that implements session utility methods
|
4 |
|
|
* Copyright: 2008 Regents of the University of California and the
|
5 |
|
|
* National Center for Ecological Analysis and Synthesis
|
6 |
|
|
* Authors: Michael Daigle
|
7 |
|
|
*
|
8 |
|
|
* '$Author: daigle $'
|
9 |
|
|
* '$Date: 2008-08-22 16:23:38 -0700 (Fri, 22 Aug 2008) $'
|
10 |
|
|
* '$Revision: 4297 $'
|
11 |
|
|
*
|
12 |
|
|
* This program is free software; you can redistribute it and/or modify
|
13 |
|
|
* it under the terms of the GNU General Public License as published by
|
14 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
15 |
|
|
* (at your option) any later version.
|
16 |
|
|
*
|
17 |
|
|
* This program is distributed in the hope that it will be useful,
|
18 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20 |
|
|
* GNU General Public License for more details.
|
21 |
|
|
*
|
22 |
|
|
* You should have received a copy of the GNU General Public License
|
23 |
|
|
* along with this program; if not, write to the Free Software
|
24 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
25 |
|
|
*/
|
26 |
|
|
|
27 |
|
|
package edu.ucsb.nceas.metacat.service;
|
28 |
|
|
|
29 |
5619
|
berkley
|
import java.io.File;
|
30 |
|
|
import java.io.FileWriter;
|
31 |
4430
|
daigle
|
import java.io.IOException;
|
32 |
5619
|
berkley
|
import java.io.OutputStream;
|
33 |
4430
|
daigle
|
import java.io.StringReader;
|
34 |
5619
|
berkley
|
import java.net.HttpURLConnection;
|
35 |
|
|
import java.net.MalformedURLException;
|
36 |
|
|
import java.net.URL;
|
37 |
4430
|
daigle
|
import java.sql.PreparedStatement;
|
38 |
|
|
import java.sql.ResultSet;
|
39 |
|
|
import java.sql.SQLException;
|
40 |
|
|
import java.util.Vector;
|
41 |
|
|
import java.util.regex.Matcher;
|
42 |
|
|
import java.util.regex.Pattern;
|
43 |
|
|
|
44 |
5619
|
berkley
|
import org.apache.commons.io.IOUtils;
|
45 |
4430
|
daigle
|
import org.apache.log4j.Logger;
|
46 |
|
|
|
47 |
|
|
import edu.ucsb.nceas.metacat.DocumentImpl;
|
48 |
|
|
import edu.ucsb.nceas.metacat.MetaCatServlet;
|
49 |
5015
|
daigle
|
import edu.ucsb.nceas.metacat.database.DBConnection;
|
50 |
|
|
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
|
51 |
5030
|
daigle
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
52 |
5015
|
daigle
|
import edu.ucsb.nceas.metacat.shared.BaseService;
|
53 |
|
|
import edu.ucsb.nceas.metacat.shared.ServiceException;
|
54 |
4430
|
daigle
|
import edu.ucsb.nceas.utilities.FileUtil;
|
55 |
|
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
56 |
|
|
import edu.ucsb.nceas.utilities.StringUtil;
|
57 |
|
|
|
58 |
4442
|
daigle
|
public class XMLSchemaService extends BaseService {
|
59 |
4430
|
daigle
|
|
60 |
|
|
public static final String NAMESPACEKEYWORD = "xmlns";
|
61 |
|
|
|
62 |
|
|
public static final String SCHEMA_DIR = "/schema/";
|
63 |
|
|
|
64 |
|
|
private static XMLSchemaService xmlSchemaService = null;
|
65 |
|
|
|
66 |
|
|
private static Logger logMetacat = Logger.getLogger(XMLSchemaService.class);
|
67 |
|
|
|
68 |
|
|
private static boolean useFullSchemaValidation = false;
|
69 |
|
|
|
70 |
4709
|
daigle
|
// private static String documentNamespace = null;
|
71 |
4430
|
daigle
|
|
72 |
|
|
// all schema objects that represent schemas registered in the db that
|
73 |
|
|
// actually have files on disk.
|
74 |
|
|
private static Vector<XMLSchema> registeredSchemaList = new Vector<XMLSchema>();
|
75 |
|
|
|
76 |
|
|
// a convenience list that holds the names of registered namespaces.
|
77 |
|
|
private static Vector<String> nameSpaceList = new Vector<String>();
|
78 |
|
|
|
79 |
|
|
// a convenience string that holds all name spaces and locations in a space
|
80 |
|
|
// delimited format
|
81 |
|
|
private static String nameSpaceAndLocationString = "";
|
82 |
|
|
|
83 |
|
|
/**
|
84 |
|
|
* private constructor since this is a singleton
|
85 |
|
|
*/
|
86 |
|
|
private XMLSchemaService() {
|
87 |
5326
|
jones
|
_serviceName = "XMLSchemaService";
|
88 |
|
|
try {
|
89 |
|
|
doRefresh();
|
90 |
|
|
} catch (ServiceException e) {
|
91 |
|
|
logMetacat.debug(e.getMessage());
|
92 |
|
|
}
|
93 |
4430
|
daigle
|
}
|
94 |
|
|
|
95 |
|
|
/**
|
96 |
|
|
* Get the single instance of XMLService.
|
97 |
|
|
*
|
98 |
|
|
* @return the single instance of XMLService
|
99 |
|
|
*/
|
100 |
|
|
public static XMLSchemaService getInstance() {
|
101 |
|
|
if (xmlSchemaService == null) {
|
102 |
|
|
xmlSchemaService = new XMLSchemaService();
|
103 |
|
|
}
|
104 |
|
|
return xmlSchemaService;
|
105 |
|
|
}
|
106 |
|
|
|
107 |
4442
|
daigle
|
public boolean refreshable() {
|
108 |
|
|
return true;
|
109 |
|
|
}
|
110 |
|
|
|
111 |
4430
|
daigle
|
/**
|
112 |
|
|
* refresh the persistant values in this service.
|
113 |
|
|
*/
|
114 |
4981
|
daigle
|
public void doRefresh() throws ServiceException {
|
115 |
5326
|
jones
|
logMetacat.debug("XMLService.doRefresh - refreshing the schema service.");
|
116 |
4430
|
daigle
|
try {
|
117 |
|
|
populateRegisteredSchemaList();
|
118 |
|
|
setUseFullSchemaValidation();
|
119 |
|
|
createRegisteredNameSpaceList();
|
120 |
|
|
createRegisteredNameSpaceAndLocationString();
|
121 |
|
|
} catch (PropertyNotFoundException pnfe) {
|
122 |
5311
|
daigle
|
logMetacat.error("XMLService.doRefresh - Could not find property: xml.useFullSchemaValidation. " +
|
123 |
4430
|
daigle
|
"Setting to false.");
|
124 |
|
|
}
|
125 |
|
|
}
|
126 |
|
|
|
127 |
4981
|
daigle
|
public void stop() throws ServiceException {
|
128 |
|
|
return;
|
129 |
|
|
}
|
130 |
|
|
|
131 |
4430
|
daigle
|
/**
|
132 |
|
|
* Gets the registered schema list. This list holds schemas that exist in
|
133 |
|
|
* the xml_catalog table that also have associated files in the schema
|
134 |
|
|
* directory.
|
135 |
|
|
*
|
136 |
|
|
* @return a list of XMLSchema objects holding registered schema information
|
137 |
|
|
*/
|
138 |
5340
|
berkley
|
public Vector<XMLSchema> getRegisteredSchemaList() {
|
139 |
4430
|
daigle
|
return registeredSchemaList;
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
/**
|
143 |
|
|
* Gets the name space and location string. This is a convenience method.
|
144 |
|
|
* The string will have space delimited namespaces and locations that are
|
145 |
|
|
* held in the registered schema list.
|
146 |
|
|
*
|
147 |
|
|
* @return a string that holds space delimited registered namespaces and
|
148 |
|
|
* locations.
|
149 |
|
|
*/
|
150 |
5340
|
berkley
|
public String getNameSpaceAndLocationString() {
|
151 |
4430
|
daigle
|
return nameSpaceAndLocationString;
|
152 |
|
|
}
|
153 |
|
|
|
154 |
|
|
/**
|
155 |
|
|
* Gets a list of name spaces. This is a convenience method. The list will
|
156 |
|
|
* have all namespaces that are held in the registered schema list.
|
157 |
|
|
*
|
158 |
|
|
* @return a list that holds registered namespaces.
|
159 |
|
|
*/
|
160 |
5340
|
berkley
|
public Vector<String> getNameSpaceList() {
|
161 |
4430
|
daigle
|
return nameSpaceList;
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
/**
|
165 |
|
|
* Report whether xml parsing is set to use full schema parsing. If full
|
166 |
|
|
* schema parsing is true, new schemas will be validated before being
|
167 |
|
|
* entered into the database and file system.
|
168 |
|
|
*
|
169 |
|
|
* @return true if the xml.useFullSchemaValidation property is set to true,
|
170 |
|
|
* false otherwise.
|
171 |
|
|
*/
|
172 |
5340
|
berkley
|
public boolean useFullSchemaValidation() {
|
173 |
4430
|
daigle
|
return useFullSchemaValidation;
|
174 |
|
|
}
|
175 |
|
|
|
176 |
|
|
/**
|
177 |
|
|
* sets the UseFullSchemaValidation variable. The only way this should be
|
178 |
|
|
* set is in the constructor or the refresh methods.
|
179 |
|
|
*/
|
180 |
5340
|
berkley
|
private void setUseFullSchemaValidation() throws PropertyNotFoundException {
|
181 |
4430
|
daigle
|
String strUseFullSchemaValidation =
|
182 |
|
|
PropertyService.getProperty("xml.useFullSchemaValidation");
|
183 |
|
|
useFullSchemaValidation = Boolean.valueOf(strUseFullSchemaValidation);
|
184 |
|
|
}
|
185 |
|
|
|
186 |
|
|
/**
|
187 |
|
|
* Populate the list of registered schemas. This reads all schemas in the
|
188 |
|
|
* xml_catalog table and then makes sure the schema actually exists and is
|
189 |
|
|
* readable on disk.
|
190 |
|
|
*/
|
191 |
5340
|
berkley
|
public void populateRegisteredSchemaList() {
|
192 |
4430
|
daigle
|
DBConnection conn = null;
|
193 |
|
|
int serialNumber = -1;
|
194 |
|
|
PreparedStatement pstmt = null;
|
195 |
|
|
ResultSet resultSet = null;
|
196 |
|
|
registeredSchemaList = new Vector<XMLSchema>();
|
197 |
|
|
|
198 |
|
|
// get the system id from the xml_catalog table for all schemas.
|
199 |
|
|
String sql = "SELECT public_id, system_id FROM xml_catalog where "
|
200 |
|
|
+ "entry_type ='" + DocumentImpl.SCHEMA + "'";
|
201 |
|
|
try {
|
202 |
|
|
// check out DBConnection
|
203 |
|
|
conn = DBConnectionPool
|
204 |
|
|
.getDBConnection("XMLService.populateRegisteredSchemaList");
|
205 |
|
|
serialNumber = conn.getCheckOutSerialNumber();
|
206 |
|
|
pstmt = conn.prepareStatement(sql);
|
207 |
5311
|
daigle
|
logMetacat.debug("XMLService.populateRegisteredSchemaList - Selecting schemas: " + pstmt.toString());
|
208 |
4430
|
daigle
|
pstmt.execute();
|
209 |
|
|
resultSet = pstmt.getResultSet();
|
210 |
|
|
|
211 |
|
|
// make sure the schema actually exists on the file system. If so,
|
212 |
4664
|
daigle
|
// add it to the registered schema list.
|
213 |
4430
|
daigle
|
while (resultSet.next()) {
|
214 |
|
|
String fileNamespace = resultSet.getString(1);
|
215 |
|
|
String fileLocation = resultSet.getString(2);
|
216 |
5311
|
daigle
|
logMetacat.debug("XMLService.populateRegisteredSchemaList - Registering schema: " + fileNamespace + " " + fileLocation);
|
217 |
4430
|
daigle
|
XMLSchema xmlSchema = new XMLSchema(fileNamespace);
|
218 |
5619
|
berkley
|
if(fileLocation.startsWith("http://") || fileLocation.startsWith("https://"))
|
219 |
|
|
{
|
220 |
|
|
//System.out.println("processing an http schemal location");
|
221 |
|
|
logMetacat.debug("XMLService.populateRegisteredSchemaList - Processing http schema location: " + fileLocation);
|
222 |
|
|
xmlSchema.setExternalFileUri(fileLocation);
|
223 |
|
|
//cache the file
|
224 |
|
|
try
|
225 |
|
|
{
|
226 |
|
|
URL u = new URL(fileLocation);
|
227 |
|
|
//System.out.println("downloading " + fileLocation);
|
228 |
|
|
logMetacat.debug("XMLService.populateRegisteredSchemaList - Downloading http based schema...");
|
229 |
|
|
HttpURLConnection connection = (HttpURLConnection) u.openConnection();
|
230 |
|
|
connection.setDoOutput(true);
|
231 |
|
|
connection.setRequestMethod("GET");
|
232 |
|
|
connection.connect();
|
233 |
|
|
String schema = IOUtils.toString(connection.getInputStream());
|
234 |
|
|
|
235 |
|
|
String deployDir = PropertyService.getProperty("application.deployDir");
|
236 |
|
|
String contextName = PropertyService.getProperty("application.context");
|
237 |
|
|
String filename = fileLocation.substring(fileLocation.lastIndexOf("/"),
|
238 |
|
|
fileLocation.length());
|
239 |
|
|
File schemaFile = new File(deployDir + "/" + contextName + "/" +
|
240 |
|
|
"schema/" + filename);
|
241 |
|
|
//System.out.println("writing schema to " + schemaFile.getAbsolutePath());
|
242 |
|
|
FileWriter fw = new FileWriter(schemaFile);
|
243 |
|
|
fw.write(schema);
|
244 |
|
|
fw.flush();
|
245 |
|
|
fw.close();
|
246 |
|
|
logMetacat.debug("XMLService.populateRegisteredSchemaList - Schema downloaded to " + schemaFile.getAbsolutePath());
|
247 |
|
|
fileLocation = "/schema/" + filename;
|
248 |
|
|
//System.out.println("fileLocation set to " + fileLocation);
|
249 |
|
|
logMetacat.debug("XMLService.populateRegisteredSchemaList - fileLocation set to " + fileLocation);
|
250 |
|
|
xmlSchema.setFileName(fileLocation);
|
251 |
|
|
}
|
252 |
|
|
catch(MalformedURLException me)
|
253 |
|
|
{
|
254 |
|
|
logMetacat.warn("Could not cache a registered schema at " + fileLocation +
|
255 |
|
|
" because a connection could not be made to the given url: " +
|
256 |
|
|
me.getMessage());
|
257 |
|
|
}
|
258 |
|
|
catch (IOException ioe)
|
259 |
|
|
{
|
260 |
|
|
logMetacat.warn("Could not cache a registered schema at " + fileLocation +
|
261 |
|
|
" because an IOException occured: " + ioe.getMessage());
|
262 |
|
|
}
|
263 |
|
|
catch(PropertyNotFoundException pnfe)
|
264 |
|
|
{
|
265 |
|
|
logMetacat.warn("Could not cache a registered schema at " + fileLocation +
|
266 |
|
|
" because the property 'application.tempDir' could not be found.");
|
267 |
|
|
}
|
268 |
|
|
|
269 |
|
|
xmlSchema.setFileName(fileLocation);
|
270 |
|
|
}
|
271 |
|
|
else
|
272 |
|
|
{
|
273 |
|
|
xmlSchema.setFileName(fileLocation);
|
274 |
|
|
}
|
275 |
|
|
|
276 |
|
|
if (FileUtil.getFileStatus(xmlSchema.getLocalFileDir()) >= FileUtil.EXISTS_READABLE)
|
277 |
|
|
{
|
278 |
4430
|
daigle
|
registeredSchemaList.add(xmlSchema);
|
279 |
5619
|
berkley
|
}
|
280 |
|
|
else if(fileLocation.startsWith("http://") || fileLocation.startsWith("https://"))
|
281 |
|
|
{ //the schema resides on a different server, to validate, we need to go get it
|
282 |
|
|
registeredSchemaList.add(xmlSchema);
|
283 |
|
|
}
|
284 |
|
|
else
|
285 |
|
|
{
|
286 |
5311
|
daigle
|
logMetacat.warn("XMLService.populateRegisteredSchemaList - Schema file: " + xmlSchema.getLocalFileDir() + " is registered "
|
287 |
4430
|
daigle
|
+ " in the database but does not exist on the file system.");
|
288 |
|
|
}
|
289 |
|
|
}
|
290 |
|
|
} catch (SQLException e) {
|
291 |
5311
|
daigle
|
logMetacat.error("XMLService.populateRegisteredSchemaList - SQL Error: "
|
292 |
4430
|
daigle
|
+ e.getMessage());
|
293 |
|
|
} finally {
|
294 |
|
|
try {
|
295 |
|
|
pstmt.close();
|
296 |
|
|
}// try
|
297 |
|
|
catch (SQLException sqlE) {
|
298 |
5311
|
daigle
|
logMetacat.error("XMLSchemaService.populateRegisteredSchemaList - Error in XMLService.populateRegisteredSchemaList(): "
|
299 |
4430
|
daigle
|
+ sqlE.getMessage());
|
300 |
|
|
}
|
301 |
|
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
302 |
|
|
}
|
303 |
|
|
}
|
304 |
|
|
|
305 |
|
|
/**
|
306 |
|
|
* create a space delimited string of all namespaces and locations
|
307 |
|
|
* in the registered schema list.
|
308 |
|
|
*/
|
309 |
|
|
private static void createRegisteredNameSpaceAndLocationString() {
|
310 |
|
|
boolean firstRow = true;
|
311 |
4442
|
daigle
|
nameSpaceAndLocationString = "";
|
312 |
|
|
|
313 |
4430
|
daigle
|
for (XMLSchema xmlSchema : registeredSchemaList) {
|
314 |
|
|
if (!firstRow) {
|
315 |
|
|
nameSpaceAndLocationString += " ";
|
316 |
|
|
}
|
317 |
|
|
nameSpaceAndLocationString += xmlSchema.getFileNamespace() + " "
|
318 |
|
|
+ xmlSchema.getLocalFileUri();
|
319 |
|
|
firstRow = false;
|
320 |
|
|
}
|
321 |
|
|
}
|
322 |
|
|
|
323 |
|
|
/**
|
324 |
|
|
* create a lsit of all namespaces in the registered schema list.
|
325 |
|
|
*/
|
326 |
|
|
private static void createRegisteredNameSpaceList() {
|
327 |
4442
|
daigle
|
nameSpaceList = new Vector<String>();
|
328 |
4430
|
daigle
|
for (XMLSchema xmlSchema : registeredSchemaList) {
|
329 |
|
|
nameSpaceList.add(xmlSchema.getFileNamespace());
|
330 |
|
|
}
|
331 |
|
|
}
|
332 |
|
|
|
333 |
|
|
/**
|
334 |
|
|
* Checks to see that all schemas are registered. If a single one in the
|
335 |
|
|
* list is not, this will return false.
|
336 |
|
|
*
|
337 |
|
|
* @param schemaList
|
338 |
|
|
* a list of schemas as they appear in xml.
|
339 |
|
|
* @return true if all schemas are registered.
|
340 |
|
|
*/
|
341 |
|
|
public static boolean areAllSchemasRegistered(Vector<XMLSchema> schemaList) {
|
342 |
|
|
for (XMLSchema xmlSchema : schemaList) {
|
343 |
|
|
if ( ! isSchemaRegistered(xmlSchema)) {
|
344 |
|
|
return false;
|
345 |
|
|
}
|
346 |
|
|
}
|
347 |
|
|
return true;
|
348 |
|
|
}
|
349 |
|
|
|
350 |
|
|
/**
|
351 |
|
|
* Returns true if the schema is registered.
|
352 |
|
|
*
|
353 |
|
|
* @param schema
|
354 |
|
|
* a single schema as it appears in xml
|
355 |
|
|
* @return true if the schema is registered, false otherwise.
|
356 |
|
|
*/
|
357 |
|
|
public static boolean isSchemaRegistered(XMLSchema xmlSchema) {
|
358 |
|
|
for (XMLSchema registeredXmlSchema : registeredSchemaList) {
|
359 |
|
|
if (registeredXmlSchema.getLocalFileUri().equals(
|
360 |
|
|
xmlSchema.getLocalFileUri())
|
361 |
|
|
&& registeredXmlSchema.getFileNamespace().equals(
|
362 |
|
|
xmlSchema.getFileNamespace())) {
|
363 |
|
|
return true;
|
364 |
|
|
}
|
365 |
|
|
}
|
366 |
|
|
|
367 |
|
|
return false;
|
368 |
|
|
}
|
369 |
|
|
|
370 |
|
|
/**
|
371 |
|
|
* See if schemas have been specified in the xml:schemalocation attribute.
|
372 |
|
|
* If so, return a vector of the system ids.
|
373 |
|
|
*
|
374 |
|
|
* @param xml
|
375 |
|
|
* the document we want to look in for schema location
|
376 |
|
|
* @return a vector of XMLSchema objects, or an empty vector if none are
|
377 |
|
|
* found
|
378 |
|
|
*/
|
379 |
|
|
public static Vector<XMLSchema> findSchemasInXML(StringReader xml) throws IOException {
|
380 |
|
|
Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
|
381 |
|
|
Vector<XMLSchema> schemaList = new Vector<XMLSchema>();
|
382 |
|
|
|
383 |
|
|
// no xml. return empty vector
|
384 |
|
|
if (xml == null) {
|
385 |
5311
|
daigle
|
logMetacat.debug("XMLSchemaService.findSchemasInXML - Returning empty schemaList.");
|
386 |
4430
|
daigle
|
return schemaList;
|
387 |
|
|
}
|
388 |
|
|
|
389 |
|
|
// Get the "second line" from the xml
|
390 |
|
|
String targetLine = getSchemaLine(xml);
|
391 |
|
|
|
392 |
|
|
// see if there is a match for xsi.schemaLocation. If so, extract the
|
393 |
|
|
// schemas.
|
394 |
|
|
if (targetLine != null) {
|
395 |
5368
|
leinfelder
|
String regex = "(\\p{Graph}*):schemaLocation=\"([^\"]*)\"";
|
396 |
4430
|
daigle
|
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE
|
397 |
|
|
| Pattern.DOTALL);
|
398 |
|
|
Matcher matcher = pattern.matcher(targetLine);
|
399 |
|
|
int i = 0;
|
400 |
|
|
while (i < targetLine.length()) {
|
401 |
|
|
if (!matcher.find(i)) {
|
402 |
|
|
break;
|
403 |
|
|
}
|
404 |
|
|
|
405 |
|
|
String uri = matcher.group(2);
|
406 |
|
|
uri = StringUtil.replaceTabsNewLines(uri);
|
407 |
|
|
uri = StringUtil.replaceDuplicateSpaces(uri);
|
408 |
|
|
|
409 |
|
|
// each namespace could have several schema locations. parsedUri
|
410 |
|
|
// will hold a list of uri and files.
|
411 |
|
|
Vector<String> parsedUri = StringUtil.toVector(uri, ' ');
|
412 |
|
|
for (int j = 0; j < parsedUri.size(); j = j + 2) {
|
413 |
|
|
if (j + 1 >= parsedUri.size()) {
|
414 |
|
|
throw new IOException(
|
415 |
|
|
"Odd number of elements found when parsing schema location: "
|
416 |
|
|
+ targetLine
|
417 |
|
|
+ ". There should be an even number of uri/files in location.");
|
418 |
|
|
}
|
419 |
|
|
XMLSchema xmlSchema = new XMLSchema(parsedUri.get(j), parsedUri
|
420 |
|
|
.get(j + 1));
|
421 |
|
|
schemaList.add(xmlSchema);
|
422 |
|
|
}
|
423 |
|
|
i = matcher.end();
|
424 |
|
|
}
|
425 |
|
|
}
|
426 |
|
|
|
427 |
5311
|
daigle
|
logMetacat.debug("XMLSchemaService.findSchemasInXML - Schemas for eml are " + schemaList.toString());
|
428 |
4430
|
daigle
|
|
429 |
|
|
return schemaList;
|
430 |
|
|
}
|
431 |
|
|
|
432 |
|
|
/**
|
433 |
|
|
* Returns all the namespace for an xml document. This is done by getting
|
434 |
|
|
* the internal namespace declaration (prefix) and looking for xmlns:<prefix>
|
435 |
|
|
*
|
436 |
|
|
* @param xml
|
437 |
|
|
* the document to search
|
438 |
|
|
* @return a string holding the namespace
|
439 |
|
|
*/
|
440 |
|
|
public static String findDocumentNamespace(StringReader xml) throws IOException {
|
441 |
|
|
String namespace = null;
|
442 |
|
|
|
443 |
|
|
String eml2_0_0NameSpace = DocumentImpl.EML2_0_0NAMESPACE;
|
444 |
|
|
String eml2_0_1NameSpace = DocumentImpl.EML2_0_1NAMESPACE;
|
445 |
|
|
String eml2_1_0NameSpace = DocumentImpl.EML2_1_0NAMESPACE;
|
446 |
5709
|
leinfelder
|
String eml2_1_1NameSpace = DocumentImpl.EML2_1_1NAMESPACE;
|
447 |
4430
|
daigle
|
|
448 |
5709
|
leinfelder
|
|
449 |
4430
|
daigle
|
if (xml == null) {
|
450 |
5311
|
daigle
|
logMetacat.debug("XMLSchemaService.findDocumentNamespace - XML doc is null. There is no namespace.");
|
451 |
4430
|
daigle
|
return namespace;
|
452 |
|
|
}
|
453 |
|
|
|
454 |
|
|
String targetLine = getSchemaLine(xml);
|
455 |
|
|
|
456 |
|
|
// the prefix is at the beginning of the doc
|
457 |
|
|
String prefix = null;
|
458 |
4474
|
daigle
|
String regex1 = "^\\s*(\\p{Graph}+):\\p{Graph}* ";
|
459 |
4430
|
daigle
|
Pattern pattern = Pattern.compile(regex1, Pattern.CASE_INSENSITIVE);
|
460 |
|
|
Matcher matcher = pattern.matcher(targetLine);
|
461 |
|
|
if (matcher.find()) {
|
462 |
|
|
prefix = matcher.group(1).trim();
|
463 |
|
|
}
|
464 |
|
|
|
465 |
|
|
// if a prefix was found, we are looking for xmlns:<prefix>="namespace"
|
466 |
|
|
// if no prefix was found, we grab the first namespace.
|
467 |
|
|
String regex2;
|
468 |
|
|
if (prefix != null) {
|
469 |
6526
|
leinfelder
|
regex2 = "xmlns:" + prefix + "=['\"](.*)['\"]";
|
470 |
4430
|
daigle
|
} else {
|
471 |
6526
|
leinfelder
|
regex2 = "xmlns:.*=['\"](.*)['\"]";
|
472 |
4430
|
daigle
|
}
|
473 |
|
|
Pattern pattern2 = Pattern.compile(regex2, Pattern.CASE_INSENSITIVE);
|
474 |
|
|
Matcher matcher2 = pattern2.matcher(targetLine);
|
475 |
|
|
if (matcher2.find()) {
|
476 |
|
|
namespace = matcher2.group(1);
|
477 |
|
|
|
478 |
|
|
if (namespace.indexOf(eml2_0_0NameSpace) != -1) {
|
479 |
|
|
namespace = eml2_0_0NameSpace;
|
480 |
|
|
} else if (namespace.indexOf(eml2_0_1NameSpace) != -1) {
|
481 |
|
|
namespace = eml2_0_1NameSpace;
|
482 |
|
|
} else if (namespace.indexOf(eml2_1_0NameSpace) != -1) {
|
483 |
|
|
namespace = eml2_1_0NameSpace;
|
484 |
5709
|
leinfelder
|
} else if (namespace.indexOf(eml2_1_1NameSpace) != -1) {
|
485 |
|
|
namespace = eml2_1_1NameSpace;
|
486 |
4430
|
daigle
|
}
|
487 |
|
|
}
|
488 |
|
|
|
489 |
|
|
return namespace;
|
490 |
|
|
}
|
491 |
|
|
|
492 |
|
|
/**
|
493 |
|
|
* Return the line from xml that holds the metadata like namespace and
|
494 |
|
|
* schema location
|
495 |
|
|
*
|
496 |
|
|
* @param xml
|
497 |
|
|
* the document to parse
|
498 |
|
|
* @return the "second" line of the document
|
499 |
|
|
*/
|
500 |
|
|
private static String getSchemaLine(StringReader xml) throws IOException {
|
501 |
|
|
Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
|
502 |
|
|
// find the line
|
503 |
|
|
String secondLine = null;
|
504 |
|
|
int count = 0;
|
505 |
|
|
final int TARGETNUM = 1;
|
506 |
|
|
StringBuffer buffer = new StringBuffer();
|
507 |
|
|
boolean comment = false;
|
508 |
|
|
boolean processingInstruction = false;
|
509 |
|
|
char thirdPreviousCharacter = '?';
|
510 |
|
|
char secondPreviousCharacter = '?';
|
511 |
|
|
char previousCharacter = '?';
|
512 |
|
|
char currentCharacter = '?';
|
513 |
|
|
int tmp = xml.read();
|
514 |
|
|
while (tmp != -1) {
|
515 |
|
|
currentCharacter = (char)tmp;
|
516 |
|
|
//in a comment
|
517 |
|
|
if (currentCharacter == '-' && previousCharacter == '-'
|
518 |
|
|
&& secondPreviousCharacter == '!'
|
519 |
|
|
&& thirdPreviousCharacter == '<') {
|
520 |
|
|
comment = true;
|
521 |
|
|
}
|
522 |
|
|
//out of comment
|
523 |
|
|
if (comment && currentCharacter == '>' && previousCharacter == '-'
|
524 |
|
|
&& secondPreviousCharacter == '-') {
|
525 |
|
|
comment = false;
|
526 |
|
|
}
|
527 |
|
|
|
528 |
|
|
//in a processingInstruction
|
529 |
|
|
if (currentCharacter == '?' && previousCharacter == '<') {
|
530 |
|
|
processingInstruction = true;
|
531 |
|
|
}
|
532 |
|
|
|
533 |
|
|
//out of processingInstruction
|
534 |
|
|
if (processingInstruction && currentCharacter == '>'
|
535 |
|
|
&& previousCharacter == '?') {
|
536 |
|
|
processingInstruction = false;
|
537 |
|
|
}
|
538 |
|
|
|
539 |
|
|
//this is not comment or a processingInstruction
|
540 |
|
|
if (currentCharacter != '!' && previousCharacter == '<'
|
541 |
|
|
&& !comment && !processingInstruction) {
|
542 |
|
|
count++;
|
543 |
|
|
}
|
544 |
|
|
|
545 |
|
|
// get target line
|
546 |
|
|
if (count == TARGETNUM && currentCharacter != '>') {
|
547 |
|
|
buffer.append(currentCharacter);
|
548 |
|
|
}
|
549 |
|
|
if (count == TARGETNUM && currentCharacter == '>') {
|
550 |
|
|
break;
|
551 |
|
|
}
|
552 |
|
|
thirdPreviousCharacter = secondPreviousCharacter;
|
553 |
|
|
secondPreviousCharacter = previousCharacter;
|
554 |
|
|
previousCharacter = currentCharacter;
|
555 |
|
|
tmp = xml.read();
|
556 |
|
|
}
|
557 |
|
|
secondLine = buffer.toString();
|
558 |
5311
|
daigle
|
logMetacat.debug("XMLSchemaService.getSchemaLine - the second line string is: " + secondLine);
|
559 |
4430
|
daigle
|
|
560 |
|
|
xml.reset();
|
561 |
|
|
return secondLine;
|
562 |
|
|
}
|
563 |
|
|
|
564 |
|
|
/**
|
565 |
|
|
* Get a schema file name from the schema uri.
|
566 |
|
|
*
|
567 |
|
|
* @param uri
|
568 |
|
|
* the uri from which to extract the file name
|
569 |
|
|
* @return a string holding the file name
|
570 |
|
|
*/
|
571 |
|
|
public static String getSchemaFileNameFromUri(String uri) {
|
572 |
|
|
// get filename from systemId
|
573 |
|
|
String filename = uri;
|
574 |
|
|
|
575 |
|
|
if (filename != null && !(filename.trim()).equals("")) {
|
576 |
|
|
int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
|
577 |
|
|
if (slash > -1) {
|
578 |
|
|
filename = filename.substring(slash + 1);
|
579 |
|
|
}
|
580 |
|
|
}
|
581 |
|
|
|
582 |
|
|
return filename;
|
583 |
|
|
}
|
584 |
5941
|
tao
|
|
585 |
|
|
/**
|
586 |
|
|
* Get a base url from the schema url. If url=http://www.example.com/example.xsd,
|
587 |
|
|
* http://www.example.com/ will be returned.
|
588 |
|
|
*
|
589 |
|
|
* @param uri
|
590 |
|
|
* the uri from which to extract the base url
|
591 |
|
|
* @return a string holding the base url. null will be return if it is not url.
|
592 |
|
|
*/
|
593 |
|
|
public static String getBaseUrlFromSchemaURL(String url)
|
594 |
|
|
{
|
595 |
|
|
String baseURL = null;
|
596 |
|
|
if (url != null && (url.indexOf("http://") != -1 || url.indexOf("https://") !=-1))
|
597 |
|
|
{
|
598 |
|
|
int slash = url.lastIndexOf('/');
|
599 |
|
|
if (slash > -1)
|
600 |
|
|
{
|
601 |
|
|
baseURL = url.substring(0,slash+1);
|
602 |
|
|
}
|
603 |
|
|
}
|
604 |
|
|
return baseURL;
|
605 |
|
|
}
|
606 |
4430
|
daigle
|
}
|