01: /*
02: * Copyright 2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.xml.sax;
18:
19: import java.io.IOException;
20:
21: import org.springframework.core.io.Resource;
22: import org.xml.sax.InputSource;
23:
24: /**
25: * Convenient utility methods for dealing with SAX.
26: *
27: * @author Arjen Poutsma
28: * @since 1.0.0
29: */
30: public abstract class SaxUtils {
31:
32: /**
33: * Creates a SAX <code>InputSource</code> from the given resource. Sets the system identifier to the resource's
34: * <code>URL</code>, if available.
35: *
36: * @param resource the resource
37: * @return the input source created from the resource
38: * @throws IOException if an I/O exception occurs
39: * @see InputSource#setSystemId(String)
40: * @see #getSystemId(org.springframework.core.io.Resource)
41: */
42: public static InputSource createInputSource(Resource resource)
43: throws IOException {
44: InputSource inputSource = new InputSource(resource
45: .getInputStream());
46: inputSource.setSystemId(getSystemId(resource));
47: return inputSource;
48: }
49:
50: /** Retrieves the URL from the given resource as System ID. Returns <code>null</code> if it cannot be openened. */
51: public static String getSystemId(Resource resource) {
52: try {
53: return resource.getURL().toString();
54: } catch (IOException e) {
55: return null;
56: }
57: }
58:
59: }
|