3ea135bb |
1 | /**\r |
2 | * Copyright (C) 2003 <a href="http://www.lohndirekt.de/">lohndirekt.de</a>\r |
3 | *\r |
4 | * This library is free software; you can redistribute it and/or\r |
5 | * modify it under the terms of the GNU Lesser General Public\r |
6 | * License as published by the Free Software Foundation; either\r |
7 | * version 2.1 of the License, or (at your option) any later version.\r |
8 | * \r |
9 | * This library is distributed in the hope that it will be useful,\r |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of\r |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r |
12 | * Lesser General Public License for more details.\r |
13 | * \r |
14 | * You should have received a copy of the GNU Lesser General Public\r |
15 | * License along with this library; if not, write to the Free Software\r |
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r |
17 | * \r |
18 | */\r |
19 | package de.lohndirekt.print.attribute;\r |
20 | \r |
21 | import java.io.IOException;\r |
22 | import java.util.Locale;\r |
23 | import java.util.logging.Level;\r |
24 | import java.util.logging.Logger;\r |
25 | \r |
26 | import javax.print.Doc;\r |
27 | import javax.print.attribute.Attribute;\r |
28 | import javax.print.attribute.AttributeSet;\r |
29 | import javax.print.attribute.HashAttributeSet;\r |
30 | import javax.print.attribute.HashPrintJobAttributeSet;\r |
31 | import javax.print.attribute.PrintJobAttribute;\r |
32 | import javax.print.attribute.PrintJobAttributeSet;\r |
33 | import javax.print.attribute.PrintRequestAttributeSet;\r |
34 | import javax.print.attribute.standard.DocumentName;\r |
35 | \r |
36 | import de.lohndirekt.print.SimpleMultiDoc;\r |
37 | import de.lohndirekt.print.attribute.ipp.DocumentFormat;\r |
38 | import de.lohndirekt.print.attribute.ipp.LastDocument;\r |
39 | \r |
40 | /**\r |
41 | * @author ld-development\r |
42 | *\r |
43 | * \r |
44 | */\r |
45 | public final class AttributeHelper {\r |
46 | \r |
47 | private final static Logger log = Logger.getLogger(AttributeHelper.class.getName());\r |
48 | /**\r |
49 | * filters the given attributes\r |
50 | * \r |
51 | * @param attributes\r |
52 | * @return only the attributes wich are of type <code>PrintJobAttribute</code> and not operation attributes \r |
53 | */\r |
54 | public final static PrintJobAttributeSet jobAttributes(PrintRequestAttributeSet attributes) {\r |
55 | PrintJobAttributeSet jobAttributes = new HashPrintJobAttributeSet();\r |
56 | if (attributes != null) {\r |
57 | AttributeSet invalidAttributes = jobOperationAttributes(attributes);\r |
58 | Object[] attributeArray = attributes.toArray();\r |
59 | for (int i = 0; i < attributeArray.length; i++) {\r |
60 | Attribute attribute = (Attribute) attributeArray[i];\r |
61 | //attributes-charset, attributes-natural-language etc. are not set by the user\r |
62 | if (attribute instanceof PrintJobAttribute && !(invalidAttributes.containsValue(attribute))) {\r |
63 | jobAttributes.add(attribute);\r |
64 | }\r |
65 | }\r |
66 | }\r |
67 | return jobAttributes;\r |
68 | }\r |
69 | \r |
70 | /**\r |
71 | * filters the given attributes\r |
72 | * \r |
73 | * @param attributes\r |
74 | * @return only job-operation attributes\r |
75 | */\r |
76 | public final static AttributeSet jobOperationAttributes(PrintRequestAttributeSet attributes) {\r |
77 | AttributeSet operationAttributes = new HashAttributeSet();\r |
78 | if (attributes != null) {\r |
79 | Object[] attributeArray = attributes.toArray();\r |
80 | for (int i = 0; i < attributeArray.length; i++) {\r |
81 | Attribute attribute = (Attribute) attributeArray[i];\r |
82 | //attributes-charset, attributes-natural-language etc. are not set by the user\r |
83 | if (attribute.getCategory().equals(IppAttributeName.JOB_NAME.getCategory())\r |
84 | || attribute.getCategory().equals(IppAttributeName.FIDELITY.getCategory())\r |
85 | || attribute.getCategory().equals(IppAttributeName.JOB_IMPRESSIONS.getCategory())\r |
86 | || attribute.getCategory().equals(IppAttributeName.JOB_K_OCTETS.getCategory())\r |
87 | || attribute.getCategory().equals(IppAttributeName.JOB_MEDIA_SHEETS.getCategory())\r |
88 | || attribute.getCategory().equals(IppAttributeName.JOB_NAME.getCategory())\r |
89 | || attribute.getCategory().equals(IppAttributeName.COMPRESSION.getCategory())\r |
90 | || attribute.getCategory().equals(IppAttributeName.REQUESTING_USER_NAME.getCategory())\r |
91 | || attribute.getCategory().equals(IppAttributeName.REQUESTING_USER_PASSWD.getCategory())) {\r |
92 | operationAttributes.add(attribute);\r |
93 | }\r |
94 | }\r |
95 | }\r |
96 | return operationAttributes;\r |
97 | }\r |
98 | \r |
99 | /**\r |
100 | * @param multiDoc\r |
101 | * @return a <code>List</code> with the document-format, document-name and last-document\r |
102 | */\r |
103 | public final static AttributeSet docOperationAttributes(SimpleMultiDoc multiDoc) {\r |
104 | AttributeSet operationAttributes = new HashAttributeSet();\r |
105 | try {\r |
106 | operationAttributes = docOperationAttributes(multiDoc.getDoc());\r |
107 | } catch (IOException e) {\r |
108 | log.log(Level.SEVERE, "Could not get Doc from multiDoc", e);\r |
109 | }\r |
110 | LastDocument lastDocument;\r |
111 | if (multiDoc.isLast()) {\r |
112 | lastDocument = LastDocument.TRUE;\r |
113 | } else {\r |
114 | lastDocument = LastDocument.FALSE;\r |
115 | }\r |
116 | operationAttributes.add(lastDocument);\r |
117 | return operationAttributes;\r |
118 | }\r |
119 | \r |
120 | /**\r |
121 | * @param doc\r |
122 | * @return a <code>List</code> with the document-format and document-name\r |
123 | */\r |
124 | public final static AttributeSet docOperationAttributes(Doc doc) {\r |
125 | AttributeSet operationAttributes = new HashAttributeSet();\r |
126 | if (doc.getAttributes() != null) {\r |
127 | DocumentName docName = (DocumentName) doc.getAttributes().get(IppAttributeName.DOCUMENT_NAME.getCategory());\r |
128 | if (docName != null) {\r |
129 | operationAttributes.add(docName);\r |
130 | }\r |
131 | }\r |
132 | operationAttributes.add(new DocumentFormat(doc.getDocFlavor().getMimeType(), Locale.getDefault()));\r |
133 | return operationAttributes;\r |
134 | }\r |
135 | \r |
136 | /**\r |
137 | * @param operationAttributes2\r |
138 | * @return\r |
139 | */\r |
140 | public final static Attribute[] getOrderedOperationAttributeArray(AttributeSet operationAttributes2) {\r |
141 | AttributeSet copy = new HashAttributeSet(operationAttributes2);\r |
142 | Attribute[] attributes = new Attribute[copy.size()];\r |
143 | int i = 0;\r |
144 | // start with Charset\r |
145 | Class category = IppAttributeName.CHARSET.getCategory();\r |
146 | if (copy.containsKey(category)) {\r |
147 | attributes[i] = copy.get(category);\r |
148 | copy.remove(category);\r |
149 | i++;\r |
150 | }\r |
151 | // start with Charset\r |
152 | category = IppAttributeName.NATURAL_LANGUAGE.getCategory();\r |
153 | if (copy.containsKey(category)) {\r |
154 | attributes[i] = copy.get(category);\r |
155 | copy.remove(category);\r |
156 | i++;\r |
157 | }\r |
158 | // add the rest\r |
159 | Attribute[] remaining = copy.toArray();\r |
160 | for (int j = 0; j < remaining.length; j++) {\r |
161 | attributes[i+j] = remaining[j];\r |
162 | }\r |
163 | return attributes;\r |
164 | }\r |
165 | \r |
166 | \r |
167 | \r |
168 | }\r |