KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > format > FormatterFactory


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  */

12 package com.tonbeller.wcf.format;
13
14 import java.io.IOException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.Locale JavaDoc;
17
18 import org.apache.commons.digester.Digester;
19 import org.apache.commons.digester.xmlrules.DigesterLoader;
20 import org.apache.log4j.Logger;
21 import org.xml.sax.InputSource JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23
24 import com.tonbeller.tbutils.res.Resources;
25 import com.tonbeller.wcf.utils.SoftException;
26
27 /**
28  * Created on 15.11.2002
29  *
30  * @author av
31  */

32 public class FormatterFactory {
33     private static Logger logger = Logger.getLogger(FormatterFactory.class);
34
35   private FormatterFactory() {}
36
37   /**
38    * returns a new instance
39    */

40   public static Formatter instance(Locale JavaDoc locale) {
41     URL JavaDoc defaultXml = Formatter.class.getResource("config.xml");
42     Formatter formatter = new Formatter();
43     fillFormatter(formatter, locale, defaultXml);
44     String JavaDoc s = Resources.instance().getOptionalString("wcf.formatter.config.xml", null);
45     if (s != null) {
46       URL JavaDoc analyseXml = Formatter.class.getResource(s);
47       fillFormatter(formatter, locale, analyseXml);
48     }
49
50     return formatter;
51   }
52
53   private static void fillFormatter(Formatter formatter, Locale JavaDoc locale, URL JavaDoc configXml) {
54
55     if (locale == null)
56       locale = Locale.getDefault();
57
58     URL JavaDoc rulesXml = Formatter.class.getResource("rules.xml");
59     Digester digester = DigesterLoader.createDigester(rulesXml);
60     digester.setValidating(false);
61     digester.push(formatter);
62     try {
63       digester.parse(new InputSource JavaDoc(configXml.toExternalForm()));
64     } catch (IOException JavaDoc e) {
65       logger.error("exception caught", e);
66       throw new SoftException(e);
67     } catch (SAXException JavaDoc e) {
68       logger.error("exception caught", e);
69       throw new SoftException(e);
70     }
71     formatter.setLocale(locale);
72   }
73
74
75 }
76
Popular Tags