Login | Register
My pages Projects Community openCollabNet

Discussions > cvs > CVS update: MODIFIED: util ...

Project highlights: Architectural Overview

joist
Discussion topic

Hide all messages in topic

All messages in topic

CVS update: MODIFIED: util ...

Author ms
Full name zxcv
Date 2001-05-09 18:39:07 PDT
Message User: ms
  Date: 01/05/09 18:39:07

  Modified: java/org/joist/util TagValidator.java
  Log:
  changing from static util methods to instance methods. this allows faster execution and more ability to validate custom html tags in specific fields in forms
  
  Revision Changes Path
  1.9 +60 -105 joist/java/org/joist​/util/TagValidator.j​ava
  
  http://joist.tigris.​org/source/browse/jo​ist/java/org/joist/u​til/TagValidator.jav​a.diff?r1=1.8&r2​=1.9
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: TagValidator.java
  ====================​====================​====================​=======
  RCS file: /usr/local/tigris/da​ta/helm/cvs/reposito​ry/joist/java/org/jo​ist/util/TagValidato​r.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -b -r1.8 -r1.9
  --- TagValidator.java 2001/05/03 10:35:35 1.8
  +++ TagValidator.java 2001/05/10 01:39:07 1.9
  @@ -67,7 +67,7 @@
     * @author David C. Pellegrini <A HREF="mailto:davidp@​dataweb-systems.com"​>davidp@dataweb-s​ystems.com</A>​
     * @author Jon S. Stevens <A HREF="mailto:jon@lat​chkey.com">jon@la​tchkey.com</A>​
     * @author Michael Salmon <a href="mailto:ms@coll​ab.net">ms@collab​.net</a>
  - * @version $Revision: 1.8 $
  + * @version $Revision: 1.9 $
     */
   public class TagValidator
   {
  @@ -94,16 +94,16 @@
        "COLS","FACE","GUTTE​R","HEIGHT","HREF","​SIZE","START","TYPE"​,"WIDTH",
        "COMPACT","NOSHADE","NOWRAP"};
   
  - static final private String frontRegExp = "<\\s*/?\\s*(\\s*";
  - static final private String middleRegExp = "\\s*)\\s*\\b\\s*(\\s*(\\s*";
  - static final private String endRegExp = "\\s*)\\s*(\\s​*=\\s*\"?[^<​>\\s]+\"?\\s*)​?\\s*)*\\s*>"​;
  -
  - // to be used from addAllowTags(ArrayList)
  - static private ArrayList allowTags = null;
  -
  - static final private String orDelim = "|";
  - static final private String lt = "&lt;";
  - static final private String gt = "&gt;";
  + final private String frontRegExp = "<\\s*/?\\s*(\\s*";
  + final private String middleRegExp = "\\s*)\\s*\\b\\s*(\\s*(\\s*";
  + final private String endRegExp = "\\s*)\\s*(\\s​*=\\s*\"?[^<​>\\s]+\"?\\s*)​?\\s*)*\\s*>"​;
  +
  + // the tags to match with this object
  + private ArrayList allowTags = null;
  +
  + final private String orDelim = "|";
  + final private String lt = "&lt;";
  + final private String gt = "&gt;";
       
       // For substitution ...
       static final private PatternCompiler substCompiler = new Perl5Compiler();
  @@ -119,92 +119,30 @@
   
       static final private String matchRegExp = "</?[^<>\\s]*>";
   
  - /**
  - * Tests the operation of the methods.
  - * Takes one argument: the string to be validated
  - * @param args A variable of type String
  - */
  - public static final void main(String args[])
  - {
  - String input = null;
  - if (args.length == 0 )
  - input = "this is a <script> test";
  - else
  - input = args[0];
               
  - databaseTest();
  -
  - }
  -
  - //REM??
  - public static final void databaseTest()
  - {
  - String username = "";
  - String password = "";
  - String host = "";
  - String db = "";
  - String DRIVER_MM = "org.gjt.mm.mysql.Driver";
  - String url = "jdbc:mysql://" + host + "/" + db;
  - Statement stmt = null;
  - Connection conn = null;
  - try
  - {
  - String DB_CONNECTION = url +"?user="+ username + "&password="+password;
  - Class.forName( DRIVER_MM ).newInstance();
  - conn = DriverManager.getConnection( DB_CONNECTION );
  -
  - String[] tables = {"Developer","Evalua​tion","Interest",
  - "Milestone","Milesto​ne","Milestone", "Milestone",
  - "Proposal","Proposal​","Proposal",
  - "RFP","RFP","RFPComm​ent","Skill","Sponso​r",
  - "Wish","Wish","Wish"​,"WishComment"};
  -
  - String[] columns = {"achievements","eva​luation","statement"​,"deliverables","ann​ouncement",
  - "response","review",​"statement","deliver​ables","milestones",​"description",
  - "milestones","commen​t","name","name","na​me","description","d​eliverables","commen​t"};
  -
  - for ( int i=0; i<tables.length; i++ )
  - {
  - String sql = "select " + columns[i] + " from " + tables[i];
  - stmt = conn.createStatement();
  - ResultSet rs = stmt.executeQuery ( sql );
  - int rowID = 1;
  - while (rs.next())
  - {
  - StringBuffer errorMsg = new StringBuffer();
  - String input = rs.getString ( columns[i] );
  - if (input == null || input.length() == 0)
  - {
  -// System.out.println ("Row is empty: " + rowID );
  - }
  - else if ( ! validate(input) )
  - {
  -// System.out.println ("Error in row: " + rowID );
  - System.out.println ("RowID: " + rowID + " Table: " + tables[i] + " Column: " + columns[i]);
  - System.out.println ("Error Message:\n" + errorMsg.toString() + "\n" );
  - }
  - rowID++;
  - }
  - }
  - }
  - catch ( Exception e )
  + /**
  + * populate the validator with some tags to allow
  + */
  + public TagValidator (String[] tags)
           {
  - e.printStackTrace();
  - System.out.println (e.toString());
  + addAllowTags(tags);
           }
  - finally
  +
  + /**
  + * Constructor setting up a validator for ALL_TAGS
  + */
  + public TagValidator ()
           {
  - try { if (stmt != null) stmt.close(); } catch (Exception e){}
  - try { if (conn != null) conn.close(); } catch (Exception e){}
  - }
  + mergeTagLists();
       }
  +
       /**
           This method builds the regex for the pattern matching.
           We do this as a dynamic string because it makes it easier to
           add new items.
        * @return A variable of type String
       */
  - private static String buildRegex()
  + private String buildRegex()
       {
           mergeTagLists();
           StringBuffer sb = new StringBuffer();
  @@ -246,7 +184,7 @@
           This method builds an HTML representation of all of the allowed Tags
        * @return A variable of type String
       */
  - private static String buildError()
  + private String buildError()
       {
           StringBuffer sb = new StringBuffer();
           boolean firstTime = true;
  @@ -299,7 +237,7 @@
        * @param inputString A variable of type String
        * @return a boolean indicating whether the HTML was OK
        */
  - public static boolean validate (String inputString)
  + public boolean validate (String inputString)
       {
           // Bootstrap the first time through ...
           if (substPattern == null)
  @@ -337,6 +275,19 @@
       }
   
       /**
  + * Check input string for HTML with specific given tags only
  + *
  + * @param inputString The String to check for HTML
  + * @param tags The String array of tags to check for {"br","a"} for ex.
  + */
  + public boolean validate (String inputString, String[] tags)
  + {
  + setAllowTags(tags);
  + return validate(inputString);
  + }
  +
  +
  + /**
        * Check if there are any HTML tags in the string at all
        *
        * @param inputString - String to check
  @@ -385,8 +336,14 @@
       return unclean;
       }
   
  - public static void addAllowTags(String[] newTags)
  + /**
  + * Appends given tags to validator's list of tags to allow
  + *
  + * @param String[] newTags tags to match, form of {"h1","h2", etc..
  + */
  + public void addAllowTags(String[] newTags)
       {
  + substPattern = null;
           // let npe's pass if given a null list
           if (allowTags == null && newTags != null)
           {
  @@ -394,11 +351,20 @@
           }
           for (int i = 0; i < newTags.length; i++)
           {
  + System.err.print(" " + newTags[i]);
               allowTags.add(newTags[i]);
           }
  + System.err.println();
  + }
  +
  + public void setAllowTags (String[] newTags)
  + {
  + substPattern = null;
  + allowTags = null;
  + addAllowTags(newTags);
       }
       
  - private static void mergeTagLists()
  + private void mergeTagLists()
       {
           // only add ALL_TAGS if none are explicitly given
           if (allowTags == null)
  @@ -408,23 +374,12 @@
       }
   
       /**
  - * A way to add tags to allow list
  + * Clear out tags
        */
  - public static void addTagList(String[] extraTags)
  - {
  - initTags(extraTags.length);
  - for (int i = 0; i < extraTags.length; i++)
  - {
  - allowTags.add(extraTags[i]);
  - }
  - }
  -
  - private static void initTags(int size)
  + public void resetAllowTags()
       {
  - if (allowTags == null)
  - {
  - allowTags = new ArrayList (size);
  - }
  + substPattern = null;
  + allowTags = null;
       }
   
   }
  
  
  

--------------------​--------------------​--------------------​---------
To unsubscribe, e-mail: cvs-unsubscribe@jois​t.tigris.org
For additional commands, e-mail: cvs-help at joist dot tigris dot org
Messages per page: