01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */package org.apache.cxf.catalog;
19:
20: import java.io.IOException;
21:
22: import org.xml.sax.InputSource;
23:
24: import org.apache.cxf.resource.ExtendedURIResolver;
25: import org.apache.ws.commons.schema.XmlSchemaException;
26: import org.apache.ws.commons.schema.resolver.URIResolver;
27: import org.apache.xml.resolver.Catalog;
28:
29: /**
30: * Resolves URIs using Apache Commons Resolver API.
31: */
32: public class CatalogXmlSchemaURIResolver implements URIResolver {
33:
34: private ExtendedURIResolver resolver;
35: private Catalog catalogResolver;
36:
37: public CatalogXmlSchemaURIResolver(
38: OASISCatalogManager catalogManager) {
39: this .resolver = new ExtendedURIResolver();
40: this .catalogResolver = catalogManager.getCatalog();
41: }
42:
43: public InputSource resolveEntity(String targetNamespace,
44: String schemaLocation, String baseUri) {
45: String resolvedSchemaLocation = null;
46: try {
47: resolvedSchemaLocation = this .catalogResolver
48: .resolveSystem(schemaLocation);
49: } catch (IOException e) {
50: throw new RuntimeException("Catalog resolution failed", e);
51: }
52:
53: InputSource in = null;
54: if (resolvedSchemaLocation == null) {
55: in = this .resolver.resolve(schemaLocation, baseUri);
56: } else {
57: in = this .resolver.resolve(resolvedSchemaLocation, baseUri);
58: }
59:
60: // XXX: If we return null, a NPE is raised in SchemaBuilder.
61: // If we return new InputSource(), a XmlSchemaException is raised
62: // but without any nice error message. So let's just throw a nice error here.
63: if (in == null) {
64: throw new XmlSchemaException(
65: "Unable to locate imported document "
66: + "at '"
67: + schemaLocation
68: + "'"
69: + (baseUri == null ? "."
70: : ", relative to '" + baseUri
71: + "'."));
72: }
73:
74: return in;
75: }
76: }
|