1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A Class that stores xml schema information
|
4
|
* Copyright: 2008 Regents of the University of California and the
|
5
|
* National Center for Ecological Analysis and Synthesis
|
6
|
* Authors: Jing Tao
|
7
|
*
|
8
|
* '$Author: leinfelder $'
|
9
|
* '$Date: 2008-09-26 15:43:57 -0700 (Fri, 26 Sep 2008) $'
|
10
|
* '$Revision: 4399 $'
|
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
|
package edu.ucsb.nceas.metacat.service;
|
27
|
|
28
|
import java.io.IOException;
|
29
|
import java.io.InputStream;
|
30
|
import java.util.Vector;
|
31
|
|
32
|
import org.xml.sax.Attributes;
|
33
|
import org.xml.sax.InputSource;
|
34
|
import org.xml.sax.SAXException;
|
35
|
import org.xml.sax.XMLReader;
|
36
|
import org.xml.sax.helpers.DefaultHandler;
|
37
|
import org.xml.sax.helpers.XMLReaderFactory;
|
38
|
|
39
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
40
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
41
|
|
42
|
/**
|
43
|
* A class will parse an schema file to get information - such as included
|
44
|
* xsd files in this schema
|
45
|
* @author tao
|
46
|
*
|
47
|
*/
|
48
|
public class XMLSchemaParser extends DefaultHandler
|
49
|
{
|
50
|
private static String INCLUDE = "include";
|
51
|
private static String SCHEMALOCATION = "schemaLocation";
|
52
|
private Vector<String> includedSchemaFilePaths = new Vector<String>();
|
53
|
private InputStream schemaIn = null;
|
54
|
private XMLReader parser = null;
|
55
|
/**
|
56
|
* Constructor
|
57
|
* @param schemaIn the schema content as an InputStream object
|
58
|
*/
|
59
|
public XMLSchemaParser(InputStream schemaIn) throws SAXException, PropertyNotFoundException
|
60
|
{
|
61
|
this.schemaIn =schemaIn;
|
62
|
initParser();
|
63
|
}
|
64
|
|
65
|
/*
|
66
|
* Initialize sax parser
|
67
|
*/
|
68
|
private void initParser() throws SAXException, PropertyNotFoundException
|
69
|
{
|
70
|
// Get an instance of the parser
|
71
|
String parserName = PropertyService.getProperty("xml.saxparser");
|
72
|
parser = XMLReaderFactory.createXMLReader(parserName);
|
73
|
parser.setContentHandler(this);
|
74
|
|
75
|
}
|
76
|
|
77
|
/**
|
78
|
* Parse the schema file
|
79
|
* @throws SAXException if some sax related exception happens
|
80
|
* @throws IOException if the schema content couldn't be found
|
81
|
*/
|
82
|
public void parse() throws SAXException, IOException
|
83
|
{
|
84
|
parser.parse(new InputSource(schemaIn));
|
85
|
}
|
86
|
|
87
|
/**
|
88
|
* Get the included schema file paths in this schema
|
89
|
* @return the included schema file paths
|
90
|
*/
|
91
|
public Vector<String> getIncludedSchemaFilePathes()
|
92
|
{
|
93
|
return includedSchemaFilePaths;
|
94
|
}
|
95
|
|
96
|
/** SAX Handler that is called at the start of each XML element */
|
97
|
public void startElement(String uri, String localName, String qName,
|
98
|
Attributes atts) throws SAXException
|
99
|
{
|
100
|
if(localName != null && localName.equals(INCLUDE) && atts != null)
|
101
|
{
|
102
|
for (int i = 0; i < atts.getLength(); i++)
|
103
|
{
|
104
|
String attributeName = atts.getQName(i);
|
105
|
if(attributeName != null && attributeName.equals(SCHEMALOCATION))
|
106
|
{
|
107
|
String attributeValue = atts.getValue(i);
|
108
|
if(attributeValue != null || !attributeValue.trim().equals(""))
|
109
|
{
|
110
|
includedSchemaFilePaths.add(attributeValue);
|
111
|
}
|
112
|
}
|
113
|
|
114
|
}
|
115
|
}
|
116
|
}
|
117
|
|
118
|
}
|