KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > htmleditors > JahiaHtmlEditorsDigester


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
package org.jahia.services.htmleditors;
14
15 import java.io.File JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Set JavaDoc;
19 import java.util.Vector JavaDoc;
20 import java.util.HashMap JavaDoc;
21
22 import org.apache.commons.digester.Digester;
23 import org.apache.commons.digester.Rule;
24
25 /**
26  * Load Html Editors from XML configuration file.
27  * Implementation based on Apache Digester.
28  *
29  * @author Khue Nguyen
30  */

31 class JahiaHtmlEditorsDigester
32 {
33     private Digester digester;
34     private Vector JavaDoc editors;
35     private Vector JavaDoc cssList;
36
37     public JahiaHtmlEditorsDigester(){
38         this.initDigester();
39         this.editors = new Vector JavaDoc();
40         this.cssList = new Vector JavaDoc();
41     }
42
43     private void initDigester(){
44         this.digester = new Digester();
45
46         AddHtmlEditorRule rule = new AddHtmlEditorRule();
47         digester.addRule("editors/editor", rule);
48         digester.addRule("editors/editor/id", rule.setParamRule);
49         digester.addRule("editors/editor/name", rule.setParamRule);
50         digester.addRule("editors/editor/base-directory", rule.setParamRule);
51         digester.addRule("editors/editor/include-file", rule.setParamRule);
52         digester.addRule("editors/editor/compatibility-tester", rule.setParamRule);
53         digester.addRule("editors/editor/enable-css", rule.setParamRule);
54         digester.addRule("editors/editor/rank", rule.setParamRule);
55
56         AddCSSRule addCSSRule = new AddCSSRule();
57         digester.addRule("editors/style-sheets/css",addCSSRule);
58         digester.addRule("editors/style-sheets/css/id",addCSSRule.setParamRule);
59         digester.addRule("editors/style-sheets/css/name",addCSSRule.setParamRule);
60         digester.addRule("editors/style-sheets/css/url",addCSSRule.setParamRule);
61         digester.addRule("editors/style-sheets/css/shared",addCSSRule.setParamRule);
62         digester.addRule("editors/style-sheets/css/allowed-sites/site-key", addCSSRule.addAllowedSiteRule);
63     }
64
65     /**
66      * Full path to the XML source file, from which to load the Html Editors.
67      * The internal list of HtmlEditors will be cleared before loading again.
68      *
69      * @param sourceFile
70      */

71     public boolean loadHtmlEditors(String JavaDoc sourceFile){
72         boolean success = false;
73         try {
74             File JavaDoc xmlSourceFile = new File JavaDoc(sourceFile);
75             this.digester.parse(xmlSourceFile);
76             success = true;
77         } catch (org.xml.sax.SAXException JavaDoc saxe) {
78             saxe.printStackTrace();
79         } catch ( IOException JavaDoc ioe ){
80             ioe.printStackTrace();
81         }
82         return success;
83     }
84
85     /**
86      * Add a new Html Editor to the internal list
87      * @param id
88      * @param name
89      * @param baseDirectory
90      * @param includeFile
91      * @param enableCSS
92      */

93     public void addHtmlEditor( String JavaDoc id,
94                                String JavaDoc name,
95                                String JavaDoc baseDirectory,
96                                String JavaDoc includeFile,
97                                String JavaDoc compatibilityTester,
98                                String JavaDoc enableCSS,
99                                int rank ){
100
101         HtmlEditor editor =
102                 new JahiaHtmlEditor(id,name,baseDirectory,includeFile,
103                 compatibilityTester,"true".equalsIgnoreCase(enableCSS), rank);
104         this.editors.add(editor);
105     }
106
107     /**
108      * Returns the vector of HtmlEditors
109      * You must call loadHtmlEditors once to load them first
110      *
111      * @return
112      */

113     public Vector JavaDoc getHtmlEditors(){
114         return this.editors;
115     }
116
117     /**
118      * Returns the vector of CSS
119      * You must call loadHtmlEditors once to load them first
120      *
121      * @return
122      */

123     public Vector JavaDoc getCSSList(){
124         return this.cssList;
125     }
126
127     final class AddHtmlEditorRule extends Rule {
128         private HashMap JavaDoc params = new HashMap JavaDoc();
129         SetParamRule setParamRule = new SetParamRule();
130
131         public void end(String JavaDoc namespace, String JavaDoc name)
132                 throws Exception JavaDoc {
133             int rank = -1;
134             if (params.get("rank") != null) {
135                 rank = Integer.parseInt((String JavaDoc) params.get("rank"));
136             }
137             JahiaHtmlEditor editor = new JahiaHtmlEditor((String JavaDoc) params.get("id"),
138                     (String JavaDoc) params.get("name"),
139                     (String JavaDoc) params.get("base-directory"),
140                     (String JavaDoc) params.get("include-file"),
141                     (String JavaDoc) params.get("compatibility-tester"),
142                     "true".equalsIgnoreCase((String JavaDoc) params.get("enable-css")),
143                     rank);
144             params.clear();
145             editors.add( editor );
146         }
147
148         final class SetParamRule extends Rule {
149             public void body(String JavaDoc namespace, String JavaDoc name, String JavaDoc text)
150                     throws Exception JavaDoc {
151                 params.put(name,text);
152             }
153         }
154     }
155
156     final class AddCSSRule extends Rule {
157         private HashMap JavaDoc params = new HashMap JavaDoc();
158         private Set JavaDoc allowedSites = new HashSet JavaDoc();
159         SetParamRule setParamRule = new SetParamRule();
160         AddAllowedSiteRule addAllowedSiteRule = new AddAllowedSiteRule();
161
162         public void end(String JavaDoc namespace, String JavaDoc name)
163                 throws Exception JavaDoc {
164             HtmlEditorCSS css =
165                     new HtmlEditorCSS((String JavaDoc) params.get("id"),
166                             (String JavaDoc) params.get("name"),
167                             (String JavaDoc) params.get("url"),
168                             "true".equalsIgnoreCase((String JavaDoc) params.get("shared")));
169
170             css.addAllowedSites(this.allowedSites);
171             this.allowedSites = new HashSet JavaDoc();
172
173             params.clear();
174             allowedSites.clear();
175             cssList.add( css );
176         }
177
178         final class SetParamRule extends Rule {
179             public void body(String JavaDoc namespace, String JavaDoc name, String JavaDoc text)
180                     throws Exception JavaDoc {
181                 params.put(name,text);
182             }
183         }
184
185         final class AddAllowedSiteRule extends Rule {
186             public void body(String JavaDoc namespace, String JavaDoc name, String JavaDoc text)
187                     throws Exception JavaDoc {
188                 allowedSites.add(text);
189             }
190         }
191     }
192 }
193
194
Popular Tags