1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      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 /* $Id: AFPResourceLevelDefaults.java 1761021 2016-09-16 11:40:57Z ssteiner $ */
19 
20 package org.apache.fop.afp;
21 
22 import java.util.Map;
23 
24 import org.apache.fop.afp.AFPResourceLevel.ResourceType;
25 import org.apache.fop.afp.modca.ResourceObject;
26 
27 /**
28  * This class holds resource levels defaults for the various resource types.
29  */
30 public class AFPResourceLevelDefaults {
31 
32     private static final Map RESOURCE_TYPE_NAMES = new java.util.HashMap();
33 
34     static {
35         //Map to be extended as need arises:
36         registerResourceTypeName("goca", ResourceObject.TYPE_GRAPHIC);
37         registerResourceTypeName("bitmap", ResourceObject.TYPE_IMAGE);
38     }
39 
registerResourceTypeName(String name, byte type)40     private static void registerResourceTypeName(String name, byte type) {
41         RESOURCE_TYPE_NAMES.put(name.toLowerCase(), type);
42     }
43 
getResourceType(String resourceTypeName)44     private static byte getResourceType(String resourceTypeName) {
45         Byte result = (Byte)RESOURCE_TYPE_NAMES.get(resourceTypeName.toLowerCase());
46         if (result == null) {
47             throw new IllegalArgumentException("Unknown resource type name: " + resourceTypeName);
48         }
49         return result;
50     }
51 
52     private Map defaultResourceLevels = new java.util.HashMap();
53 
54     /**
55      * Creates a new instance with default values.
56      */
AFPResourceLevelDefaults()57     public AFPResourceLevelDefaults() {
58         // level not explicitly set/changed so default to inline for GOCA graphic objects
59         // (due to a bug in the IBM AFP Workbench Viewer (2.04.01.07), hard copy works just fine)
60         setDefaultResourceLevel(ResourceObject.TYPE_GRAPHIC,
61                 new AFPResourceLevel(ResourceType.INLINE));
62     }
63 
64     /**
65      * Sets the default resource level for a particular resource type.
66      * @param type the resource type name
67      * @param level the resource level
68      */
setDefaultResourceLevel(String type, AFPResourceLevel level)69     public void setDefaultResourceLevel(String type, AFPResourceLevel level) {
70         setDefaultResourceLevel(getResourceType(type), level);
71     }
72 
73     /**
74      * Sets the default resource level for a particular resource type.
75      * @param type the resource type ({@link ResourceObject}.TYPE_*)
76      * @param level the resource level
77      */
setDefaultResourceLevel(byte type, AFPResourceLevel level)78     public void setDefaultResourceLevel(byte type, AFPResourceLevel level) {
79         this.defaultResourceLevels.put(type, level);
80     }
81 
82     /**
83      * Returns the default resource level for a particular resource type.
84      * @param type the resource type ({@link ResourceObject}.TYPE_*)
85      * @return the default resource level
86      */
getDefaultResourceLevel(byte type)87     public AFPResourceLevel getDefaultResourceLevel(byte type) {
88         AFPResourceLevel result = (AFPResourceLevel)this.defaultResourceLevels.get(type);
89         if (result == null) {
90             result = AFPResourceInfo.DEFAULT_LEVEL;
91         }
92         return result;
93     }
94 
95     /**
96      * Allows to merge the values from one instance into another. Values from the instance passed
97      * in as a parameter override values of this instance.
98      * @param other the other instance to get the defaults from
99      */
mergeFrom(AFPResourceLevelDefaults other)100     public void mergeFrom(AFPResourceLevelDefaults other) {
101         for (Object o : other.defaultResourceLevels.entrySet()) {
102             Map.Entry entry = (Map.Entry) o;
103             Byte type = (Byte) entry.getKey();
104             AFPResourceLevel level = (AFPResourceLevel) entry.getValue();
105             this.defaultResourceLevels.put(type, level);
106         }
107     }
108 
109 }
110