001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: ImageWriterRegistry.java 496556 2007-01-16 00:59:48Z cam $ */
019:
020: package org.apache.xmlgraphics.image.writer;
021:
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.ListIterator;
027: import java.util.Map;
028: import java.util.HashMap;
029: import java.util.Properties;
030:
031: import org.apache.xmlgraphics.util.Service;
032:
033: /**
034: * Registry for ImageWriter implementations. They are primarily registered through the "Service
035: * Provider" mechanism.
036: * @see org.apache.xmlgraphics.util.Service
037: */
038: public class ImageWriterRegistry {
039:
040: private static ImageWriterRegistry instance;
041:
042: private Map imageWriterMap = new HashMap();
043: private Map preferredOrder;
044:
045: /**
046: * Default constructor. The default preferred order for the image writers is loaded from the
047: * resources.
048: */
049: public ImageWriterRegistry() {
050: Properties props = new Properties();
051: InputStream in = getClass().getResourceAsStream(
052: "default-preferred-order.properties");
053: if (in != null) {
054: try {
055: try {
056: props.load(in);
057: } finally {
058: in.close();
059: }
060: } catch (IOException ioe) {
061: throw new RuntimeException(
062: "Could not load default preferred order due to I/O error: "
063: + ioe.getMessage());
064: }
065: }
066: this .preferredOrder = props;
067: setup();
068: }
069:
070: /**
071: * Special constructor. The preferred order for the image writers can be specified as a
072: * Map (for example a Properties file). The entries of the Map consists of fully qualified
073: * class or package names as keys and integer numbers as values. Zero (0) is the default
074: * priority.
075: */
076: public ImageWriterRegistry(Properties preferredOrder) {
077: this .preferredOrder = preferredOrder;
078: setup();
079: }
080:
081: /** @return a singleton instance of the ImageWriterRegistry. */
082: public static ImageWriterRegistry getInstance() {
083: if (instance == null) {
084: instance = new ImageWriterRegistry();
085: }
086: return instance;
087: }
088:
089: private void setup() {
090: Iterator iter = Service.providers(ImageWriter.class);
091: while (iter.hasNext()) {
092: ImageWriter writer = (ImageWriter) iter.next();
093: register(writer);
094: }
095: }
096:
097: private int getPriority(ImageWriter writer) {
098: String key = writer.getClass().getName();
099: Object value = preferredOrder.get(key);
100: while (value == null) {
101: int pos = key.lastIndexOf(".");
102: if (pos < 0) {
103: break;
104: }
105: key = key.substring(0, pos);
106: value = preferredOrder.get(key);
107: }
108: return (value != null) ? Integer.parseInt(value.toString()) : 0;
109: }
110:
111: /**
112: * Registers a new ImageWriter implementation in the registry. If an ImageWriter for the same
113: * target MIME type has already been registered, it is overwritten with the new one.
114: * @param writer the ImageWriter instance to register.
115: */
116: public void register(ImageWriter writer) {
117: List entries = (List) imageWriterMap.get(writer.getMIMEType());
118: if (entries == null) {
119: entries = new java.util.ArrayList();
120: imageWriterMap.put(writer.getMIMEType(), entries);
121: }
122:
123: int priority = getPriority(writer);
124: ListIterator li;
125: li = entries.listIterator();
126: while (li.hasNext()) {
127: ImageWriter w = (ImageWriter) li.next();
128: if (getPriority(w) < priority) {
129: li.previous();
130: li.add(writer);
131: return;
132: }
133: }
134: li.add(writer);
135: }
136:
137: /**
138: * Returns an ImageWriter that can be used to encode an image to the requested MIME type.
139: * @param mime the MIME type of the desired output format
140: * @return an ImageWriter instance handling the desired output format or null if none can be
141: * found.
142: */
143: public ImageWriter getWriterFor(String mime) {
144: List entries = (List) imageWriterMap.get(mime);
145: if (entries == null) {
146: return null;
147: }
148: Iterator iter = entries.iterator();
149: while (iter.hasNext()) {
150: ImageWriter writer = (ImageWriter) iter.next();
151: if (writer.isFunctional()) {
152: return writer;
153: }
154: }
155: return null;
156: }
157:
158: }
|