KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > serialization > LabelSerializerFactory


1 package org.snipsnap.serialization;
2
3 import java.util.Map JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import org.radeox.util.Service;
7 import org.radeox.util.logging.Logger;
8
9 /** @author gis */
10 public class LabelSerializerFactory {
11
12     /** Creates an instance of a LabelSerializerFactory for the given outputFormat (as specified in @see SerializerFactory) */
13     public LabelSerializerFactory(int outputFormat) {
14         m_outputFormat = String.valueOf(outputFormat);
15     }
16
17     public LabelSerializer createSerializerFor(String JavaDoc labelType) {
18         Class JavaDoc serClass = getClassFor(labelType);
19         if (serClass == null)
20             return null;
21         try {
22             LabelSerializer serializer = (LabelSerializer)serClass.newInstance();
23             return serializer;
24         }
25         catch (Exception JavaDoc e) { }
26         return null;
27     }
28
29     /**
30      * All instances of this factory share a map of default label serializers, that will be used
31      * when no specific serializer for a given label-type is available. This is how to add those.
32      * @param format the output format as specified in @see SerializerFactory
33      */

34     public static synchronized void addDefaultLabelSerializer(String JavaDoc labelType, int format, Class JavaDoc cls) {
35         if (s_defaultClasses == null)
36             s_defaultClasses = new HashMap JavaDoc();
37         Class JavaDoc[] interfaces = cls.getInterfaces();
38         boolean ok = false;
39         for (int i = 0; i < interfaces.length; i++) {
40             if (interfaces[i].getName() == "org.snipsnap.serialization.LabelSerializer;") {
41                 ok = true;
42                 break;
43             }
44         }
45         if (!ok) {
46             Logger.debug("LabelSerializerFactory::addDefaultLabelSerializer: supplied Class does not implement LabelSerializer interface");
47             return;
48         }
49         s_defaultClasses.put(getKey(labelType, String.valueOf(format)), cls);
50     }
51
52     private Class JavaDoc getClassFor(String JavaDoc labelType) {
53         if (m_classMap.isEmpty()) // optimistically, we load them when we don't have any
54
loadClasses();
55         String JavaDoc key = getKey(labelType, m_outputFormat);
56         Class JavaDoc cls = (Class JavaDoc)m_classMap.get(key);
57         if (cls == null && s_defaultClasses != null)
58             cls = (Class JavaDoc)s_defaultClasses.get(key);
59         return cls;
60     }
61
62     private void loadClasses() {
63         m_classMap.clear();
64         try {
65             // TODO: overload Service.providers() to take a String as argument
66
Iterator JavaDoc it = Service.providers(Class.forName("org.snipsnap.serialization.LabelSerializer"));
67             while (it.hasNext()) {
68                 try {
69                     LabelSerializer ls = (LabelSerializer)it.next();
70                     int outputFormat = ls.getOutputFormat();
71                     Iterator JavaDoc typesIt = ls.getSupportedLabelTypes().iterator();
72                     while (typesIt.hasNext()) {
73                         String JavaDoc key = getKey((String JavaDoc)typesIt.next(), m_outputFormat);
74                         m_classMap.put(key, ls.getClass());
75                     }
76                 }
77                 catch (Exception JavaDoc e) // be tolerant
78
{ }
79             }
80         }
81         catch (ClassNotFoundException JavaDoc e) { }
82     }
83
84     private static String JavaDoc getKey(String JavaDoc type, String JavaDoc m_outputFormat) {
85         return m_outputFormat + "_" + type;
86     }
87
88     private String JavaDoc m_outputFormat;
89     private static Map JavaDoc s_defaultClasses = null;
90     private Map JavaDoc m_classMap = new HashMap JavaDoc();
91 }
92
Popular Tags