Initial import.
[clinton/mirror/jspi/.git] / jspi / src / main / java / de / lohndirekt / print / attribute / AttributeHelper.java
diff --git a/jspi/src/main/java/de/lohndirekt/print/attribute/AttributeHelper.java b/jspi/src/main/java/de/lohndirekt/print/attribute/AttributeHelper.java
new file mode 100644 (file)
index 0000000..96db01b
--- /dev/null
@@ -0,0 +1,168 @@
+/**\r
+ * Copyright (C) 2003 <a href="http://www.lohndirekt.de/">lohndirekt.de</a>\r
+ *\r
+ * This library is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU Lesser General Public\r
+ * License as published by the Free Software Foundation; either\r
+ * version 2.1 of the License, or (at your option) any later version.\r
+ * \r
+ * This library is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
+ * Lesser General Public License for more details.\r
+ * \r
+ * You should have received a copy of the GNU Lesser General Public\r
+ * License along with this library; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
+ *  \r
+ */\r
+package de.lohndirekt.print.attribute;\r
+\r
+import java.io.IOException;\r
+import java.util.Locale;\r
+import java.util.logging.Level;\r
+import java.util.logging.Logger;\r
+\r
+import javax.print.Doc;\r
+import javax.print.attribute.Attribute;\r
+import javax.print.attribute.AttributeSet;\r
+import javax.print.attribute.HashAttributeSet;\r
+import javax.print.attribute.HashPrintJobAttributeSet;\r
+import javax.print.attribute.PrintJobAttribute;\r
+import javax.print.attribute.PrintJobAttributeSet;\r
+import javax.print.attribute.PrintRequestAttributeSet;\r
+import javax.print.attribute.standard.DocumentName;\r
+\r
+import de.lohndirekt.print.SimpleMultiDoc;\r
+import de.lohndirekt.print.attribute.ipp.DocumentFormat;\r
+import de.lohndirekt.print.attribute.ipp.LastDocument;\r
+\r
+/**\r
+ * @author ld-development\r
+ *\r
+ * \r
+ */\r
+public final class AttributeHelper {\r
+\r
+    private final static Logger log = Logger.getLogger(AttributeHelper.class.getName());\r
+    /**\r
+     * filters the given attributes\r
+     * \r
+     * @param attributes\r
+     * @return only the attributes wich are of type <code>PrintJobAttribute</code> and not operation attributes \r
+     */\r
+    public final static PrintJobAttributeSet jobAttributes(PrintRequestAttributeSet attributes) {\r
+        PrintJobAttributeSet jobAttributes = new HashPrintJobAttributeSet();\r
+        if (attributes != null) {\r
+            AttributeSet invalidAttributes = jobOperationAttributes(attributes);\r
+            Object[] attributeArray = attributes.toArray();\r
+            for (int i = 0; i < attributeArray.length; i++) {\r
+                Attribute attribute = (Attribute) attributeArray[i];\r
+                //attributes-charset, attributes-natural-language etc. are not set by the user\r
+                if (attribute instanceof PrintJobAttribute && !(invalidAttributes.containsValue(attribute))) {\r
+                    jobAttributes.add(attribute);\r
+                }\r
+            }\r
+        }\r
+        return jobAttributes;\r
+    }\r
+\r
+    /**\r
+     * filters the given attributes\r
+     * \r
+     * @param attributes\r
+     * @return only job-operation attributes\r
+     */\r
+    public final static AttributeSet jobOperationAttributes(PrintRequestAttributeSet attributes) {\r
+        AttributeSet operationAttributes = new HashAttributeSet();\r
+        if (attributes != null) {\r
+            Object[] attributeArray = attributes.toArray();\r
+            for (int i = 0; i < attributeArray.length; i++) {\r
+                Attribute attribute = (Attribute) attributeArray[i];\r
+                //attributes-charset, attributes-natural-language etc. are not set by the user\r
+                if (attribute.getCategory().equals(IppAttributeName.JOB_NAME.getCategory())\r
+                    || attribute.getCategory().equals(IppAttributeName.FIDELITY.getCategory())\r
+                    || attribute.getCategory().equals(IppAttributeName.JOB_IMPRESSIONS.getCategory())\r
+                    || attribute.getCategory().equals(IppAttributeName.JOB_K_OCTETS.getCategory())\r
+                    || attribute.getCategory().equals(IppAttributeName.JOB_MEDIA_SHEETS.getCategory())\r
+                    || attribute.getCategory().equals(IppAttributeName.JOB_NAME.getCategory())\r
+                    || attribute.getCategory().equals(IppAttributeName.COMPRESSION.getCategory())\r
+                    || attribute.getCategory().equals(IppAttributeName.REQUESTING_USER_NAME.getCategory())\r
+                    || attribute.getCategory().equals(IppAttributeName.REQUESTING_USER_PASSWD.getCategory())) {\r
+                    operationAttributes.add(attribute);\r
+                }\r
+            }\r
+        }\r
+        return operationAttributes;\r
+    }\r
+\r
+    /**\r
+     * @param multiDoc\r
+     * @return a <code>List</code> with the document-format, document-name and last-document\r
+     */\r
+    public final static AttributeSet docOperationAttributes(SimpleMultiDoc multiDoc) {\r
+        AttributeSet operationAttributes = new HashAttributeSet();\r
+        try {\r
+            operationAttributes = docOperationAttributes(multiDoc.getDoc());\r
+        } catch (IOException e) {\r
+            log.log(Level.SEVERE, "Could not get Doc from multiDoc", e);\r
+        }\r
+        LastDocument lastDocument;\r
+        if (multiDoc.isLast()) {\r
+            lastDocument = LastDocument.TRUE;\r
+        } else {\r
+            lastDocument = LastDocument.FALSE;\r
+        }\r
+        operationAttributes.add(lastDocument);\r
+        return operationAttributes;\r
+    }\r
+\r
+    /**\r
+     * @param doc\r
+     * @return a <code>List</code> with the document-format and document-name\r
+     */\r
+    public final static AttributeSet docOperationAttributes(Doc doc) {\r
+        AttributeSet operationAttributes = new HashAttributeSet();\r
+        if (doc.getAttributes() != null) {\r
+            DocumentName docName = (DocumentName) doc.getAttributes().get(IppAttributeName.DOCUMENT_NAME.getCategory());\r
+            if (docName != null) {\r
+                operationAttributes.add(docName);\r
+            }\r
+        }\r
+        operationAttributes.add(new DocumentFormat(doc.getDocFlavor().getMimeType(), Locale.getDefault()));\r
+        return operationAttributes;\r
+    }\r
+\r
+    /**\r
+     * @param operationAttributes2\r
+     * @return\r
+     */\r
+    public final static Attribute[] getOrderedOperationAttributeArray(AttributeSet operationAttributes2) {\r
+        AttributeSet copy = new HashAttributeSet(operationAttributes2);\r
+        Attribute[] attributes = new Attribute[copy.size()];\r
+        int i = 0;\r
+        // start with Charset\r
+        Class category = IppAttributeName.CHARSET.getCategory();\r
+        if (copy.containsKey(category)) {\r
+            attributes[i] = copy.get(category);\r
+            copy.remove(category);\r
+            i++;\r
+        }\r
+        // start with Charset\r
+        category = IppAttributeName.NATURAL_LANGUAGE.getCategory();\r
+        if (copy.containsKey(category)) {\r
+            attributes[i] = copy.get(category);\r
+            copy.remove(category);\r
+            i++;\r
+        }\r
+        // add the rest\r
+        Attribute[] remaining = copy.toArray();\r
+        for (int j = 0; j < remaining.length; j++) {\r
+            attributes[i+j] = remaining[j];\r
+        }\r
+        return attributes;\r
+    }\r
+    \r
+    \r
+\r
+}\r