01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: /* $Id: XMPParser.java 426584 2006-07-28 16:01:47Z jeremias $ */
19:
20: package org.apache.xmlgraphics.xmp;
21:
22: import java.net.URL;
23:
24: import javax.xml.transform.Source;
25: import javax.xml.transform.Transformer;
26: import javax.xml.transform.TransformerException;
27: import javax.xml.transform.TransformerFactory;
28: import javax.xml.transform.sax.SAXResult;
29: import javax.xml.transform.stream.StreamSource;
30:
31: /**
32: * The XMP parser.
33: */
34: public class XMPParser {
35:
36: /**
37: * Parses an XMP file.
38: * @param url the URL to load the file from
39: * @return the parsed Metadata object
40: * @throws TransformerException if an error occurs while parsing the file
41: */
42: public static Metadata parseXMP(URL url)
43: throws TransformerException {
44: return parseXMP(new StreamSource(url.toExternalForm()));
45: }
46:
47: /**
48: * Parses an XMP file.
49: * @param src a JAXP Source object where the XMP file can be loaded from
50: * @return the parsed Metadata object
51: * @throws TransformerException if an error occurs while parsing the file
52: */
53: public static Metadata parseXMP(Source src)
54: throws TransformerException {
55: TransformerFactory tFactory = TransformerFactory.newInstance();
56: Transformer transformer = tFactory.newTransformer();
57: XMPHandler handler = createXMPHandler();
58: SAXResult res = new SAXResult(handler);
59: transformer.transform(src, res);
60: return handler.getMetadata();
61: }
62:
63: /**
64: * Creates and returns an XMPHandler for passive XMP parsing.
65: * @return the requested XMPHandler
66: */
67: public static XMPHandler createXMPHandler() {
68: return new XMPHandler();
69: }
70:
71: }
|