
net.sf.xolite.utils.SchemaLocation Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xo-lite Show documentation
Show all versions of xo-lite Show documentation
This project provides a lightweight framework to
serialize/deserialize (or marshall/unmarshall) java objects into
XML. The implementation is based on standard SAX (Simple Api for
Xml) but it follows a original approach based on the strong data
encapsulation paradigm of Object Oriented (OO)
programming.
The newest version!
/*-------------------------------------------------------------------------
Copyright 2012 Olivier Berlanger
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.
-------------------------------------------------------------------------*/
package net.sf.xolite.utils;
import java.net.URL;
/**
* Helper to build the the schema location expressed as in the standard 'xsi:schemaLocation' xml attributes.
* Such a string can be set on the SaxXMLEventParser to enable validation.
*
* Caution: This schemaLocation attribute setup works with Sun JRE default parser (Xerces) but is not guaranteed to work
* if you (or a library you're using) have changed the default XML parser implementation.
* In this case, you have to setup a validating parser yourself (following your parser documentation) and pass it to the
* SaxXMLEventParser(XMLReader)
constructor.
*
*
* @author Olivier Berlanger
*/
public class SchemaLocation {
/**
* Get a schemaLocation string for a schema packaged as an application resource.
*
* @param schemaUri
* URI of the schema
* @param referenceClass
* the reference class providing the class loader.
* @param resourcePath
* Path to the resource, it can be absolute (i.e. starting with '/') or relative to the package of this reference
* class.
* @return the schemaLocation string for the specified schema.
*/
public static String getResourceLocation(String schemaUri, Class> referenceClass, String resourcePath) {
return schemaUri + " " + getResourceURL(referenceClass, resourcePath);
}
/**
* Return the URL to a resource.
* This method verifies the existence of the requested resource and throw a IllegalArgumentException if the ressource is not
* found.
*
* @param referenceClass
* the reference class providing the class loader.
* @param resourcePath
* Path to the resource, it can be absolute (i.e. starting with '/') or relative to the package of this reference
* class.
* @return the URL to the specified resource.
* @exception IllegalArgumentException
* if the specified resource is not found.
*/
public static String getResourceURL(Class> referenceClass, String resourcePath) {
URL resourceUrl = referenceClass.getResource(resourcePath);
if (resourceUrl == null) {
String fullPath = resourcePath;
if (!resourcePath.startsWith("/")) {
fullPath = referenceClass.getPackage().getName().replace('.', '/') + "/" + resourcePath;
}
throw new IllegalArgumentException("Cannot find resource in classpath: " + fullPath);
}
return resourceUrl.toString().replace(" ", "%20");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy