001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-impl/api-impl/src/java/org/sakaiproject/metaobj/utils/xml/SchemaFactory.java $
003: * $Id: SchemaFactory.java 14225 2006-09-05 17:39:44Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.metaobj.utils.xml;
021:
022: import java.io.File;
023: import java.net.URL;
024: import java.util.Hashtable;
025:
026: import org.jdom.Document;
027: import org.jdom.input.SAXBuilder;
028: import org.sakaiproject.metaobj.utils.xml.impl.SchemaNodeImpl;
029:
030: public class SchemaFactory {
031: static private SchemaFactory schemaFactory = new SchemaFactory();
032:
033: private Hashtable schemas = new Hashtable();
034:
035: private SchemaFactory() {
036: }
037:
038: public static SchemaFactory getInstance() {
039: return schemaFactory;
040: }
041:
042: public SchemaNode getSchema(java.io.File in) {
043: if (schemas.get(in) != null) {
044: return (SchemaNode) schemas.get(in);
045: }
046:
047: SAXBuilder builder = new SAXBuilder();
048: try {
049: Document doc = builder.build(in);
050: SchemaNode node = new SchemaNodeImpl(doc, this , in);
051: schemas.put(in, node);
052: return node;
053: } catch (Exception e) {
054: throw new SchemaInvalidException(e);
055: }
056: }
057:
058: public SchemaNode getSchema(java.io.InputStream in) {
059:
060: SAXBuilder builder = new SAXBuilder();
061: try {
062: Document doc = builder.build(in);
063: SchemaNode node = new SchemaNodeImpl(doc, this , in);
064: return node;
065: } catch (Exception e) {
066: throw new SchemaInvalidException(e);
067: }
068: }
069:
070: public SchemaNode getSchema(java.io.InputStream in,
071: java.lang.String systemId) {
072: if (schemas.get(systemId) != null) {
073: return (SchemaNode) schemas.get(systemId);
074: }
075:
076: SAXBuilder builder = new SAXBuilder();
077: try {
078: Document doc = builder.build(in, systemId);
079: SchemaNode node = new SchemaNodeImpl(doc, this , in);
080: schemas.put(systemId, node);
081: return node;
082: } catch (Exception e) {
083: throw new SchemaInvalidException(e);
084: }
085: }
086:
087: public SchemaNode getSchema(java.io.Reader characterStream,
088: java.lang.String systemId) {
089: if (schemas.get(systemId) != null) {
090: return (SchemaNode) schemas.get(systemId);
091: }
092:
093: SAXBuilder builder = new SAXBuilder();
094: try {
095: Document doc = builder.build(characterStream, systemId);
096: SchemaNode node = new SchemaNodeImpl(doc, this , systemId);
097: schemas.put(systemId, node);
098: return node;
099: } catch (Exception e) {
100: throw new SchemaInvalidException(e);
101: }
102: }
103:
104: public SchemaNode getSchema(java.lang.String systemId) {
105: if (schemas.get(systemId) != null) {
106: return (SchemaNode) schemas.get(systemId);
107: }
108:
109: SAXBuilder builder = new SAXBuilder();
110: try {
111: Document doc = builder.build(systemId);
112: SchemaNode node = new SchemaNodeImpl(doc, this , systemId);
113: schemas.put(systemId, node);
114: return node;
115: } catch (Exception e) {
116: throw new SchemaInvalidException(e);
117: }
118: }
119:
120: public SchemaNode getSchema(java.net.URL url) {
121: if (schemas.get(url) != null) {
122: return (SchemaNode) schemas.get(url);
123: }
124:
125: SAXBuilder builder = new SAXBuilder();
126: try {
127: Document doc = builder.build(url);
128: SchemaNode node = new SchemaNodeImpl(doc, this , url);
129: schemas.put(url, node);
130: return node;
131: } catch (Exception e) {
132: throw new SchemaInvalidException(e);
133: }
134: }
135:
136: public SchemaNode getRelativeSchema(String schemaLocation,
137: Object path) {
138: try {
139: if (path instanceof URL) {
140: URL urlPath = (URL) path;
141: URL schemaUrl = new URL(urlPath, schemaLocation);
142: return getSchema(schemaUrl);
143: } else if (path instanceof File) {
144: File filePath = (File) path;
145: File schemaFile = new File(filePath.getParentFile(),
146: schemaLocation);
147: return getSchema(schemaFile);
148: } else {
149: URL urlPath = new URL(path.toString());
150: URL schemaUrl = new URL(urlPath, schemaLocation);
151: return getSchema(schemaUrl);
152: }
153: } catch (Exception exp) {
154: throw new SchemaInvalidException(exp);
155: }
156: }
157:
158: public void reload() {
159: schemas = new Hashtable();
160: }
161:
162: }
|