| 1 | // SECTION-START[License Header] |
| 2 | // <editor-fold defaultstate="collapsed" desc=" Generated License "> |
| 3 | /* |
| 4 | * Copyright (c) 2010 The JOMC Project |
| 5 | * Copyright (c) 2005 Christian Schulte <schulte2005@users.sourceforge.net> |
| 6 | * All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * |
| 12 | * o Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * |
| 15 | * o Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in |
| 17 | * the documentation and/or other materials provided with the |
| 18 | * distribution. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS" |
| 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 22 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 23 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR |
| 24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 27 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 28 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 30 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | * $Id: JaxpResourceResolverFactory.java 2234 2010-06-29 00:03:38Z schulte2005 $ |
| 33 | * |
| 34 | */ |
| 35 | // </editor-fold> |
| 36 | // SECTION-END |
| 37 | package org.jomc.sdk.model.support; |
| 38 | |
| 39 | import org.w3c.dom.ls.LSResourceResolver; |
| 40 | import java.io.IOException; |
| 41 | import java.io.InputStream; |
| 42 | import java.io.Reader; |
| 43 | import java.net.URL; |
| 44 | import org.jomc.sdk.model.SchemaType; |
| 45 | import org.jomc.sdk.model.SchemasType; |
| 46 | import org.w3c.dom.ls.LSInput; |
| 47 | import static org.jomc.sdk.model.modlet.SdkModelProvider.XML_SCHEMA_JAVA_CLASSPATH_ID_ATTRIBUTE; |
| 48 | |
| 49 | // SECTION-START[Documentation] |
| 50 | // <editor-fold defaultstate="collapsed" desc=" Generated Documentation "> |
| 51 | /** |
| 52 | * XML Schema Set JAXP 'LSResourceResolver' factory implementation. |
| 53 | * <p><b>Specifications</b><ul> |
| 54 | * <li>{@code 'org.w3c.dom.ls.LSResourceResolver'} {@code (org.w3c.dom.ls.LSResourceResolver)} {@code Multiton}</li> |
| 55 | * </ul></p> |
| 56 | * <p><b>Properties</b><ul> |
| 57 | * <li>"{@link #getSchemas schemas}" |
| 58 | * <blockquote>Property of type {@code org.jomc.sdk.model.SchemasType}. |
| 59 | * <p>List of XML schemas ('schemas' element from XML namespace 'http://jomc.org/sdk/model).</p> |
| 60 | * </blockquote></li> |
| 61 | * </ul></p> |
| 62 | * |
| 63 | * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0 |
| 64 | * @version $Id: JaxpResourceResolverFactory.java 2234 2010-06-29 00:03:38Z schulte2005 $ |
| 65 | */ |
| 66 | // </editor-fold> |
| 67 | // SECTION-END |
| 68 | // SECTION-START[Annotations] |
| 69 | // <editor-fold defaultstate="collapsed" desc=" Generated Annotations "> |
| 70 | @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" ) |
| 71 | // </editor-fold> |
| 72 | // SECTION-END |
| 73 | public final class JaxpResourceResolverFactory implements LSResourceResolver |
| 74 | { |
| 75 | // SECTION-START[LSResourceResolver] |
| 76 | |
| 77 | public LSInput resolveResource( final String type, final String namespaceURI, final String publicId, |
| 78 | final String systemId, final String baseURI ) |
| 79 | { |
| 80 | LSInput input = null; |
| 81 | final SchemasType schemas = this.getSchemas(); |
| 82 | |
| 83 | if ( schemas.getLanguageId().equals( type ) ) |
| 84 | { |
| 85 | SchemaType schema = null; |
| 86 | |
| 87 | if ( namespaceURI != null ) |
| 88 | { |
| 89 | schema = schemas.getSchemaByPublicId( namespaceURI ); |
| 90 | } |
| 91 | if ( schema == null && publicId != null ) |
| 92 | { |
| 93 | schema = schemas.getSchemaByPublicId( publicId ); |
| 94 | } |
| 95 | if ( schema == null && systemId != null ) |
| 96 | { |
| 97 | schema = schemas.getSchemaBySystemId( systemId ); |
| 98 | } |
| 99 | |
| 100 | if ( schema != null ) |
| 101 | { |
| 102 | input = new SchemaTypeInput( schema ); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return input; |
| 107 | } |
| 108 | |
| 109 | // SECTION-END |
| 110 | // SECTION-START[JaxpResourceResolverFactory] |
| 111 | // SECTION-END |
| 112 | // SECTION-START[Constructors] |
| 113 | // <editor-fold defaultstate="collapsed" desc=" Generated Constructors "> |
| 114 | |
| 115 | /** Creates a new {@code JaxpResourceResolverFactory} instance. */ |
| 116 | @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" ) |
| 117 | public JaxpResourceResolverFactory() |
| 118 | { |
| 119 | // SECTION-START[Default Constructor] |
| 120 | super(); |
| 121 | // SECTION-END |
| 122 | } |
| 123 | // </editor-fold> |
| 124 | // SECTION-END |
| 125 | // SECTION-START[Dependencies] |
| 126 | // SECTION-END |
| 127 | // SECTION-START[Properties] |
| 128 | // <editor-fold defaultstate="collapsed" desc=" Generated Properties "> |
| 129 | |
| 130 | /** |
| 131 | * Gets the value of the {@code schemas} property. |
| 132 | * @return List of XML schemas ('schemas' element from XML namespace 'http://jomc.org/sdk/model). |
| 133 | * @throws org.jomc.ObjectManagementException if getting the property instance fails. |
| 134 | */ |
| 135 | @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" ) |
| 136 | private org.jomc.sdk.model.SchemasType getSchemas() |
| 137 | { |
| 138 | final org.jomc.sdk.model.SchemasType _p = (org.jomc.sdk.model.SchemasType) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "schemas" ); |
| 139 | assert _p != null : "'schemas' property not found."; |
| 140 | return _p; |
| 141 | } |
| 142 | // </editor-fold> |
| 143 | // SECTION-END |
| 144 | // SECTION-START[Messages] |
| 145 | // SECTION-END |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * {@code LSInput} implementation backed by {@code SchemaType} instance. |
| 150 | * |
| 151 | * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0 |
| 152 | * @version $Id: JaxpResourceResolverFactory.java 2234 2010-06-29 00:03:38Z schulte2005 $ |
| 153 | */ |
| 154 | class SchemaTypeInput implements LSInput |
| 155 | { |
| 156 | |
| 157 | /** The schema backing the input. */ |
| 158 | private final SchemaType schemaType; |
| 159 | |
| 160 | /** |
| 161 | * Creates a new {@code SchemaTypeInput} instance taking a {@code SchemaType} instance. |
| 162 | * |
| 163 | * @param schemaType The schema backing the input. |
| 164 | */ |
| 165 | SchemaTypeInput( final SchemaType schemaType ) |
| 166 | { |
| 167 | this.schemaType = schemaType; |
| 168 | } |
| 169 | |
| 170 | public Reader getCharacterStream() |
| 171 | { |
| 172 | return null; |
| 173 | } |
| 174 | |
| 175 | public void setCharacterStream( final Reader characterStream ) |
| 176 | { |
| 177 | } |
| 178 | |
| 179 | public InputStream getByteStream() |
| 180 | { |
| 181 | try |
| 182 | { |
| 183 | final URL javaResource = this.getJavaResource(); |
| 184 | if ( javaResource != null ) |
| 185 | { |
| 186 | return javaResource.openStream(); |
| 187 | } |
| 188 | |
| 189 | return null; |
| 190 | } |
| 191 | catch ( final IOException e ) |
| 192 | { |
| 193 | return null; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | public void setByteStream( final InputStream byteStream ) |
| 198 | { |
| 199 | } |
| 200 | |
| 201 | public String getStringData() |
| 202 | { |
| 203 | return null; |
| 204 | } |
| 205 | |
| 206 | public void setStringData( final String stringData ) |
| 207 | { |
| 208 | } |
| 209 | |
| 210 | public String getSystemId() |
| 211 | { |
| 212 | final URL javaResource = this.getJavaResource(); |
| 213 | return javaResource != null ? javaResource.toExternalForm() : this.schemaType.getSystemId(); |
| 214 | } |
| 215 | |
| 216 | public void setSystemId( final String systemId ) |
| 217 | { |
| 218 | } |
| 219 | |
| 220 | public String getPublicId() |
| 221 | { |
| 222 | return this.schemaType.getPublicId(); |
| 223 | } |
| 224 | |
| 225 | public void setPublicId( final String publicId ) |
| 226 | { |
| 227 | } |
| 228 | |
| 229 | public String getBaseURI() |
| 230 | { |
| 231 | return null; |
| 232 | } |
| 233 | |
| 234 | public void setBaseURI( final String baseURI ) |
| 235 | { |
| 236 | } |
| 237 | |
| 238 | public String getEncoding() |
| 239 | { |
| 240 | return null; |
| 241 | } |
| 242 | |
| 243 | public void setEncoding( final String encoding ) |
| 244 | { |
| 245 | } |
| 246 | |
| 247 | public boolean getCertifiedText() |
| 248 | { |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | public void setCertifiedText( final boolean certifiedText ) |
| 253 | { |
| 254 | } |
| 255 | |
| 256 | private URL getJavaResource() |
| 257 | { |
| 258 | if ( this.schemaType.getOtherAttributes().containsKey( XML_SCHEMA_JAVA_CLASSPATH_ID_ATTRIBUTE ) ) |
| 259 | { |
| 260 | String absoluteLocation = |
| 261 | this.schemaType.getOtherAttributes().get( XML_SCHEMA_JAVA_CLASSPATH_ID_ATTRIBUTE ); |
| 262 | |
| 263 | if ( !absoluteLocation.startsWith( "/" ) ) |
| 264 | { |
| 265 | absoluteLocation = '/' + absoluteLocation; |
| 266 | } |
| 267 | |
| 268 | return this.getClass().getResource( absoluteLocation ); |
| 269 | } |
| 270 | |
| 271 | return null; |
| 272 | } |
| 273 | |
| 274 | } |