1 /*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5 // SAXCatalogReader.java - Read XML Catalog files
6
7 /*
8 * Copyright 2001-2004 The Apache Software Foundation or its licensors,
9 * as applicable.
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 */
23
24 package com.sun.org.apache.xml.internal.resolver.readers;
25
26 import com.sun.org.apache.xml.internal.resolver.Catalog;
27 import com.sun.org.apache.xml.internal.resolver.CatalogException;
28 import com.sun.org.apache.xml.internal.resolver.CatalogManager;
29 import com.sun.org.apache.xml.internal.resolver.helpers.Debug;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.net.MalformedURLException;
34 import java.net.URL;
135 * really matter. But it's still a bit of a hack.
136 */
137 protected Debug debug = CatalogManager.getStaticManager().debug;
138
139 /** The constructor */
140 public SAXCatalogReader() {
141 parserFactory = null;
142 parserClass = null;
143 }
144
145 /** The constructor */
146 public SAXCatalogReader(SAXParserFactory parserFactory) {
147 this.parserFactory = parserFactory;
148 }
149
150 /** The constructor */
151 public SAXCatalogReader(String parserClass) {
152 this.parserClass = parserClass;
153 }
154
155 /** Set the SAXCatalogParser class for the given namespace/root
156 * element type.
157 */
158 public void setCatalogParser(String namespaceURI,
159 String rootElement,
160 String parserClass) {
161 if (namespaceURI == null) {
162 namespaceMap.put(rootElement, parserClass);
163 } else {
164 namespaceMap.put("{"+namespaceURI+"}"+rootElement, parserClass);
165 }
166 }
167
168 /** Get the SAXCatalogParser class for the given namespace/root
169 * element type.
170 */
171 public String getCatalogParser(String namespaceURI,
172 String rootElement) {
173 if (namespaceURI == null) {
174 return (String) namespaceMap.get(rootElement);
175 } else {
176 return (String) namespaceMap.get("{"+namespaceURI+"}"+rootElement);
177 }
178 }
179
180 /**
181 * Parse an XML Catalog file.
182 *
183 * @param catalog The catalog to which this catalog file belongs
184 * @param fileUrl The URL or filename of the catalog file to process
185 *
186 * @throws MalformedURLException Improper fileUrl
187 * @throws IOException Error reading catalog file
188 */
189 public void readCatalog(Catalog catalog, String fileUrl)
190 throws MalformedURLException, IOException,
191 CatalogException {
192
193 URL url = null;
194
195 try {
196 url = new URL(fileUrl);
197 } catch (MalformedURLException e) {
198 url = new URL("file:///" + fileUrl);
309 public void startElement (String name,
310 AttributeList atts)
311 throws SAXException {
312
313 if (abandonHope) {
314 return;
315 }
316
317 if (saxParser == null) {
318 String prefix = "";
319 if (name.indexOf(':') > 0) {
320 prefix = name.substring(0, name.indexOf(':'));
321 }
322
323 String localName = name;
324 if (localName.indexOf(':') > 0) {
325 localName = localName.substring(localName.indexOf(':')+1);
326 }
327
328 String namespaceURI = null;
329 if (prefix.equals("")) {
330 namespaceURI = atts.getValue("xmlns");
331 } else {
332 namespaceURI = atts.getValue("xmlns:" + prefix);
333 }
334
335 String saxParserClass = getCatalogParser(namespaceURI,
336 localName);
337
338 if (saxParserClass == null) {
339 abandonHope = true;
340 if (namespaceURI == null) {
341 debug.message(2, "No Catalog parser for " + name);
342 } else {
343 debug.message(2, "No Catalog parser for "
344 + "{" + namespaceURI + "}"
345 + name);
346 }
347 return;
348 }
349
|
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package com.sun.org.apache.xml.internal.resolver.readers;
19
20 import com.sun.org.apache.xml.internal.resolver.Catalog;
21 import com.sun.org.apache.xml.internal.resolver.CatalogException;
22 import com.sun.org.apache.xml.internal.resolver.CatalogManager;
23 import com.sun.org.apache.xml.internal.resolver.helpers.Debug;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.net.MalformedURLException;
28 import java.net.URL;
129 * really matter. But it's still a bit of a hack.
130 */
131 protected Debug debug = CatalogManager.getStaticManager().debug;
132
133 /** The constructor */
134 public SAXCatalogReader() {
135 parserFactory = null;
136 parserClass = null;
137 }
138
139 /** The constructor */
140 public SAXCatalogReader(SAXParserFactory parserFactory) {
141 this.parserFactory = parserFactory;
142 }
143
144 /** The constructor */
145 public SAXCatalogReader(String parserClass) {
146 this.parserClass = parserClass;
147 }
148
149 /**
150 * Set the SAXCatalogParser class for the given namespace/root
151 * element type.
152 */
153 public void setCatalogParser(String namespaceURI,
154 String rootElement,
155 String parserClass) {
156 namespaceURI = namespaceURI != null ? namespaceURI.trim() : "";
157 namespaceMap.put("{"+namespaceURI+"}"+rootElement, parserClass);
158 }
159
160 /**
161 * Get the SAXCatalogParser class for the given namespace/root
162 * element type.
163 */
164 public String getCatalogParser(String namespaceURI,
165 String rootElement) {
166 namespaceURI = namespaceURI != null ? namespaceURI.trim() : "";
167 return (String) namespaceMap.get("{"+namespaceURI+"}"+rootElement);
168 }
169
170 /**
171 * Parse an XML Catalog file.
172 *
173 * @param catalog The catalog to which this catalog file belongs
174 * @param fileUrl The URL or filename of the catalog file to process
175 *
176 * @throws MalformedURLException Improper fileUrl
177 * @throws IOException Error reading catalog file
178 */
179 public void readCatalog(Catalog catalog, String fileUrl)
180 throws MalformedURLException, IOException,
181 CatalogException {
182
183 URL url = null;
184
185 try {
186 url = new URL(fileUrl);
187 } catch (MalformedURLException e) {
188 url = new URL("file:///" + fileUrl);
299 public void startElement (String name,
300 AttributeList atts)
301 throws SAXException {
302
303 if (abandonHope) {
304 return;
305 }
306
307 if (saxParser == null) {
308 String prefix = "";
309 if (name.indexOf(':') > 0) {
310 prefix = name.substring(0, name.indexOf(':'));
311 }
312
313 String localName = name;
314 if (localName.indexOf(':') > 0) {
315 localName = localName.substring(localName.indexOf(':')+1);
316 }
317
318 String namespaceURI = null;
319 if (prefix.length() == 0) {
320 namespaceURI = atts.getValue("xmlns");
321 } else {
322 namespaceURI = atts.getValue("xmlns:" + prefix);
323 }
324
325 String saxParserClass = getCatalogParser(namespaceURI,
326 localName);
327
328 if (saxParserClass == null) {
329 abandonHope = true;
330 if (namespaceURI == null) {
331 debug.message(2, "No Catalog parser for " + name);
332 } else {
333 debug.message(2, "No Catalog parser for "
334 + "{" + namespaceURI + "}"
335 + name);
336 }
337 return;
338 }
339
|