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: * @author Michael Danilov, Pavel Dolgov
019: * @version $Revision$
020: */package java.awt.datatransfer;
021:
022: import java.util.ArrayList;
023: import java.util.Arrays;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.LinkedList;
027: import java.util.List;
028: import java.util.Map;
029:
030: import org.apache.harmony.awt.datatransfer.DTK;
031:
032: public final class SystemFlavorMap implements FlavorMap, FlavorTable {
033:
034: private static final String SERIALIZED_PREFIX = "org.apache.harmony.awt.datatransfer:"; //$NON-NLS-1$
035:
036: private final HashMap<DataFlavor, List<String>> flavor2Native = new HashMap<DataFlavor, List<String>>();
037: private final HashMap<String, List<DataFlavor>> native2Flavor = new HashMap<String, List<DataFlavor>>();
038:
039: public static boolean isJavaMIMEType(String str) {
040: return ((str != null) && str.startsWith(SERIALIZED_PREFIX));
041: }
042:
043: public static String encodeJavaMIMEType(String mimeType) {
044: if (mimeType == null) {
045: return null;
046: }
047: return (SERIALIZED_PREFIX + mimeType);
048: }
049:
050: public static String decodeJavaMIMEType(String nat) {
051: if (isJavaMIMEType(nat)) {
052: return nat.substring(SERIALIZED_PREFIX.length());
053: }
054: return null;
055: }
056:
057: public static String encodeDataFlavor(DataFlavor flav) {
058: if (flav == null) {
059: return null;
060: }
061: return (SERIALIZED_PREFIX + flav.getMimeType());
062: }
063:
064: public static DataFlavor decodeDataFlavor(String nat)
065: throws ClassNotFoundException {
066: if (isJavaMIMEType(nat)) {
067: return new DataFlavor(nat.substring(SERIALIZED_PREFIX
068: .length()));
069: }
070: return null;
071: }
072:
073: public static FlavorMap getDefaultFlavorMap() {
074: DTK dtk = DTK.getDTK();
075:
076: synchronized (dtk) {
077: SystemFlavorMap flavorMap = dtk.getSystemFlavorMap();
078:
079: if (flavorMap == null) {
080: flavorMap = new SystemFlavorMap(dtk);
081: dtk.setSystemFlavorMap(flavorMap);
082: }
083:
084: return flavorMap;
085: }
086: }
087:
088: private SystemFlavorMap(DTK dtk) {
089: dtk.initSystemFlavorMap(this );
090: }
091:
092: public synchronized List<DataFlavor> getFlavorsForNative(String nat) {
093: if (nat == null) {
094: ArrayList<DataFlavor> result = new ArrayList<DataFlavor>();
095: for (String key : native2Flavor.keySet()) {
096: result.addAll(native2Flavor.get(key));
097: }
098: return result;
099: }
100:
101: List<DataFlavor> list = native2Flavor.get(nat);
102: if ((list == null || list.isEmpty()) && isJavaMIMEType(nat)) {
103: String decodedNat = decodeJavaMIMEType(nat);
104: try {
105: DataFlavor flavor = new DataFlavor(decodedNat);
106: addMapping(nat, flavor);
107: list = native2Flavor.get(nat);
108: } catch (ClassNotFoundException e) {
109: }
110: }
111: return (list != null) ? new ArrayList<DataFlavor>(list)
112: : new ArrayList<DataFlavor>();
113: }
114:
115: public synchronized List<String> getNativesForFlavor(DataFlavor flav) {
116: if (flav == null) {
117: ArrayList<String> result = new ArrayList<String>();
118: for (DataFlavor key : flavor2Native.keySet()) {
119: result.addAll(flavor2Native.get(key));
120: }
121: return result;
122: }
123:
124: List<String> list = flavor2Native.get(flav);
125: if ((list == null || list.isEmpty())
126: && flav.isFlavorSerializedObjectType()) {
127: String nat = encodeDataFlavor(flav);
128: addMapping(nat, flav);
129: list = flavor2Native.get(flav);
130: }
131: return (list != null) ? new ArrayList<String>(list)
132: : new ArrayList<String>();
133: }
134:
135: public synchronized Map<String, DataFlavor> getFlavorsForNatives(
136: String[] natives) {
137: HashMap<String, DataFlavor> map = new HashMap<String, DataFlavor>();
138: Iterator<String> it = (natives != null) ? Arrays
139: .asList(natives).iterator() : native2Flavor.keySet()
140: .iterator();
141: while (it.hasNext()) {
142: String nat = it.next();
143: List<DataFlavor> list = getFlavorsForNative(nat);
144: if (list.size() > 0) {
145: map.put(nat, list.get(0));
146: }
147: }
148: return map;
149: }
150:
151: public synchronized Map<DataFlavor, String> getNativesForFlavors(
152: DataFlavor[] flavors) {
153: HashMap<DataFlavor, String> map = new HashMap<DataFlavor, String>();
154: Iterator<DataFlavor> it = (flavors != null) ? Arrays.asList(
155: flavors).iterator() : flavor2Native.keySet().iterator();
156: while (it.hasNext()) {
157: DataFlavor flavor = it.next();
158: List<String> list = getNativesForFlavor(flavor);
159: if (list.size() > 0) {
160: map.put(flavor, list.get(0));
161: }
162: }
163: return map;
164: }
165:
166: public synchronized void setNativesForFlavor(DataFlavor flav,
167: String[] natives) {
168: LinkedList<String> list = new LinkedList<String>();
169:
170: for (String nat : natives) {
171: if (!list.contains(nat)) {
172: list.add(nat);
173: }
174: }
175:
176: if (!list.isEmpty()) {
177: flavor2Native.put(flav, list);
178: } else {
179: flavor2Native.remove(flav);
180: }
181: }
182:
183: public synchronized void setFlavorsForNative(String nat,
184: DataFlavor[] flavors) {
185: LinkedList<DataFlavor> list = new LinkedList<DataFlavor>();
186:
187: for (DataFlavor flav : flavors) {
188: if (!list.contains(flav)) {
189: list.add(flav);
190: }
191: }
192:
193: if (!list.isEmpty()) {
194: native2Flavor.put(nat, list);
195: } else {
196: native2Flavor.remove(nat);
197: }
198: }
199:
200: public synchronized void addUnencodedNativeForFlavor(
201: DataFlavor flav, String nat) {
202: List<String> natives = flavor2Native.get(flav);
203:
204: if (natives == null) {
205: natives = new LinkedList<String>();
206: flavor2Native.put(flav, natives);
207: }
208: if (!natives.contains(nat)) {
209: natives.add(nat);
210: }
211: }
212:
213: public synchronized void addFlavorForUnencodedNative(String nat,
214: DataFlavor flav) {
215: List<DataFlavor> flavors = native2Flavor.get(nat);
216:
217: if (flavors == null) {
218: flavors = new LinkedList<DataFlavor>();
219: native2Flavor.put(nat, flavors);
220: }
221: if (!flavors.contains(flav)) {
222: flavors.add(flav);
223: }
224: }
225:
226: private void addMapping(String nat, DataFlavor flav) {
227: addUnencodedNativeForFlavor(flav, nat);
228: addFlavorForUnencodedNative(nat, flav);
229: }
230: }
|