Results 1 to 10 of 10
  1. #1
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default Need help in completing this code

    Hi, I have written a code which will identify all the supposedly HTML Tags in the input string & put them in new string e.g. ABC.

    [ code] package com.test;

    import java.util.ArrayList;

    class Test {

    /**
    * @param args
    */
    public static void main(String[] args) {

    String Demo = "<h1>This is a string </h1>that we </br> want to ttest";
    try{
    ArrayList result = token(Demo );

    // System.out.println("Hi");

    for(int i=0; i<result.size();i++)
    System.out.println("" + result.get(i));
    }
    catch(Exception e){
    System.out.println("Wrong size of token");
    }
    }


    public static ArrayList token(String test) throws Exception
    {

    ArrayList result= new ArrayList();
    StringBuffer tt = new StringBuffer();
    boolean flag =false;
    Exception e = new Exception();
    for(int i=0; i<test.length();i++)
    {
    // System.out.println("flag "+flag);


    if(test.charAt(i)=='<')
    {
    //System.out.println("in "+test.charAt(i));
    tt = new StringBuffer();
    flag= true;
    continue;
    }
    if(test.charAt(i)=='>')
    {
    flag=false;
    if(test.length()!=0)
    {
    result.add(tt.toString());
    if(tt.length()>6)
    {

    throw new Exception(e);
    }
    tt = new StringBuffer();

    }
    continue;
    }
    if(flag)
    {
    tt.append(String.valueOf(test.charAt(i)));
    //System.out.println("tt "+tt.toString());
    }
    }
    return result;
    }
    } [ /code]


    Now I want to define a allowable set of HTML tags either as a hashmap or Set and then compare if the generated string ABC is part of this allowable set. If not then throw a validation error.
    Allowable set will contain tags like <b>,<strong>,<p>,<br> and this code should allow input text to contain tags only within this set and not allow tags like <javascript>
    Last edited by nn12; 09-08-2008 at 08:37 PM. Reason: used codetags as per request

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Please use codetags
    Java Code:
    [ code] text [ /code]
    After that, i'll have a look at that...
    I die a little on the inside...
    Every time I get shot.

  3. #3
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default

    added code tags

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    After you added the code tags, did the formatting look any different? Try again, use upper case: CODE and remove spaces inside the [ ].

  5. #5
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Quote Originally Posted by nn12 View Post
    added code tags
    Quote Originally Posted by Norm View Post
    After you added the code tags, did the formatting look any different? Try again, use upper case: CODE and remove spaces inside the [ ].
    Okay, nn12, couldn't you seriously find that out for yourself... ?
    I die a little on the inside...
    Every time I get shot.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    He's cross-posted the same stuff in the forums.sun.com forums.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    After you added the code tags, did the formatting look any different? Try again, use upper case: CODE and remove spaces inside the [ ].
    Actually addition spaces is the reason that not to formatted the code correctly. You can use lower case letters as well.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Use a Java collocation to store initial tags. Then compare it with the newly finding tags in your string. So where are you stuck with?

  9. #9
    nn12 is offline Member
    Join Date
    Sep 2008
    Posts
    85
    Rep Power
    0

    Default

    Hi, i have indented the code properly for your ease.
    My questions are as below:
    1)How should I store my new found tags, at line no.19. An example would be really helpful
    2)What should the best option to store allowable tags & how should I compare it.
    Many thanks in advance

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by nn12 View Post
    Hi, i have indented the code properly for your ease.
    My questions are as below:
    1)How should I store my new found tags, at line no.19. An example would be really helpful
    2)What should the best option to store allowable tags & how should I compare it.
    Many thanks in advance
    As I said the best thing is use one of a collection in Java. May be an ArrayList. So you can easily compare elements on each other. Just try it first.

Similar Threads

  1. Completing A Program..
    By Louise1875 in forum New To Java
    Replies: 1
    Last Post: 05-11-2008, 05:24 AM
  2. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 10:12 PM
  3. Here is my code, can anyone here help me?
    By Java_Man in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 02:41 AM
  4. tic tac toe code
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-23-2007, 04:36 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •