001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.ken.util;
017
018import java.io.Reader;
019import java.io.StringReader;
020
021import javax.xml.XMLConstants;
022import javax.xml.parsers.DocumentBuilder;
023import javax.xml.parsers.DocumentBuilderFactory;
024
025import org.kuali.rice.ken.bo.NotificationContentTypeBo;
026import org.kuali.rice.ken.service.NotificationContentTypeService;
027import org.w3c.dom.DOMImplementation;
028import org.w3c.dom.ls.DOMImplementationLS;
029import org.w3c.dom.ls.LSInput;
030import org.w3c.dom.ls.LSResourceResolver;
031
032/**
033 * Resource resolver for SchemaFactory.  For now used during validation of NotificationRequest content element.
034 * Looks up XSD in NotificationContentType record.
035 * @author Kuali Rice Team (rice.collab@kuali.org)
036 */
037public class ContentTypeLSResourceResolver extends ContentTypeResourceResolver implements LSResourceResolver {
038    /**
039     * Constructs a ContentTypeLSResourceResolver.java.
040     * @param notificationContentTypeService
041     */
042    public ContentTypeLSResourceResolver(NotificationContentTypeService notificationContentTypeService) {
043        super(notificationContentTypeService);
044    }
045
046    /**
047     * @see org.w3c.dom.ls.LSResourceResolver#resolveResource(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
048     */
049    public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
050        if (!type.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
051            return null;
052        }
053
054        if (!systemId.startsWith(CONTENT_TYPE_PREFIX)) {
055            LOG.warn("Cannot resolve non-ContentType resources");
056            return null;
057        }
058
059        NotificationContentTypeBo notificationContentType = resolveContentType(systemId);
060        if (notificationContentType == null) {
061            LOG.error("Unable to resolve system id '" + systemId + "' locally...delegating to default resolution strategy.");
062            return null;
063        }
064
065        Reader reader = new StringReader(notificationContentType.getXsd());
066        try {
067            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
068            DocumentBuilder builder = factory.newDocumentBuilder();
069            DOMImplementation domImpl = builder.getDOMImplementation();
070            DOMImplementationLS dils = (DOMImplementationLS) domImpl;
071            LSInput input = dils.createLSInput();
072            input.setCharacterStream(reader);
073            return input;
074        } catch (Exception e) {
075            throw new RuntimeException(e);
076        }
077    }
078}