SchemaManifestParser.java
4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.xmp.schema.service.impl;
import com.adobe.xmp.schema.service.SchemaServiceException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class SchemaManifestParser {
public static final String NS_XMP_SCHEMA = "http://ns.adobe.com/xmp/schema/1.0/";
public static SchemaManifest parse(URI manifestURI) throws SchemaServiceException {
try {
InputSource input = SchemaManifestParser.getInputSource(manifestURI);
DocumentBuilder builder = SchemaManifestParser.getDocumentBuilder();
Document doc = builder.parse(input);
Element element = doc.getDocumentElement();
if (element != null && element.getLocalName().equals("manifest")) {
SchemaManifest manifest = new SchemaManifest();
String basePath = SchemaManifestParser.getBasePath(element);
manifest.includePaths = SchemaManifestParser.iterateIncludePaths(element, basePath);
manifest.schemaFiles = SchemaManifestParser.iterateSchemas(element, basePath);
if (manifest.schemaFiles.size() > 0) {
return manifest;
}
throw new SchemaServiceException("No schemas have been found in the manifest file.");
}
throw new SchemaServiceException("Root element 'xmps:manifest' not found in manifest file.");
}
catch (Exception e) {
throw new SchemaServiceException("Manifest file cannot be parsed: " + e.getMessage());
}
}
private static String getBasePath(Element element) {
Attr basePathAttr = element.getAttributeNode("basePath");
return basePathAttr != null ? basePathAttr.getValue() : "";
}
private static List<String> iterateIncludePaths(Element element, String basePath) {
NodeList entries = element.getElementsByTagNameNS("http://ns.adobe.com/xmp/schema/1.0/", "includePath");
ArrayList<String> result = new ArrayList<String>();
for (int index = 0; index < entries.getLength(); ++index) {
Node entry = entries.item(index);
Node includePathAttr = entry.getAttributes().getNamedItem("path");
result.add(basePath + includePathAttr.getNodeValue());
}
return result;
}
private static Map<String, String> iterateSchemas(Element element, String basePath) {
NodeList entries = element.getElementsByTagNameNS("http://ns.adobe.com/xmp/schema/1.0/", "schema");
HashMap<String, String> result = new HashMap<String, String>();
for (int index = 0; index < entries.getLength(); ++index) {
Node entry = entries.item(index);
NamedNodeMap attrs = entry.getAttributes();
Node attrNS = attrs.getNamedItem("ns");
Node attrURI = attrs.getNamedItem("uri");
if (attrNS == null || attrURI == null) continue;
String ns = attrNS.getNodeValue();
String uriStr = basePath + attrURI.getNodeValue();
result.put(ns, uriStr);
}
return result;
}
private static InputSource getInputSource(URI manifestURI) throws IOException, MalformedURLException, FileNotFoundException {
if (manifestURI != null) {
InputStream stream = manifestURI.toURL().openStream();
return new InputSource(stream);
}
throw new FileNotFoundException("URI is null");
}
private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setIgnoringComments(true);
return factory.newDocumentBuilder();
}
public static class SchemaManifest {
public List<String> includePaths;
public Map<String, String> schemaFiles;
}
}