01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package javax.print;
19:
20: import java.io.IOException;
21: import java.io.OutputStream;
22: import java.util.ArrayList;
23: import java.util.List;
24: import org.apache.harmony.x.print.PSStreamPrintServiceFactory;
25: import org.apache.harmony.x.print.util.FactoryLocator;
26:
27: public abstract class StreamPrintServiceFactory {
28:
29: // static part of the class
30: private static List listOfSPSFactories = new ArrayList();
31:
32: public static StreamPrintServiceFactory[] lookupStreamPrintServiceFactories(
33: DocFlavor flavor, String outputMimeType) {
34:
35: int l;
36: ArrayList<StreamPrintServiceFactory> selectedSPSFactories = new ArrayList<StreamPrintServiceFactory>();
37: StreamPrintServiceFactory factory;
38: StreamPrintServiceFactory[] a = {};
39:
40: FactoryLocator fl = new FactoryLocator();
41: try {
42: fl.lookupAllFactories();
43: } catch (IOException io) {
44: return selectedSPSFactories.toArray(a);
45: }
46: listOfSPSFactories = fl.getFactoryClasses();
47:
48: // no selection
49: if (flavor == null && outputMimeType == null) {
50: return (StreamPrintServiceFactory[]) listOfSPSFactories
51: .toArray(a);
52: }
53: // make a selection by
54: l = listOfSPSFactories.size();
55: for (int i = 0; i < l; i++) {
56: factory = (StreamPrintServiceFactory) listOfSPSFactories
57: .get(i);
58: if ((outputMimeType == null
59: || outputMimeType.equalsIgnoreCase(factory
60: .getOutputFormat()) || (factory instanceof PSStreamPrintServiceFactory && outputMimeType
61: .equalsIgnoreCase("internal/postscript")))
62: && (flavor == null || isElementOf(flavor, factory
63: .getSupportedDocFlavors()))) {
64:
65: selectedSPSFactories.add(factory);
66: }
67: }
68: return selectedSPSFactories.toArray(a);
69: }
70:
71: /*
72: * auxilary method for element search
73: */
74: private static boolean isElementOf(DocFlavor flavor,
75: DocFlavor[] flavors) {
76: for (int i = 0; i < flavors.length; i++) {
77: if (flavor.equals(flavors[i])) {
78: return true;
79: }
80: }
81: return false;
82: }
83:
84: public StreamPrintServiceFactory() {
85: super ();
86: }
87:
88: public abstract String getOutputFormat();
89:
90: public abstract DocFlavor[] getSupportedDocFlavors();
91:
92: public abstract StreamPrintService getPrintService(OutputStream out);
93:
94: }
|