001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.deployment.resolver;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023: import org.apache.ws.commons.schema.resolver.DefaultURIResolver;
024: import org.xml.sax.InputSource;
025:
026: import java.io.ByteArrayInputStream;
027: import java.io.ByteArrayOutputStream;
028: import java.io.File;
029: import java.io.FileInputStream;
030: import java.io.IOException;
031: import java.net.URI;
032: import java.util.zip.ZipEntry;
033: import java.util.zip.ZipInputStream;
034:
035: /**
036: * A custom URI resolver that can
037: */
038: public class AARFileBasedURIResolver extends DefaultURIResolver {
039:
040: protected static final Log log = LogFactory
041: .getLog(AARFileBasedURIResolver.class);
042:
043: private File aarFile;
044: private URI lastImportLocation;
045:
046: public AARFileBasedURIResolver(File aarFile) {
047: this .aarFile = aarFile;
048: }
049:
050: public InputSource resolveEntity(String targetNamespace,
051: String schemaLocation, String baseUri) {
052: //no issue with
053: if (isAbsolute(schemaLocation)) {
054: return super .resolveEntity(targetNamespace, schemaLocation,
055: baseUri);
056: } else {
057: //validate
058: if ((baseUri == null || "".equals(baseUri))
059: && schemaLocation.startsWith("..")) {
060: throw new RuntimeException(
061: "Unsupported schema location " + schemaLocation);
062: }
063:
064: lastImportLocation = URI.create(baseUri).resolve(
065: schemaLocation);
066: ZipInputStream zin = null;
067: try {
068: zin = new ZipInputStream(new FileInputStream(aarFile));
069:
070: ZipEntry entry;
071: byte[] buf = new byte[1024];
072: int read;
073: ByteArrayOutputStream out;
074: String searchingStr = lastImportLocation.toString();
075: while ((entry = zin.getNextEntry()) != null) {
076: String entryName = entry.getName().toLowerCase();
077: if (entryName.equalsIgnoreCase(searchingStr)) {
078: out = new ByteArrayOutputStream();
079: while ((read = zin.read(buf)) > 0) {
080: out.write(buf, 0, read);
081: }
082: ByteArrayInputStream in = new ByteArrayInputStream(
083: out.toByteArray());
084: return new InputSource(in);
085: }
086: }
087:
088: } catch (IOException e) {
089: throw new RuntimeException(e);
090: } finally {
091: try {
092: if (zin != null) {
093: zin.close();
094: }
095: } catch (IOException e) {
096: log.debug(e);
097: }
098: }
099:
100: }
101:
102: log.info("AARFileBasedURIResolver: Unable to resolve"
103: + lastImportLocation);
104: return null;
105: }
106: }
|