Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. package HTMLTAGS;
  2.  
  3. import java.io.IOException;
  4. import java.util.Scanner;
  5. import java.util.Set;
  6. import java.util.HashSet;
  7. import java.util.Stack;
  8.  
  9. import htmlSupport.*;
  10.  
  11. public class HTML4 {
  12.         private static Set<String> optionalTags = new HashSet<String>();
  13.        
  14.         private static void populateOptionalTags() {
  15.             String [ ] optionals = {"p", "li", "html", "body", "tr", "td"};
  16.             for (int i=0; i<optionals.length; i++)
  17.                 optionalTags.add(optionals[i]);
  18.         }
  19.        
  20.         private static boolean isOptional(CSC300HTMLTag tag) {
  21.             return optionalTags.contains(tag.getName());
  22.         }
  23.        
  24.         public static void main(String[] args) throws IOException {
  25.             Stack<CSC300HTMLTag> startTags = new Stack<CSC300HTMLTag>();
  26.             Scanner console = new Scanner(System.in);
  27.             CSC300HTMLTag nextTag, poppedTag;
  28.             populateOptionalTags();
  29.             System.out.println("Enter a URL");
  30.             String url = console.next();
  31.             HTMLTokenizer tokenizer = new HTMLTokenizer(url);
  32.            
  33.             while (tokenizer.hasNext()) {
  34.                 nextTag = tokenizer.next();
  35.                 if (nextTag.getType() == CSC300HTMLTag.START) {
  36.                     startTags.push(nextTag);
  37.                     //System.out.println("Tag Pushed");
  38.                     // push the name of the tag onto the stack
  39.                 }
  40.                 else if (nextTag.getType() == CSC300HTMLTag.SIMPLE){
  41.                     //System.out.println("SIMPLE");
  42.                     continue;
  43.                 }
  44.                
  45.                 else {
  46.                     if(startTags.peek().getName().equals(nextTag.getName())){
  47.                     poppedTag = startTags.pop();
  48.                    
  49.                     //System.out.println("Valid so far");
  50.                     }
  51.                     else if(optionalTags.contains(startTags.peek().getName())){
  52.                         while(optionalTags.contains(startTags.peek().getName())){
  53.                         //System.out.println("HTML CODE IS VALID/BALANCED STUCK HERE");
  54.                         startTags.pop();
  55.                         //Was getting an empty stack exception so used the line below to fix that.
  56.                         if (startTags.isEmpty()){
  57.                             System.out.println("Code is Valid");
  58.                             return;
  59.                         }
  60.                         }
  61.                     }
  62.                     else {
  63.                         System.out.println("HTML CODE IS NOT VALID 1");
  64.                        
  65.                         return;
  66.                     }
  67.                     if (startTags.isEmpty()){
  68.                     System.out.println("HTML CODE IS NOT VALID 2");return;}            
  69.                                                                                            
  70.                     // if the tag names of nextTag and poppedTag are the same,
  71.                     // continue
  72.  
  73.                     // otherwise, if poppedTag doesn't require an end tag,
  74.                     // keep popping the stack until:
  75.                     //
  76.                     // (1) you find a matching tag (then HTML is valid so far)
  77.                     // (2) hasNext returns false (then HTML is not valid)
  78.                     // (3) the stack becomes empty (then HTML is not valid)
  79.                 }      
  80.                    
  81.             }
  82.             System.out.println("HTML CODE IS VALID");
  83.         }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement