1
|
/**
|
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
|
import java.io.File;
|
30
|
import java.io.FileWriter;
|
31
|
import java.io.IOException;
|
32
|
import java.io.OutputStream;
|
33
|
import java.io.StringReader;
|
34
|
import java.net.HttpURLConnection;
|
35
|
import java.net.MalformedURLException;
|
36
|
import java.net.URL;
|
37
|
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
|
import org.apache.commons.io.IOUtils;
|
45
|
import org.apache.log4j.Logger;
|
46
|
|
47
|
import edu.ucsb.nceas.metacat.DocumentImpl;
|
48
|
import edu.ucsb.nceas.metacat.MetaCatServlet;
|
49
|
import edu.ucsb.nceas.metacat.database.DBConnection;
|
50
|
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
|
51
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
52
|
import edu.ucsb.nceas.metacat.shared.BaseService;
|
53
|
import edu.ucsb.nceas.metacat.shared.ServiceException;
|
54
|
import edu.ucsb.nceas.utilities.FileUtil;
|
55
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
56
|
import edu.ucsb.nceas.utilities.StringUtil;
|
57
|
|
58
|
public class XMLSchemaService extends BaseService {
|
59
|
|
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
|
// private static String documentNamespace = null;
|
71
|
|
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
|
_serviceName = "XMLSchemaService";
|
88
|
try {
|
89
|
doRefresh();
|
90
|
} catch (ServiceException e) {
|
91
|
logMetacat.debug(e.getMessage());
|
92
|
}
|
93
|
}
|
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
|
public boolean refreshable() {
|
108
|
return true;
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* refresh the persistant values in this service.
|
113
|
*/
|
114
|
public void doRefresh() throws ServiceException {
|
115
|
logMetacat.debug("XMLService.doRefresh - refreshing the schema service.");
|
116
|
try {
|
117
|
populateRegisteredSchemaList();
|
118
|
setUseFullSchemaValidation();
|
119
|
createRegisteredNameSpaceList();
|
120
|
createRegisteredNameSpaceAndLocationString();
|
121
|
} catch (PropertyNotFoundException pnfe) {
|
122
|
logMetacat.error("XMLService.doRefresh - Could not find property: xml.useFullSchemaValidation. " +
|
123
|
"Setting to false.");
|
124
|
}
|
125
|
}
|
126
|
|
127
|
public void stop() throws ServiceException {
|
128
|
return;
|
129
|
}
|
130
|
|
131
|
/**
|
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
|
public Vector<XMLSchema> getRegisteredSchemaList() {
|
139
|
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
|
public String getNameSpaceAndLocationString() {
|
151
|
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
|
public Vector<String> getNameSpaceList() {
|
161
|
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
|
public boolean useFullSchemaValidation() {
|
173
|
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
|
private void setUseFullSchemaValidation() throws PropertyNotFoundException {
|
181
|
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
|
public void populateRegisteredSchemaList() {
|
192
|
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
|
logMetacat.debug("XMLService.populateRegisteredSchemaList - Selecting schemas: " + pstmt.toString());
|
208
|
pstmt.execute();
|
209
|
resultSet = pstmt.getResultSet();
|
210
|
|
211
|
// make sure the schema actually exists on the file system. If so,
|
212
|
// add it to the registered schema list.
|
213
|
while (resultSet.next()) {
|
214
|
String fileNamespace = resultSet.getString(1);
|
215
|
String fileLocation = resultSet.getString(2);
|
216
|
logMetacat.debug("XMLService.populateRegisteredSchemaList - Registering schema: " + fileNamespace + " " + fileLocation);
|
217
|
XMLSchema xmlSchema = new XMLSchema(fileNamespace);
|
218
|
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.setReadTimeout(5000);
|
233
|
//System.out.println("the ============== the read timeout is ================"+connection.getReadTimeout());
|
234
|
//System.out.println("the ============== the connection timeout is ================"+connection.getConnectTimeout());
|
235
|
connection.connect();
|
236
|
String schema = IOUtils.toString(connection.getInputStream());
|
237
|
|
238
|
String deployDir = PropertyService.getProperty("application.deployDir");
|
239
|
String contextName = PropertyService.getProperty("application.context");
|
240
|
String filename = fileLocation.substring(fileLocation.lastIndexOf("/"),
|
241
|
fileLocation.length());
|
242
|
File schemaFile = new File(deployDir + "/" + contextName + "/" +
|
243
|
"schema/" + filename);
|
244
|
//System.out.println("writing schema to " + schemaFile.getAbsolutePath());
|
245
|
FileWriter fw = new FileWriter(schemaFile);
|
246
|
fw.write(schema);
|
247
|
fw.flush();
|
248
|
fw.close();
|
249
|
logMetacat.debug("XMLService.populateRegisteredSchemaList - Schema downloaded to " + schemaFile.getAbsolutePath());
|
250
|
fileLocation = "/schema/" + filename;
|
251
|
//System.out.println("fileLocation set to " + fileLocation);
|
252
|
logMetacat.debug("XMLService.populateRegisteredSchemaList - fileLocation set to " + fileLocation);
|
253
|
xmlSchema.setFileName(fileLocation);
|
254
|
}
|
255
|
catch(MalformedURLException me)
|
256
|
{
|
257
|
logMetacat.warn("Could not cache a registered schema at " + fileLocation +
|
258
|
" because a connection could not be made to the given url: " +
|
259
|
me.getMessage());
|
260
|
}
|
261
|
catch (IOException ioe)
|
262
|
{
|
263
|
logMetacat.warn("Could not cache a registered schema at " + fileLocation +
|
264
|
" because an IOException occured: " + ioe.getMessage());
|
265
|
}
|
266
|
catch(PropertyNotFoundException pnfe)
|
267
|
{
|
268
|
logMetacat.warn("Could not cache a registered schema at " + fileLocation +
|
269
|
" because the property 'application.tempDir' could not be found.");
|
270
|
}
|
271
|
|
272
|
xmlSchema.setFileName(fileLocation);
|
273
|
}
|
274
|
else
|
275
|
{
|
276
|
xmlSchema.setFileName(fileLocation);
|
277
|
}
|
278
|
|
279
|
if (FileUtil.getFileStatus(xmlSchema.getLocalFileDir()) >= FileUtil.EXISTS_READABLE)
|
280
|
{
|
281
|
registeredSchemaList.add(xmlSchema);
|
282
|
}
|
283
|
else if(fileLocation.startsWith("http://") || fileLocation.startsWith("https://"))
|
284
|
{ //the schema resides on a different server, to validate, we need to go get it
|
285
|
registeredSchemaList.add(xmlSchema);
|
286
|
}
|
287
|
else
|
288
|
{
|
289
|
logMetacat.warn("XMLService.populateRegisteredSchemaList - Schema file: " + xmlSchema.getLocalFileDir() + " is registered "
|
290
|
+ " in the database but does not exist on the file system.");
|
291
|
}
|
292
|
}
|
293
|
} catch (SQLException e) {
|
294
|
logMetacat.error("XMLService.populateRegisteredSchemaList - SQL Error: "
|
295
|
+ e.getMessage());
|
296
|
} finally {
|
297
|
try {
|
298
|
pstmt.close();
|
299
|
}// try
|
300
|
catch (SQLException sqlE) {
|
301
|
logMetacat.error("XMLSchemaService.populateRegisteredSchemaList - Error in XMLService.populateRegisteredSchemaList(): "
|
302
|
+ sqlE.getMessage());
|
303
|
}
|
304
|
DBConnectionPool.returnDBConnection(conn, serialNumber);
|
305
|
}
|
306
|
}
|
307
|
|
308
|
/**
|
309
|
* create a space delimited string of all namespaces and locations
|
310
|
* in the registered schema list.
|
311
|
*/
|
312
|
private static void createRegisteredNameSpaceAndLocationString() {
|
313
|
boolean firstRow = true;
|
314
|
nameSpaceAndLocationString = "";
|
315
|
|
316
|
for (XMLSchema xmlSchema : registeredSchemaList) {
|
317
|
if (!firstRow) {
|
318
|
nameSpaceAndLocationString += " ";
|
319
|
}
|
320
|
nameSpaceAndLocationString += xmlSchema.getFileNamespace() + " "
|
321
|
+ xmlSchema.getLocalFileUri();
|
322
|
firstRow = false;
|
323
|
}
|
324
|
}
|
325
|
|
326
|
/**
|
327
|
* create a lsit of all namespaces in the registered schema list.
|
328
|
*/
|
329
|
private static void createRegisteredNameSpaceList() {
|
330
|
nameSpaceList = new Vector<String>();
|
331
|
for (XMLSchema xmlSchema : registeredSchemaList) {
|
332
|
nameSpaceList.add(xmlSchema.getFileNamespace());
|
333
|
}
|
334
|
}
|
335
|
|
336
|
/**
|
337
|
* Checks to see that all schemas are registered. If a single one in the
|
338
|
* list is not, this will return false.
|
339
|
*
|
340
|
* @param schemaList
|
341
|
* a list of schemas as they appear in xml.
|
342
|
* @return true if all schemas are registered.
|
343
|
*/
|
344
|
public static boolean areAllSchemasRegistered(Vector<XMLSchema> schemaList) {
|
345
|
for (XMLSchema xmlSchema : schemaList) {
|
346
|
if ( ! isSchemaRegistered(xmlSchema)) {
|
347
|
return false;
|
348
|
}
|
349
|
}
|
350
|
return true;
|
351
|
}
|
352
|
|
353
|
/**
|
354
|
* Returns true if the schema is registered.
|
355
|
*
|
356
|
* @param schema
|
357
|
* a single schema as it appears in xml
|
358
|
* @return true if the schema is registered, false otherwise.
|
359
|
*/
|
360
|
public static boolean isSchemaRegistered(XMLSchema xmlSchema) {
|
361
|
for (XMLSchema registeredXmlSchema : registeredSchemaList) {
|
362
|
if (registeredXmlSchema.getLocalFileUri().equals(
|
363
|
xmlSchema.getLocalFileUri())
|
364
|
&& registeredXmlSchema.getFileNamespace().equals(
|
365
|
xmlSchema.getFileNamespace())) {
|
366
|
return true;
|
367
|
}
|
368
|
}
|
369
|
|
370
|
return false;
|
371
|
}
|
372
|
|
373
|
/**
|
374
|
* See if schemas have been specified in the xml:schemalocation attribute.
|
375
|
* If so, return a vector of the system ids.
|
376
|
*
|
377
|
* @param xml
|
378
|
* the document we want to look in for schema location
|
379
|
* @return a vector of XMLSchema objects, or an empty vector if none are
|
380
|
* found
|
381
|
*/
|
382
|
public static Vector<XMLSchema> findSchemasInXML(StringReader xml) throws IOException {
|
383
|
Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
|
384
|
Vector<XMLSchema> schemaList = new Vector<XMLSchema>();
|
385
|
|
386
|
// no xml. return empty vector
|
387
|
if (xml == null) {
|
388
|
logMetacat.debug("XMLSchemaService.findSchemasInXML - Returning empty schemaList.");
|
389
|
return schemaList;
|
390
|
}
|
391
|
|
392
|
// Get the "second line" from the xml
|
393
|
String targetLine = getSchemaLine(xml);
|
394
|
|
395
|
// see if there is a match for xsi.schemaLocation. If so, extract the
|
396
|
// schemas.
|
397
|
if (targetLine != null) {
|
398
|
String regex = "(\\p{Graph}*):schemaLocation=\"([^\"]*)\"";
|
399
|
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE
|
400
|
| Pattern.DOTALL);
|
401
|
Matcher matcher = pattern.matcher(targetLine);
|
402
|
int i = 0;
|
403
|
while (i < targetLine.length()) {
|
404
|
if (!matcher.find(i)) {
|
405
|
break;
|
406
|
}
|
407
|
|
408
|
String uri = matcher.group(2);
|
409
|
uri = StringUtil.replaceTabsNewLines(uri);
|
410
|
uri = StringUtil.replaceDuplicateSpaces(uri);
|
411
|
|
412
|
// each namespace could have several schema locations. parsedUri
|
413
|
// will hold a list of uri and files.
|
414
|
Vector<String> parsedUri = StringUtil.toVector(uri, ' ');
|
415
|
for (int j = 0; j < parsedUri.size(); j = j + 2) {
|
416
|
if (j + 1 >= parsedUri.size()) {
|
417
|
throw new IOException(
|
418
|
"Odd number of elements found when parsing schema location: "
|
419
|
+ targetLine
|
420
|
+ ". There should be an even number of uri/files in location.");
|
421
|
}
|
422
|
XMLSchema xmlSchema = new XMLSchema(parsedUri.get(j), parsedUri
|
423
|
.get(j + 1));
|
424
|
schemaList.add(xmlSchema);
|
425
|
}
|
426
|
i = matcher.end();
|
427
|
}
|
428
|
}
|
429
|
|
430
|
logMetacat.debug("XMLSchemaService.findSchemasInXML - Schemas for eml are " + schemaList.toString());
|
431
|
|
432
|
return schemaList;
|
433
|
}
|
434
|
|
435
|
/**
|
436
|
* Returns all the namespace for an xml document. This is done by getting
|
437
|
* the internal namespace declaration (prefix) and looking for xmlns:<prefix>
|
438
|
*
|
439
|
* @param xml
|
440
|
* the document to search
|
441
|
* @return a string holding the namespace
|
442
|
*/
|
443
|
public static String findDocumentNamespace(StringReader xml) throws IOException {
|
444
|
String namespace = null;
|
445
|
|
446
|
String eml2_0_0NameSpace = DocumentImpl.EML2_0_0NAMESPACE;
|
447
|
String eml2_0_1NameSpace = DocumentImpl.EML2_0_1NAMESPACE;
|
448
|
String eml2_1_0NameSpace = DocumentImpl.EML2_1_0NAMESPACE;
|
449
|
String eml2_1_1NameSpace = DocumentImpl.EML2_1_1NAMESPACE;
|
450
|
|
451
|
|
452
|
if (xml == null) {
|
453
|
logMetacat.debug("XMLSchemaService.findDocumentNamespace - XML doc is null. There is no namespace.");
|
454
|
return namespace;
|
455
|
}
|
456
|
|
457
|
String targetLine = getSchemaLine(xml);
|
458
|
|
459
|
// the prefix is at the beginning of the doc
|
460
|
String prefix = null;
|
461
|
String regex1 = "^\\s*(\\p{Graph}+):\\p{Graph}* ";
|
462
|
Pattern pattern = Pattern.compile(regex1, Pattern.CASE_INSENSITIVE);
|
463
|
Matcher matcher = pattern.matcher(targetLine);
|
464
|
if (matcher.find()) {
|
465
|
prefix = matcher.group(1).trim();
|
466
|
}
|
467
|
|
468
|
// if a prefix was found, we are looking for xmlns:<prefix>="namespace"
|
469
|
// if no prefix was found, we grab the first namespace.
|
470
|
String regex2;
|
471
|
if (prefix != null) {
|
472
|
regex2 = "xmlns:" + prefix + "=['\"](.*)['\"]";
|
473
|
} else {
|
474
|
regex2 = "xmlns:.*=['\"](.*)['\"]";
|
475
|
}
|
476
|
Pattern pattern2 = Pattern.compile(regex2, Pattern.CASE_INSENSITIVE);
|
477
|
Matcher matcher2 = pattern2.matcher(targetLine);
|
478
|
if (matcher2.find()) {
|
479
|
namespace = matcher2.group(1);
|
480
|
|
481
|
if (namespace.indexOf(eml2_0_0NameSpace) != -1) {
|
482
|
namespace = eml2_0_0NameSpace;
|
483
|
} else if (namespace.indexOf(eml2_0_1NameSpace) != -1) {
|
484
|
namespace = eml2_0_1NameSpace;
|
485
|
} else if (namespace.indexOf(eml2_1_0NameSpace) != -1) {
|
486
|
namespace = eml2_1_0NameSpace;
|
487
|
} else if (namespace.indexOf(eml2_1_1NameSpace) != -1) {
|
488
|
namespace = eml2_1_1NameSpace;
|
489
|
}
|
490
|
}
|
491
|
|
492
|
return namespace;
|
493
|
}
|
494
|
|
495
|
/**
|
496
|
* Return the line from xml that holds the metadata like namespace and
|
497
|
* schema location
|
498
|
*
|
499
|
* @param xml
|
500
|
* the document to parse
|
501
|
* @return the "second" line of the document
|
502
|
*/
|
503
|
private static String getSchemaLine(StringReader xml) throws IOException {
|
504
|
Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
|
505
|
// find the line
|
506
|
String secondLine = null;
|
507
|
int count = 0;
|
508
|
final int TARGETNUM = 1;
|
509
|
StringBuffer buffer = new StringBuffer();
|
510
|
boolean comment = false;
|
511
|
boolean processingInstruction = false;
|
512
|
char thirdPreviousCharacter = '?';
|
513
|
char secondPreviousCharacter = '?';
|
514
|
char previousCharacter = '?';
|
515
|
char currentCharacter = '?';
|
516
|
int tmp = xml.read();
|
517
|
while (tmp != -1) {
|
518
|
currentCharacter = (char)tmp;
|
519
|
//in a comment
|
520
|
if (currentCharacter == '-' && previousCharacter == '-'
|
521
|
&& secondPreviousCharacter == '!'
|
522
|
&& thirdPreviousCharacter == '<') {
|
523
|
comment = true;
|
524
|
}
|
525
|
//out of comment
|
526
|
if (comment && currentCharacter == '>' && previousCharacter == '-'
|
527
|
&& secondPreviousCharacter == '-') {
|
528
|
comment = false;
|
529
|
}
|
530
|
|
531
|
//in a processingInstruction
|
532
|
if (currentCharacter == '?' && previousCharacter == '<') {
|
533
|
processingInstruction = true;
|
534
|
}
|
535
|
|
536
|
//out of processingInstruction
|
537
|
if (processingInstruction && currentCharacter == '>'
|
538
|
&& previousCharacter == '?') {
|
539
|
processingInstruction = false;
|
540
|
}
|
541
|
|
542
|
//this is not comment or a processingInstruction
|
543
|
if (currentCharacter != '!' && previousCharacter == '<'
|
544
|
&& !comment && !processingInstruction) {
|
545
|
count++;
|
546
|
}
|
547
|
|
548
|
// get target line
|
549
|
if (count == TARGETNUM && currentCharacter != '>') {
|
550
|
buffer.append(currentCharacter);
|
551
|
}
|
552
|
if (count == TARGETNUM && currentCharacter == '>') {
|
553
|
break;
|
554
|
}
|
555
|
thirdPreviousCharacter = secondPreviousCharacter;
|
556
|
secondPreviousCharacter = previousCharacter;
|
557
|
previousCharacter = currentCharacter;
|
558
|
tmp = xml.read();
|
559
|
}
|
560
|
secondLine = buffer.toString();
|
561
|
logMetacat.debug("XMLSchemaService.getSchemaLine - the second line string is: " + secondLine);
|
562
|
|
563
|
xml.reset();
|
564
|
return secondLine;
|
565
|
}
|
566
|
|
567
|
/**
|
568
|
* Get a schema file name from the schema uri.
|
569
|
*
|
570
|
* @param uri
|
571
|
* the uri from which to extract the file name
|
572
|
* @return a string holding the file name
|
573
|
*/
|
574
|
public static String getSchemaFileNameFromUri(String uri) {
|
575
|
// get filename from systemId
|
576
|
String filename = uri;
|
577
|
|
578
|
if (filename != null && !(filename.trim()).equals("")) {
|
579
|
int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
|
580
|
if (slash > -1) {
|
581
|
filename = filename.substring(slash + 1);
|
582
|
}
|
583
|
}
|
584
|
|
585
|
return filename;
|
586
|
}
|
587
|
|
588
|
/**
|
589
|
* Get a base url from the schema url. If url=http://www.example.com/example.xsd,
|
590
|
* http://www.example.com/ will be returned.
|
591
|
*
|
592
|
* @param uri
|
593
|
* the uri from which to extract the base url
|
594
|
* @return a string holding the base url. null will be return if it is not url.
|
595
|
*/
|
596
|
public static String getBaseUrlFromSchemaURL(String url)
|
597
|
{
|
598
|
String baseURL = null;
|
599
|
if (url != null && (url.indexOf("http://") != -1 || url.indexOf("https://") !=-1))
|
600
|
{
|
601
|
int slash = url.lastIndexOf('/');
|
602
|
if (slash > -1)
|
603
|
{
|
604
|
baseURL = url.substring(0,slash+1);
|
605
|
}
|
606
|
}
|
607
|
return baseURL;
|
608
|
}
|
609
|
}
|