KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > dataxslt > MicrositeFactory


1 /*
2   Copyright (C) 2003 Know Gate S.L. All rights reserved.
3                       C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.dataxslt;
34
35 import java.util.HashMap JavaDoc;
36
37 /**
38  * A SoftReferences cache to Microsite objects.
39  * When working with XSL tranformations, typically only a small number of Microsite
40  * definitions are used. the definitions are stored in XML files that have to be parsed
41  * for adding data to PageSets.<br>
42  * MicrositeFactory loads once and the reuses Microsite objects reducing disk access
43  * and CPU intensive XML parsing routines.
44  * @author Sergio Montoro Ten
45  * @version 1.0
46  */

47 import java.lang.ref.SoftReference JavaDoc;
48
49 public class MicrositeFactory {
50   public static boolean bCache = true;
51   public static HashMap JavaDoc oMicrosites = new HashMap JavaDoc();
52
53   public MicrositeFactory() {
54   }
55
56   /**
57    * @return Caching status on/off
58    */

59   public static boolean cache () {
60     return bCache;
61   }
62
63   /**
64    * Turns Microsite caching on/off
65    * @param bCacheOnOf <b>true</b> if Microsite caching is to be activated,
66    * <b>false</b> if Microsite caching is to be deactivated.
67    */

68   public static void cache (boolean bCacheOnOf) {
69     bCache = bCacheOnOf;
70     if (false==bCacheOnOf)
71       oMicrosites.clear();
72   }
73
74   /**
75    * Get a Microsite from an XML file
76    * If Microsite is cached then cached instance is returned.
77    * @param sURI XML file URI starting with file://
78    * (for example file:///opt/knowgate/storage/xslt/templates/Comtemporary.xml)
79    * @param bValidateXML <b>true</b> if XML validation with W3C schemas is to be done,
80    * <b>false</b> is no validation is to be done.
81    * @return Microsite instance
82    * @throws ClassNotFoundException
83    * @throws IllegalAccessException
84    */

85   public static synchronized Microsite getInstance(String JavaDoc sURI, boolean bValidateXML) throws ClassNotFoundException JavaDoc, Exception JavaDoc, IllegalAccessException JavaDoc {
86     Microsite oRetObj;
87     Object JavaDoc oRefObj;
88
89     if (bCache) {
90       oRefObj = oMicrosites.get(sURI);
91
92       if (null == oRefObj) {
93         oRetObj = new Microsite(sURI, bValidateXML);
94         oMicrosites.put(sURI, new SoftReference JavaDoc(oRetObj));
95       }
96       else {
97         oRetObj = (Microsite) ( (SoftReference JavaDoc) oRefObj).get();
98         if (null == oRetObj)
99           oRetObj = new Microsite(sURI, bValidateXML);
100       }
101       return oRetObj;
102     }
103     else
104       oRetObj = new Microsite(sURI, bValidateXML);
105
106     return oRetObj;
107   } // getInstance
108

109   // ---------------------------------------------------------------------------
110

111   /**
112    * Get a Microsite from an XML file
113    * If Microsite is cached then cached instance is returned.
114    * XML validation is disabled.
115    * @param sURI XML file path
116    * @return Microsite instance
117    * @throws ClassNotFoundException
118    * @throws IllegalAccessException
119    */

120
121   public static Microsite getInstance(String JavaDoc sURI) throws ClassNotFoundException JavaDoc, Exception JavaDoc, IllegalAccessException JavaDoc {
122     return getInstance(sURI, false);
123   } // getInstance
124
}
Popular Tags