blob: f1d31db31b709e0c64baf5378cd36aec30744325 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.lenya.modules.svg;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import org.apache.avalon.framework.activity.Startable;
import org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.parameters.ParameterException;
import org.apache.avalon.framework.parameters.Parameterizable;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.Serviceable;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.batik.util.ParsedURL;
import org.apache.batik.util.ParsedURLData;
import org.apache.batik.util.ParsedURLProtocolHandler;
import org.apache.cocoon.CascadingIOException;
import org.apache.excalibur.source.Source;
import org.apache.excalibur.source.SourceResolver;
/**
* Batik URL protocol handler for protocols which are handled by the SourceResolver.
*/
public class ProtocolHandler extends AbstractLogEnabled implements ParsedURLProtocolHandler,
ThreadSafe, Startable, Component, Serviceable, Parameterizable {
protected static final String PARAM_PROTOCOL = "protocol";
public static final String ROLE = ProtocolHandler.class.getName();
private SourceResolver resolver;
private ServiceManager manager;
private String protocol;
public ParsedURLData parseURL(String urlStr) {
if (this.resolver == null) {
throw new IllegalStateException("Please call setResolver() first!");
}
return new ParsedUrlData(getProtocolHandled(), this.resolver, urlStr);
}
public ParsedURLData parseURL(ParsedURL basepurl, String urlStr) {
return parseURL(urlStr);
}
static class ParsedUrlData extends ParsedURLData {
private Source source;
private SourceResolver resolver;
private String url;
public ParsedUrlData(String protocol, SourceResolver resolver, String url) {
this.url = url;
this.protocol = protocol;
this.resolver = resolver;
try {
this.source = resolver.resolveURI(url);
this.contentType = this.source.getMimeType();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Open a stream for the data. If the thread-local
* <code>SourceResolver</code> exists, then use it, otherwise fall
* back to <code>SourceHandler</code>.
*/
protected InputStream openStreamInternal(String userAgent, Iterator mimeTypes,
Iterator encodingTypes) throws IOException {
try {
return this.source.getInputStream();
} catch (Exception e) {
throw new CascadingIOException("Cannot open URL " + this.url, e);
} finally {
this.resolver.release(this.source);
}
}
}
public void start() throws Exception {
this.resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
ParsedURL.registerHandler(this);
}
public void stop() throws Exception {
}
public void service(ServiceManager manager) throws ServiceException {
this.manager = manager;
}
public String getProtocolHandled() {
return this.protocol;
}
public void parameterize(Parameters params) throws ParameterException {
this.protocol = params.getParameter(PARAM_PROTOCOL);
}
}