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.resource;
19:
20: import java.io.IOException;
21: import java.io.InputStream;
22: import java.util.Stack;
23:
24: import org.xml.sax.InputSource;
25:
26: public class ExtendedURIResolver {
27:
28: private org.apache.cxf.resource.URIResolver currentResolver;
29: private String lastestImportUri;
30: private Stack<InputStream> resourceOpened = new Stack<InputStream>();
31:
32: public ExtendedURIResolver() {
33: currentResolver = new org.apache.cxf.resource.URIResolver();
34: }
35:
36: public InputSource resolve(String curUri, String baseUri) {
37: try {
38: lastestImportUri = curUri;
39: currentResolver.resolve(baseUri, curUri, getClass());
40: if (currentResolver.isResolved()) {
41: if (currentResolver.getURI() != null
42: && currentResolver.getURI().isAbsolute()) {
43: // When importing a relative file,
44: // setSystemId with an absolute path so the
45: // resolver finds any files which that file
46: // imports with locations relative to it.
47: curUri = currentResolver.getURI().toString();
48: }
49: if (currentResolver.isFile()) {
50: curUri = currentResolver.getFile()
51: .getAbsoluteFile().toURI().toString();
52: }
53: InputStream in = currentResolver.getInputStream();
54: resourceOpened.addElement(in);
55: InputSource source = new InputSource(in);
56: source.setSystemId(curUri);
57: source.setPublicId(lastestImportUri);
58: return source;
59: }
60: } catch (IOException e) {
61: // move on...
62: }
63: return null;
64: // return new InputSource(schemaLocation);
65: }
66:
67: public void close() {
68: try {
69: while (!resourceOpened.isEmpty()) {
70: InputStream in = resourceOpened.pop();
71: in.close();
72: }
73: } catch (IOException ioe) {
74: // move on...
75: }
76: }
77:
78: public String getLatestImportURI() {
79: return this .getURI();
80: }
81:
82: public String getURI() {
83: if (currentResolver.getURI() != null) {
84: return currentResolver.getURI().toString();
85: } else {
86: return lastestImportUri;
87: }
88: }
89:
90: }
|