All Downloads are FREE. Search and download functionalities are using the official Maven repository.

hu.icellmobilsoft.coffee.rest.validation.catalog.PublicCatalogResolver Maven / Gradle / Ivy

There is a newer version: 2.9.0
Show newest version
/*-
 * #%L
 * Coffee
 * %%
 * Copyright (C) 2020 i-Cell Mobilsoft Zrt.
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package hu.icellmobilsoft.coffee.rest.validation.catalog;

import java.io.StringReader;
import java.util.Iterator;

import javax.xml.catalog.Catalog;

import jakarta.enterprise.context.Dependent;
import jakarta.inject.Inject;

import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSResourceResolver;
import org.xml.sax.InputSource;

import hu.icellmobilsoft.coffee.cdi.logger.AppLogger;
import hu.icellmobilsoft.coffee.cdi.logger.ThisLogger;
import hu.icellmobilsoft.coffee.rest.validation.xml.utils.IXsdResourceResolver;

/**
 * Implements CatalogResolver. 
* This class implements a SAX EntityResolver, StAX XMLResolver, Schema Validation LSResourceResolver and Transform URIResolver. * See {@link javax.xml.catalog.Util} {@link javax.xml.catalog.CatalogResolverImpl} * * @author imre.scheffer * @author mark.petrenyi * @since 1.0.0 */ @Dependent public class PublicCatalogResolver implements LSResourceResolver, IXsdResourceResolver { @Inject private Catalog catalog; @Inject @ThisLogger private AppLogger log; /** * Default constructor, constructs a new object. */ public PublicCatalogResolver() { super(); } /** * Allow the application to resolve external resources. * * @param publicId * The public identifier of the external entity being referenced, or null if no public identifier was supplied or if the * resource is not an entity. * @param systemId * The system identifier, a URI reference [IETF RFC 2396], of the external resource * being referenced, or null if no system identifier was supplied. * @return A InputSource object describing the new input source, or null to request that the parser open a regular URI * connection to the resource. */ public InputSource resolveEntity(String publicId, String systemId) { String resolvedSystemId = resolve(catalog, publicId, systemId); if (resolvedSystemId != null) { return new InputSource(resolvedSystemId); } // no action, allow the parser to continue return new InputSource(new StringReader("")); } private String resolve(Catalog catalog, String publicId, String systemId) { log.debug("Try resolve catalog with publicId:[{0}], systemId:[{1}]", publicId, systemId); String resolvedSystemId = null; if (systemId != null) { /* * If a system identifier is specified, it is used no matter how prefer is set. */ resolvedSystemId = catalog.matchSystem(systemId); } if (resolvedSystemId == null && publicId != null) { resolvedSystemId = catalog.matchPublic(publicId); } if (resolvedSystemId == null && systemId != null) { resolvedSystemId = catalog.matchURI(systemId); } // search alternative catalogs if (resolvedSystemId == null) { Iterator iter = catalog.catalogs().iterator(); while (iter.hasNext()) { resolvedSystemId = resolve(iter.next(), publicId, systemId); if (resolvedSystemId != null) { break; } } } // We retrieve the XSD defined by the catalog from the classpath. if (resolvedSystemId != null) { log.debug("Found resource with catalog resolvedSystemId:[{0}]", resolvedSystemId); } else { log.warn("Resource in catalog with publicId:[{0}], systemId:[{1}] not found!", publicId, systemId); } return resolvedSystemId; } /** {@inheritDoc} */ @Override public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) { InputSource is = resolveEntity(publicId != null ? publicId : namespaceURI, systemId); if (is != null && !is.isEmpty()) { return new CatalogLsInputImpl(is.getSystemId()); } // no action, allow the parser to continue return null; } /** * {@inheritDoc} * * It saves the provided XSD file path parameter into an internal variable (if necessary) */ @Override public void setXsdDirPath(String xsdDirPath) { // For the catalog, this is not needed } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy