SchemaManifestParser.java 4.5 KB
/*
 * 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;
    }

}