Project Report: fawkez

Packagesummary org.jcoderz.commons.taskdefs

org.jcoderz.commons.taskdefs.AppInfoSaxHandler

LineHitsNoteSource
1  /*
2   * $Id: AppInfoSaxHandler.java 1575 2009-12-07 07:19:21Z amandel $
3   *
4   * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5   *
6   * Redistribution and use in source and binary forms, with or without
7   * modification, are permitted provided that the following conditions are
8   * met:
9   *
10   *    * Redistributions of source code must retain the above copyright
11   *      notice, this list of conditions and the following disclaimer.
12   *    * Redistributions in binary form must reproduce the above
13   *      copyright notice, this list of conditions and the following
14   *      disclaimer in the documentation and/or other materials
15   *      provided with the distribution.
16   *    * Neither the name of the jCoderZ.org Project nor the names of
17   *      its contributors may be used to endorse or promote products
18   *      derived from this software without specific prior written
19   *      permission.
20   *
21   * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25   * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   */
33  package org.jcoderz.commons.taskdefs;
34  
35  import java.util.ArrayList;
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Map;
39  import java.util.regex.Pattern;
40  
41  import org.xml.sax.Attributes;
42  import org.xml.sax.SAXException;
43  import org.xml.sax.SAXParseException;
44  import org.xml.sax.helpers.DefaultHandler;
45  
46  class AppInfoSaxHandler
47        extends DefaultHandler
48  {
49     /** Application Identifier of fawkeZ. */
50     public static final int APPLICATION_ID_FWK = 1;
51     /** Application Identifier of asf. */
52     public static final int APPLICATION_ID_ASF = 100;
53     /** Application Identifier of ppg. */
54     public static final int APPLICATION_ID_PPG = 101;
55     /** Application Identifier of taco. */
56     public static final int APPLICATION_ID_TAC = 102;
57     /** Application Identifier of application (amandel). */
58     public static final int APPLICATION_ID_ACM = 120;
59     /** Application Identifier of application (amandel). */
60     public static final int APPLICATION_ID_ACR = 121;
61     /** Application Identifier of application (amandel). */
62     public static final int APPLICATION_ID_DMB = 122;
63  
640    private static final Pattern REGEX_SINGLE_QUOTES
65           = Pattern.compile(".*[^']'[^'].*",
66              Pattern.DOTALL + Pattern.MULTILINE);
67  
680    private static final Pattern REGEX_VARIABLES
69           = Pattern.compile(".*\\{.+\\}.*",
70              Pattern.DOTALL + Pattern.MULTILINE);
71  
72     private static final String EMPTY_STRING = "";
73  
740    private SAXParseException mSaxParseException = null;
750    private boolean mValidationError = false;
76  
77     /** Maps application id (Integer) to app short-name (String). */
780    private final NamedMap mMap = new NamedMap("applications");
79  
800    private String mCurrentAppName = EMPTY_STRING;
810    private String mCurrentGrpName = EMPTY_STRING;
820    private String mCurrentMsgName = EMPTY_STRING;
830    private int mCurrentAppId = 0;
840    private int mCurrentGrpId = 0;
850    private int mCurrentMsgId = 0;
86  
870    private final List mWarningMessages = new ArrayList();
88  
890    private final StringBuffer mBuffer = new StringBuffer();
900    private boolean mCaptureCharacters = false;
91  
92     /**
93      * Constructor.
94      */
95     public AppInfoSaxHandler ()
960    {
970       mMap.registerApplication(APPLICATION_ID_FWK, "FWK");
980       mMap.registerApplication(APPLICATION_ID_ASF, "ASF");
990       mMap.registerApplication(APPLICATION_ID_PPG, "PPG");
1000       mMap.registerApplication(APPLICATION_ID_TAC, "TAC");
1010       mMap.registerApplication(APPLICATION_ID_ACR, "ACR");
1020       mMap.registerApplication(APPLICATION_ID_ACM, "ACM");
1030       mMap.registerApplication(APPLICATION_ID_DMB, "DMB");
1040    }
105  
106     /** {@inheritDoc} */
107     public void error (SAXParseException exception)
108     {
1090      mValidationError = true;
1100      mSaxParseException = exception;
1110    }
112  
113     /** {@inheritDoc} */
114     public void fatalError (SAXParseException exception)
115     {
1160       mValidationError = true;
1170       mSaxParseException = exception;
1180    }
119  
120     /** {@inheritDoc} */
121     public void warning (SAXParseException exception)
122     {
123        // NOP
1240(1)   }
125  
126     /** {@inheritDoc} */
127     public void startDocument ()
128     {
1290       reset();
1300    }
131  
132     /** {@inheritDoc} */
133     public void startElement (String uri, String localName,
134           String qName, Attributes attributes)
135           throws SAXException
136     {
137        try
138        {
1390          if ("application".equals(localName))
140           {
1410             mCurrentAppName = attributes.getValue("short-name");
1420             mCurrentAppId = Integer.parseInt(attributes.getValue("id"));
143  
1440             mMap.addApplication(mCurrentAppId, mCurrentAppName);
145           }
1460          else if ("group".equals(localName))
147           {
1480             mCurrentGrpName = attributes.getValue("short-name");
1490             mCurrentGrpId = Integer.parseInt(attributes.getValue("id"));
150  
1510             mMap.getApp(mCurrentAppId).addGroup(mCurrentGrpId, mCurrentGrpName);
152           }
1530          else if ("message".equals(localName))
154           {
1550             mCurrentMsgName = attributes.getValue("name");
1560             mCurrentMsgId = Integer.parseInt(attributes.getValue("id"));
157  
1580             mMap.getApp(mCurrentAppId).getGrp(
159                    mCurrentGrpId).addMessage(mCurrentMsgId, mCurrentMsgName);
160           }
1610          else if ("text".equals(localName) || "solution".equals(localName))
162           {
1630             captureCharacters();
164           }
165        }
1660       catch (AppInfoException e)
167        {
1680          throw new SAXException(e);
1690       }
1700    }
171  
172     /** {@inheritDoc} */
173     public void endElement (String uri, String localName, String qName)
174     {
1750       if ("text".equals(localName))
176        {
1770          final String cdata = characters().trim();
1780          validateText(cdata);
1790       }
1800       else if ("solution".equals(localName))
181        {
1820          final String cdata = characters().trim();
1830          validateSolution(cdata);
1840       }
1850       else if ("description".equals(localName))
186        {
1870          final String cdata = characters().trim();
1880          validateDescription(cdata);
1890       }
1900       else if ("procedure".equals(localName))
191        {
1920          final String cdata = characters().trim();
1930          validateProcedure(cdata);
1940       }
1950       else if ("validation".equals(localName))
196        {
1970          final String cdata = characters().trim();
1980          validateValidation(cdata);
1990       }
2000       else if ("application".equals(localName))
201        {
2020          mCurrentAppId = 0;
2030          mCurrentAppName = EMPTY_STRING;
204        }
2050       else if ("group".equals(localName))
206        {
2070          mCurrentGrpId = 0;
2080          mCurrentGrpName = EMPTY_STRING;
209        }
2100       else if ("message".equals(localName))
211        {
2120          mCurrentMsgId = 0;
2130          mCurrentMsgName = EMPTY_STRING;
214        }
2150    }
216  
217     /** {@inheritDoc} */
218     public void characters (char[] ch, int start, int length)
219     {
2200       if (mCaptureCharacters)
221        {
2220          mBuffer.append(ch, start, length);
223        }
2240    }
225  
226     /**
227      * Returns the captured characters and <b>clears</b> the internal
228      * buffer.
229      * @return the captured characters.
230      */
231     public String characters ()
232     {
2330       final String result = mBuffer.toString();
2340       mBuffer.setLength(0);
2350       mCaptureCharacters = false;
2360       return result;
237     }
238  
239     /**
240      * Returns <tt>true</tt> if there are warning messages available.
241      * @return <tt>true</tt> if there are warning messages available;
242      *       <tt>false</tt> otherwise.
243      */
244     public boolean hasWarningMessages ()
245     {
2460       return !mWarningMessages.isEmpty();
247     }
248  
249     /**
250      * Returns a list&lt;String&gt; of warning messages.
251      * @return a list&lt;String&gt; of warning messages.
252      */
253     public List getWarningMessages ()
254     {
2550       return mWarningMessages;
256     }
257  
258  
259     void captureCharacters ()
260     {
2610       mCaptureCharacters = true;
2620    }
263  
264     SAXParseException getParseException ()
265     {
2660       return mSaxParseException;
267     }
268  
269     boolean hasValidationErrors ()
270     {
2710       return mValidationError;
272     }
273  
274     private void validateText (String cdata)
275     {
2760       if (cdata != null)
277        {
2780          if (REGEX_SINGLE_QUOTES.matcher(cdata).matches())
279           {
2800             warn("The text element contains single quotes.");
281           }
282        }
2830    }
284  
285     private void validateSolution (String cdata)
286     {
2870       if (cdata != null)
288        {
2890          if (REGEX_VARIABLES.matcher(cdata).matches())
290           {
2910(2)            warn("The solution element MUST NOT use variables: " + cdata);
292           }
293        }
2940    }
295  
296     private void validateDescription (String cdata)
297     {
2980       if (cdata != null)
299        {
3000          if (REGEX_VARIABLES.matcher(cdata).matches())
301           {
3020             warn("The solution element MUST NOT use variables: " + cdata);
303           }
304        }
3050    }
306  
307     private void validateProcedure (String cdata)
308     {
3090       if (cdata != null)
310        {
3110          if (REGEX_VARIABLES.matcher(cdata).matches())
312           {
3130             warn("The solution element MUST NOT use variables: " + cdata);
314           }
315        }
3160    }
317  
318     private void validateValidation (String cdata)
319     {
3200       if (cdata != null)
321        {
3220          if (REGEX_VARIABLES.matcher(cdata).matches())
323           {
3240             warn("The solution element MUST NOT use variables: " + cdata);
325           }
326        }
3270    }
328  
329     private void warn (String message)
330     {
3310       mWarningMessages.add("[" + mCurrentAppName + "."
332              + mCurrentGrpName + "." + mCurrentMsgName + "] " + message);
3330    }
334  
335     private void reset ()
336     {
3370       mBuffer.setLength(0);
3380       mCaptureCharacters = false;
3390       mCurrentAppName = EMPTY_STRING;
3400       mCurrentGrpName = EMPTY_STRING;
3410       mCurrentMsgName = EMPTY_STRING;
3420       mWarningMessages.clear();
3430       mCurrentAppId = 0;
3440       mCurrentGrpId = 0;
3450       mCurrentMsgId = 0;
3460    }
347  
348     private static class NamedMap
349     {
3500       private final Map mMap = new HashMap();
351        private final String mName;
352  
353        public NamedMap (String name)
3540       {
3550          mName = name;
3560       }
357  
358        public String getName ()
359        {
3600          return mName;
361        }
362  
363        public boolean contains (int id)
364        {
3650          return mMap.containsKey(new Integer(id));
366        }
367  
368        public NamedMap getApp (int id)
369        {
3700          return (NamedMap) mMap.get(new Integer(id));
371        }
372  
373        public void registerApplication (int id, String appName)
374        {
3750          mMap.put(new Integer(id), new NamedMap(appName));
3760       }
377  
378        public void addApplication (int id, String appName)
379              throws AppInfoException
380        {
3810          if (!contains(id))
382           {
3830             throw new AppInfoException(
384                    "Application " + appName + " with the identifier "
385                    + id + " is not registered. "
386                    + "Registered applications are " + mMap);
387           }
3880       }
389  
390  
391        public NamedMap getGrp (int id)
392        {
3930          return (NamedMap) mMap.get(new Integer(id));
394        }
395  
396        public void addGroup (int id, String groupName)
397              throws AppInfoException
398        {
3990          if (contains(id))
400           {
4010             final String registeredGrpName
402                    = ((NamedMap) mMap.get(new Integer(id))).getName();
4030             throw new AppInfoException("The group " + groupName
404                    + " with the id " + id
405                    + " is already assigned to " + registeredGrpName + ".");
406           }
4070          mMap.put(new Integer(id), new NamedMap(groupName));
4080       }
409  
410        public void addMessage (int id, String messageName)
411              throws AppInfoException
412        {
4130          if (contains(id))
414           {
4150             final String registeredMsgName = (String) mMap.get(new Integer(id));
4160             throw new AppInfoException("The message " + messageName
417                    + " with the id " + id
418                    + " is already assigned to " + registeredMsgName + ".");
419           }
4200          mMap.put(new Integer(id), messageName);
421  
4220       }
423  
424        public String toString ()
425        {
4260          final StringBuffer sb = new StringBuffer();
4270          sb.append(mName);
4280          sb.append(' ');
4290          sb.append(mMap);
4300          return sb.toString();
431        }
432     }
433  
434  
435     private static class AppInfoException
436           extends Exception
437     {
438        private static final long serialVersionUID = 1L;
439  
440        public AppInfoException (String msg)
441        {
4420          super(msg);
4430       }
444  
445        public AppInfoException (String msg, Throwable cause)
446        {
4470          super(msg, cause);
4480       }
449     }
450  
451  }

Findings in this File

w (1) 124 : 0 method org.jcoderz.commons.taskdefs.AppInfoSaxHandler.warning(SAXParseException) is implemented with an exact copy of it's superclass's method
i (2) 291 : 18 The String literal "The solution element MUST NOT use variables: " appears 4 times in this file; the first occurrence is on line 291