KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > engines > shared > SmallText_Field


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 //
14
// SmallText_Field
15
// EV 14.01.20001
16
//
17
// handleField( mode, jParams )
18
//
19

20 package org.jahia.engines.shared;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 import org.apache.log4j.Logger;
27 import org.jahia.data.ConnectionTypes;
28 import org.jahia.data.FormDataManager;
29 import org.jahia.data.containers.JahiaContainer;
30 import org.jahia.data.fields.JahiaFieldDefinitionProperties;
31 import org.jahia.data.fields.JahiaField;
32 import org.jahia.engines.EngineMessages;
33 import org.jahia.engines.EngineParams;
34 import org.jahia.engines.JahiaEngine;
35 import org.jahia.engines.JahiaEngineTools;
36 import org.jahia.exceptions.JahiaException;
37 import org.jahia.params.ParamBean;
38 import org.jahia.registries.EnginesRegistry;
39 import org.jahia.registries.ServicesRegistry;
40 import org.jahia.services.acl.JahiaBaseACL;
41 import org.jahia.services.htmlparser.*;
42 import org.jahia.services.sites.*;
43 import org.jahia.utils.JahiaTools;
44 import org.jahia.utils.I18n;
45 import org.jahia.bin.Jahia;
46 import java.io.UnsupportedEncodingException JavaDoc;
47
48 public class SmallText_Field
49 {
50
51     private static Logger logger = Logger.getLogger(SmallText_Field.class);
52
53     private static final int MAX_SMALLTEXT_LENGTH = 250;
54
55
56     private static SmallText_Field theObject = null;
57     public static final String JavaDoc JSP_FILE = "/jsp/jahia/engines/shared/smalltext_field.jsp";
58     public static final String JavaDoc READONLY_JSP = "/jsp/jahia/engines/shared/readonly_smalltext_field.jsp";
59
60
61     /***
62         * getInstance
63         * AK 19.12.2000
64         *
65         */

66     public static synchronized SmallText_Field getInstance()
67     {
68         if (theObject == null) {
69             theObject = new SmallText_Field();
70         }
71         return theObject;
72     } // end getInstance
73

74
75
76     /***
77         * handles the field actions
78         *
79         * @param jParams a ParamBean object
80         * @param mode the mode, according to JahiaEngine
81         * @return true if everything went okay, false if not
82         * @see org.jahia.engines.JahiaEngine
83         *
84         */

85     public boolean handleField( ParamBean jParams, Integer JavaDoc modeInt, HashMap JavaDoc engineMap )
86     throws JahiaException
87     {
88         int mode = modeInt.intValue();
89         JahiaField theField = (JahiaField) engineMap.get( "theField" );
90         jParams.getRequest().setAttribute(JahiaEngine.ENGINE_MODE_ATTRIBUTE,new Integer JavaDoc(mode));
91         switch (mode)
92         {
93             case (JahiaEngine.LOAD_MODE) : return composeEngineMap( jParams, engineMap, theField );
94             case (JahiaEngine.UPDATE_MODE) : return getFormData( jParams, engineMap, theField );
95             case (JahiaEngine.SAVE_MODE) : return saveData( jParams, engineMap, theField );
96         }
97         return false;
98     } // end handleField
99

100
101     // Oops... (DJ)
102
private String JavaDoc cypher (String JavaDoc s)
103     {
104         StringBuffer JavaDoc result = new StringBuffer JavaDoc ();
105         for (int i=0; i<s.length(); i++)
106         {
107             result.append ((char)(s.charAt(i) ^ 1));
108         }
109         return result.toString();
110     }
111
112     /***
113         * gets POST data from the form and saves it in session
114         *
115         * @param jParams a ParamBean object
116         * @param engineMap the engine hashmap
117         * @param theField the field we are working on
118         * @return true if everything went okay, false if not
119         *
120         */

121     private boolean getFormData( ParamBean jParams, HashMap JavaDoc engineMap, JahiaField theField )
122     throws JahiaException
123     {
124         boolean out = true;
125
126         String JavaDoc fieldValue = jParams.getRequest()
127                              .getParameter( "_" +
128                              new Integer JavaDoc(theField.getID()).toString() );
129
130         String JavaDoc engineParams = jParams.getRequest().getParameter( "engine_params" );
131         fieldValue = JahiaTools.replacePattern(fieldValue,"|","&#124;");
132
133         if ( fieldValue != null && "ISO-8859-1".equalsIgnoreCase(jParams.settings().
134                                           getDefaultResponseBodyEncoding())) {
135             // This code is used to transform submissions that might contain
136
// Windows 1252 characters (happens mostly when copy-pasting
137
// from Word documents) to ISO-8859-1 equivalents. Note that
138
// some of these conversions are actually LONGER than the
139
// original. For example the Euro character gets changed to
140
// "Euro" string. This conversion is not necessary for UTF-8
141
// because copy-paste does the conversion automatically in that
142
// case.
143
fieldValue = I18n.windows1252ToISO(fieldValue);
144         }
145         String JavaDoc oriFieldValue = fieldValue;
146
147         JahiaSite site = ServicesRegistry.getInstance().getJahiaSitesService()
148                        .getSite(theField.getJahiaID());
149
150         if ( fieldValue != null && site.getHtmlCleanup()==1 ){
151             Vector JavaDoc DOMVisitors = ServicesRegistry.getInstance()
152                                  .getHtmlParserService()
153                                  .getHtmlDOMVisitors(theField.getJahiaID());
154
155             ExtractLinksDOMVisitor linkExtractionVisitor = null;
156             if (site.getHtmlMarkupFiltering() == 1) {
157                 linkExtractionVisitor = new ExtractLinksDOMVisitor();
158                 linkExtractionVisitor.init(site.getID());
159                 DOMVisitors.add(linkExtractionVisitor);
160             }
161
162             // we test if the field value is a Jahia marker, in which case
163
// we do not modify the field value.
164
if (!fieldValue.startsWith("<jahia-")) {
165                 fieldValue = JahiaTextContentTidy.tidyPreProcessing(fieldValue);
166                 fieldValue = ServicesRegistry.getInstance().
167                              getHtmlParserService()
168                              .parse(fieldValue, DOMVisitors, theField.getSiteID());
169                 fieldValue = JahiaTextContentTidy.tidyPostProcessing(fieldValue);
170             }
171
172             EngineMessages resultMessages = ServicesRegistry.getInstance().getHtmlParserService()
173                 .checkTextParsingErrors(fieldValue);
174             if ( !resultMessages.isEmpty() ){
175                 resultMessages.saveMessages(String.valueOf(theField.getID()) + ".", jParams.getRequest());
176                 fieldValue = oriFieldValue;
177                 out = false;
178             }
179
180             if (linkExtractionVisitor != null) {
181                 Iterator JavaDoc linkIter = linkExtractionVisitor.getDocumentLinks().iterator();
182                 while (linkIter.hasNext()) {
183                     String JavaDoc curLink = (String JavaDoc) linkIter.next();
184                     logger.debug("Found link [" + curLink + "] in small text field.");
185                 }
186             }
187         }
188
189         if (fieldValue!=null)
190         {
191             if (cypher(fieldValue).equals("Rid&r!rn!mtbjx")) // Oops... (DJ)
192
fieldValue=cypher ("Rid&r!`!ru`s");
193             if (cypher(fieldValue).equals("Ctu!rid!bsx!bsx!bshdr"))
194                 fieldValue=cypher ("ho!ids!mnodmx!id`su-!uihojhof///");
195             if (cypher(fieldValue).equals("Hg!uidsd&r!onuihof"))
196                 fieldValue=cypher ("lhrrhof!ho!lx!mhgd");
197             if (cypher(fieldValue).equals("uido!vix!en!uidrd!ud`sr"))
198                 fieldValue=cypher ("bnld!`u!ohfiu!>///!!!!ZCshuodx\\");
199             if (cypher(fieldValue).equals("i`qqx!chsuie`x!e`e`&r"))
200                 fieldValue=cypher ("=hlf!rsb<#iuuq;..vvv/k`ih`/nsf.OHBD/fhg#?");
201
202             // we must check the length here, by correctly handling multibyte
203
// characters in the full byte length (unfortunately some databases
204
// such as Oracle using byte length instead of character length).
205
String JavaDoc encoding = jParams.getRequest().getCharacterEncoding();
206             if (encoding == null) {
207                 encoding = Jahia.getSettings().getDefaultResponseBodyEncoding();
208             }
209             try {
210                 int byteLength = fieldValue.getBytes(encoding).length;
211                 while (byteLength > MAX_SMALLTEXT_LENGTH) {
212                     logger.debug("Byte length of field value is over limit, truncating one byte from end...");
213                     // here we remove one character at a time because the byte
214
// length of a character may vary a lot.
215
fieldValue = fieldValue.substring(0, fieldValue.length() -1);
216                     byteLength = fieldValue.getBytes(encoding).length;
217                 }
218             } catch (UnsupportedEncodingException JavaDoc uee) {
219                 logger.error("Error while calculating byte length of field value for encoding " + encoding, uee);
220             }
221         }
222
223         EngineParams eParams = new EngineParams( engineParams );
224         String JavaDoc localSwitch = eParams.getParameter( "localswitch" );
225         String JavaDoc dataSourceUrl = eParams.getParameter( "dsurl" );
226
227         if (dataSourceUrl != null) {
228             theField.setValue( dataSourceUrl );
229             theField.setConnectType( ConnectionTypes.DATASOURCE );
230         } else if (localSwitch != null) {
231             theField.setValue( theField.getDefinition().getDefaultValue( jParams.getPage().getPageTemplateID() ) );
232             theField.setConnectType( ConnectionTypes.LOCAL );
233         } else if (fieldValue != null) {
234             logger.debug ("Field value = " + fieldValue);
235             EngineMessages resultMessages = new EngineMessages();
236             if (FormDataManager.getInstance().checkIntegrity(fieldValue, resultMessages)) {
237                 theField.setValue( fieldValue );
238             } else {
239                 theField.setValue( fieldValue );
240                 out = false;
241             }
242             logger.debug ("New Field value = " + fieldValue);
243         }
244
245         return out;
246     } // end getFormData
247

248
249
250     /***
251         * saves data in datasource
252         *
253         * @param jParams a ParamBean object
254         * @param engineMap the engine hashmap
255         * @param theField the field we are working on
256         * @return true if everything went okay, false if not
257         *
258         */

259     private boolean saveData( ParamBean jParams, HashMap JavaDoc engineMap, JahiaField theField )
260     throws JahiaException
261     {
262         logger.debug ("Saving Field " + theField.getDefinition().getName() + " : " + theField.getValue() );
263         //theField.setValue(FormDataManager.getInstance().formDecode(theField.getValue()));
264
return theField.save(jParams);
265
266     } // end saveData
267

268
269
270     /***
271         * composes engine hash map
272         *
273         * @param jParams a ParamBean object
274         * @param engineMap the engine hashmap
275         * @param theField the field we are working on
276         * @return true if everything went okay, false if not
277         *
278         */

279     private boolean composeEngineMap( ParamBean jParams, HashMap JavaDoc engineMap, JahiaField theField )
280     throws JahiaException
281     {
282
283         boolean editable = false;
284         JahiaContainer theContainer = (JahiaContainer)engineMap.get("theContainer");
285         if ( theContainer == null ){
286             // in case of a field , not a field in a container
287
editable = true;
288         } else {
289             HashMap JavaDoc ctnListFieldAcls = (HashMap JavaDoc) engineMap
290                     .get("ctnListFieldAcls");
291
292             if (theContainer.getListID() != 0 && ctnListFieldAcls != null
293                     && ctnListFieldAcls.size() > 0) {
294                 JahiaBaseACL acl = JahiaEngineTools.getCtnListFieldACL(
295                         ctnListFieldAcls, theField.getID());
296                 if (acl != null) {
297                     editable = acl.getPermission(jParams.getUser(),
298                             JahiaBaseACL.WRITE_RIGHTS, true);
299                 }
300             } else {
301                 editable = true;
302             }
303         }
304
305         String JavaDoc output = "";
306
307         String JavaDoc forward = theField.getDefinition()
308                        .getProperty(JahiaFieldDefinitionProperties.FIELD_UPDATE_JSP_FILE_PROP);
309         if ( forward == null ){
310             forward = SmallText_Field.JSP_FILE;
311             if ( !editable ){
312                 forward = SmallText_Field.READONLY_JSP;
313             }
314         }
315
316         if ( editable ) {
317
318             String JavaDoc localSwitchUrl = "ReloadEngine('localswitch" + EngineParams.VALUE_TOKEN + "yes')";
319             engineMap.put( "localSwitchUrl", localSwitchUrl );
320
321             String JavaDoc dataSourceConnectUrl = "";
322             JahiaEngine dsEngine = (JahiaEngine) EnginesRegistry.getInstance().getEngine( "selectdatasource" );
323             dataSourceConnectUrl = dsEngine.renderLink( jParams, theField );
324             engineMap.put( "dataSourceConnectUrl", dataSourceConnectUrl );
325
326             String JavaDoc dataSourceIDUrl = "";
327             dsEngine = (JahiaEngine) EnginesRegistry.getInstance().getEngine( "viewdatasourceid" );
328             dataSourceIDUrl = dsEngine.renderLink( jParams, theField );
329             engineMap.put( "dataSourceIDUrl", dataSourceIDUrl );
330
331             boolean isIE = false;
332             String JavaDoc userAgent = jParams.getRequest().getHeader( "user-agent" );
333             if (userAgent != null) {
334                 isIE = (userAgent.indexOf( "IE" ) != -1);
335             }
336             jParams.getRequest().setAttribute("isIE" , new Boolean JavaDoc(isIE));
337
338             JahiaSite site = ServicesRegistry.getInstance().getJahiaSitesService()
339                            .getSite(theField.getJahiaID());
340
341             String JavaDoc fieldValue = theField.getValue();
342             if ( fieldValue == null ){
343                 fieldValue = "";
344             }
345             String JavaDoc oriFieldValue = fieldValue;
346             if ( site.getHtmlCleanup()==1 ){
347                 Vector JavaDoc DOMVisitors = ServicesRegistry.getInstance()
348                                .getHtmlParserService()
349                                .getHtmlDOMVisitors(theField.getJahiaID());
350
351                 fieldValue = JahiaTextContentTidy.tidyPreProcessing(fieldValue);
352                 fieldValue = ServicesRegistry.getInstance().getHtmlParserService()
353                        .parse(fieldValue,DOMVisitors,theField.getSiteID());
354                 fieldValue = JahiaTextContentTidy.tidyPostProcessing(fieldValue);
355             }
356
357             EngineMessages resultMessages = ServicesRegistry.getInstance().getHtmlParserService()
358                 .checkTextParsingErrors(fieldValue);
359             if ( !resultMessages.isEmpty() ){
360                 resultMessages.saveMessages(String.valueOf(theField.getID()) + ".", jParams.getRequest());
361                 fieldValue = oriFieldValue;
362             }
363
364             theField.setValue( FormDataManager.getInstance().formEncode(fieldValue) );
365             output = ServicesRegistry.getInstance().getJahiaFetcherService().fetchServlet( jParams, forward );
366         } else {
367             output = ServicesRegistry.getInstance().getJahiaFetcherService().fetchServlet( jParams, forward );
368         }
369         engineMap.put( "fieldForm", output );
370         return true;
371
372     } // end composeEngineMap
373

374
375 } // end SmallText_Field
376
Popular Tags