Package vmcreative.injection

Source Code of vmcreative.injection.HTMLInjectionTarget$HTMLTargetField

Copyright 2009 VM Creative PTY LTD

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.



package vmcreative.injection;

import java.io.File;
import java.io.IOException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import vmcreative.htmlparser.HTMLParser;
import vmcreative.htmlparser.ParseException;
import vmcreative.htmlparser.Tag;


public class HTMLInjectionTarget implements InjectionTarget {
 
  private List positionsList = new ArrayList();
  private String trailingData;
 
  public HTMLInjectionTarget(String html) throws ParseException, IOException {
 
      HTMLParser htmlparser = new HTMLParser(html, new String[]{"iwindow", "igroup", "irow"});
   
    parse(htmlparser);
  }
 
  public HTMLInjectionTarget(File htmlFile) throws ParseException, IOException {
   
   
    // Parse the html document and translate tags into target positions
   
    HTMLParser htmlparser = new HTMLParser(htmlFile, new String[]{"iwindow", "igroup", "irow"});
   
    parse(htmlparser);
   
  }
 
  private void parse(HTMLParser htmlparser) throws ParseException {
      List tagList;
      int size;
      Tag lastTag;
   
    tagList = htmlparser.getTagList();
    size = tagList.size();
   
    if(size == 0) {
      this.trailingData = htmlparser.getText(0, htmlparser.getHTMLLength()-1);
      return;
    }
    // Add all the fields to the html tree as tags
    for(int i = 0; i < size; i++) { 
      parseTag(htmlparser, (Tag)tagList.get(i));
    }
   
    // Convert the html tags into injection targets
    size = tagList.size();
    Tag tag;
    for(int i = 0; i < size; i++) { 
      tag = (Tag)tagList.get(i);
        if(tag.getName().equalsIgnoreCase("irow")) {
          this.positionsList.add(new HTMLTargetRow(htmlparser, null, tag));
      } else if(tag.getName().equalsIgnoreCase("iwindow")) {
        this.positionsList.add(new HTMLTargetWindow(htmlparser, null, tag));
      } else {
        this.positionsList.add(new HTMLTargetGroup(htmlparser, null, tag));
      }
    }
   
    // All the text after the last tag
    lastTag = (Tag)tagList.get(size - 1);
   
    if(lastTag.getEndTag() == null) {
      this.trailingData = htmlparser.getText(lastTag.getEndPosition()+2, htmlparser.getHTMLLength()-1);
    } else {
      this.trailingData = htmlparser.getText(lastTag.getEndTag().getEndPosition()+1, htmlparser.getHTMLLength()-1);
    }
   
    this.trailingData = stripTag(this.trailingData, "igroup");
  }
  /*
  private void parseTag(HTMLParser htmlparser, List tagList, int index) {
    // Get the tag
    Tag tag = (Tag)tagList.get(index);
    char[] chars = htmlparser.getInternalArray();
    List newTags = new ArrayList();
    Map attributes;
   
    // Parse tag into new tags
    //System.out.println("parsing tag");
   
   
    // While pos isn't end of tag Search for label start
   
   
   
    int end = tag.getEndTag().getStartPosition() - 1;
    int i = tag.getEndPosition() + 1;
    int tagStart;
    while(i < end) {
      // If found find end of label and create tag
      if(chars[i] == '#') {
        tagStart = i;
        i++;
        while(isLabelChar(chars[i])) {
          i++;
        }
        Tag t = new Tag(htmlparser.getText(tagStart+1, i-1), false, tag.getAttributes(), tagStart, i-2);
          tag.addChildTag(t);
        //newTags.add(t = new Tag(htmlparser.getText(tagStart+1, i-1), false, tag.getAttributes(), tagStart, i-2));
          //System.out.println(t);
      }     
      i++;
    }
       
    // Remove or adjust the tag
    //if(tag.getName().equalsIgnoreCase("igroup")) {
    //  tagList.remove(index);
    //} else {
      index++; 
    //}
   
    // Insert new tags
    //tagList.addAll(index, newTags);
    // System.out.println(tagList);
  }
  */
 
  private void parseTag(HTMLParser htmlparser, Tag tag) throws ParseException {
    int start = tag.getEndPosition()+1;
    int pos = start;
      int endTagPos = getEndOfTag(tag);
    int endPos = endTagPos;
      int nextChild = 1;
    Tag childTag;
    List fieldTags;
   
    while(pos < endTagPos) {
      if(tag.getChildCount() >= nextChild) {
          childTag = tag.getChildTag(nextChild - 1);
        endPos = childTag.getStartPosition() - 1 ;
      } else {
        endPos = getEndOfTag(tag);
        childTag = null;
      }
     
      fieldTags = parseFieldTags(htmlparser, pos, endPos);
           
      // Insert child tags
      if(!fieldTags.isEmpty()) {
        tag.insertTags(fieldTags, nextChild-1);
        nextChild += fieldTags.size();
      }
                 
      // If the child tag has its own children parse those
      if(childTag != null) parseTag(htmlparser, childTag);
     
      // Increment
      try {
        pos = childTag == null ? endPos : childTag.getEndTag().getEndPosition() + 1;
      } catch(NullPointerException q) {
        throw new ParseException("ParseException: Tag: <" + childTag.getName() + "> has no maching end tag.");
      }
      nextChild++;
    }
  }
 
  private List parseFieldTags(HTMLParser htmlparser, int start, int end) {
    List fieldList = new ArrayList();
    char[] chars = htmlparser.getInternalArray();
      int i = start;
      int tagStart;
     
    // Loop through characters between start and end;
    while(i < end) {
         
          if(chars[i] == '#') {  // See if this char is start of tag
              tagStart = i;
              i++;
              while(isLabelChar(chars[i])) {
                  i++;
              }
       
        int endOfTag = chars[i] == '#' ? i : i - 1; // Set end of tag correctly
               
       
              Tag t = new Tag(htmlparser.getText(tagStart+1, i-1), false, null, tagStart, endOfTag);
              fieldList.add(t);
               
          }          
          i++;
      }
   
    return fieldList;
  }
 
  private int getEndOfTag(Tag tag) {
    if(tag.getEndTag() == null) return tag.getEndPosition();
   
    return tag.getEndTag().getStartPosition();
  }
 
  /*
  private int indexOf(char[] chars, char chr, int searchStart, int searchEnd) {
    int i = searchStart;
   
    while(i < searchEnd && chars[i] != chr) i++;
   
    return chars[i] == chr;
  }
  */
  private boolean isLabelChar(char chr) {
    return Character.isLetterOrDigit(chr) || chr ==  '_'
  }
 
  private String stripTag(String str, String tagName) {
    return str.replaceAll("</?" + tagName + ".*>", "");
  }
 
  private void addTarget(HTMLParser htmlparser, HTMLTargetGroup parentGroup, Tag tag) {
   
    // Create the new target position based on what the tag is
    TargetPosition targetPosition;
   
     if(tag.getName().equalsIgnoreCase("irow")) {
       targetPosition = new HTMLTargetRow(htmlparser, parentGroup, tag);
     } else if(tag.getName().equalsIgnoreCase("iwindow")) {
         targetPosition = new HTMLTargetWindow(htmlparser, parentGroup, tag);
     } else if(tag.getName().equalsIgnoreCase("igroup")) {
         targetPosition = new HTMLTargetGroup(htmlparser, parentGroup, tag);
     } else {
         targetPosition = new HTMLTargetField(htmlparser, parentGroup, tag);
     }
    
     if(parentGroup != null) {
         parentGroup.addPosition(targetPosition);
     } else {
         this.positionsList.add(targetPosition);
     }
   
    /* Delete ---
   
    if(tag.getEndTag() == null) {
      // Its a field tag
      parentGroup.addPosition(new HTMLTargetField(htmlparser, parentGroup, tag)); 
    } else {
      // Group
      parentGroup.addPosition(new HTMLTargetGroup(htmlparser, parentGroup, tag));
    }
    */
  }
 
  // Implemented methods --------------
 
  public int getPositionCount() { return this.positionsList.size(); }
  public TargetPosition getPosition(int positionNo) { return (TargetPosition)this.positionsList.get(positionNo); }
  public String getTrailingData() { return this.trailingData; }

 


  // Inner classes ---------------
 
 
  private class HTMLTargetWindow extends HTMLTargetGroup implements TargetWindow{
    private String trailingData = "";
   
      public HTMLTargetWindow(HTMLParser htmlparser, HTMLTargetGroup parentGroup, Tag tag) {
      super(htmlparser, parentGroup, tag);
     
      // CHECK THIS!
     
      if(getPositionCount() == 0) {
        this.trailingData = htmlparser.getText(tag.getEndPosition() + 1, tag.getEndTag().getStartPosition()-1);
      } else {
        List childtags = tag.getChildTags();
       
        Tag lastChildTag = (Tag)childtags.get(childtags.size()-1);
   
          int start = lastChildTag.getEndTag() == null ? lastChildTag.getEndPosition() + 1 : lastChildTag.getEndTag().getEndPosition() + 1;
          this.trailingData = htmlparser.getText(start, tag.getEndTag().getStartPosition()-1);      }
    }
   
      public int getType() { return InjectionStreamer.WINDOW; }
    public String getTrailingData() { return this.trailingData; }
  }
 
  private class HTMLTargetRow extends HTMLTargetGroup implements TargetRow {
    public HTMLTargetRow(HTMLParser htmlparser, HTMLTargetGroup parentGroup, Tag tag) {
      super(htmlparser, parentGroup, tag);
    }

    public int getPageLimit() {
      return -1;
    }
   
    public int getType() { return InjectionStreamer.ROW; }
  }

  private class HTMLTargetGroup implements TargetGroup {
    // Variables ----------------
    private String trailingData = "";
    private String leadingData;
    private String name;
    private List subPositions = new ArrayList();
   
    public HTMLTargetGroup(HTMLParser htmlparser, HTMLTargetGroup parentGroup, Tag tag) {
      int startPos;
        Tag previousTag = tag.getPreviousTag();
     
      // Get leading data
      if(previousTag == null) {
        Tag parentTag;
       
        parentTag = tag.getParent(); // See if there is a parent
       
        if(parentTag != null ) {
          startPos = parentTag.getEndPosition() + 1;
            this.leadingData = htmlparser.getText(startPos, tag.getStartPosition()-1);
        } else {
          // This is the first node so the leading data is everything from the beginining of the doc
          this.leadingData = htmlparser.getText(0, tag.getStartPosition()-1);
         
        }
      } else {
       
        if(previousTag.getEndTag() == null) {
          startPos = previousTag.getEndPosition() + 1;
        } else {
          startPos = previousTag.getEndTag().getEndPosition() > tag.getStartPosition() ?
            previousTag.getEndPosition()
            : previousTag.getEndTag().getEndPosition();
         
          startPos ++;
        }

        this.leadingData = htmlparser.getText(startPos, tag.getStartPosition()-1);         
      }
     
            
      //this.leadingData = stripTag(this.leadingData, "igroup"); 
      this.name = tag.getAttribute("name");
     
      // Process child tags
      Tag childTag;
      int childCount = tag.getChildCount();
     
      for(int i = 0; i < childCount; i++) {
        addTarget(htmlparser, this, tag.getChildTag(i));
      }
     
      // Set trailing data 19/01/08
       if(getPositionCount() == 0) {
           this.trailingData = htmlparser.getText(tag.getEndPosition() + 1, tag.getEndTag().getStartPosition()-1);
       } else {
           List childtags = tag.getChildTags();
          
           Tag lastChildTag = (Tag)childtags.get(childtags.size()-1);
        
         //if(getType() == InjectionStreamer.FIELD) {
        
           int start = lastChildTag.getEndTag() == null ? lastChildTag.getEndPosition() + 1 : lastChildTag.getEndTag().getEndPosition() + 1;
           this.trailingData = htmlparser.getText(start, tag.getEndTag().getStartPosition()-1);
         //}
       }
    }
           
    // Implemented methods ---------------
    public String getTrailingData() { return this.trailingData; }
    public String getLeadingData()  { return this.leadingData; }
    public String getName() { return this.name; }
    public int getType() { return InjectionStreamer.GROUP; }
   
    public String toString() { return "Leading Data: {" + this.leadingData + "} Name: {" + this.name + "} Type: {" + getType() + "}"; }

    private void addPosition(TargetPosition targetPosition) {
      this.subPositions.add(targetPosition);
    }


    public int getPositionCount() { return this.subPositions.size(); }

    public TargetPosition getPosition(int index) { return (TargetPosition)this.subPositions.get(index); }
/*
    public int getStart() {
      return 0;
    }

    public int getEnd() {
      return 0;
    }
*/
  }

  private class HTMLTargetField implements TargetField {
    private String name;
    private String leadingData;
    private String trailingData;
    private TargetGroup targetGroup;
    private int start;
    private int end;
   
      public HTMLTargetField(HTMLParser htmlparser, HTMLTargetGroup parentTarget, Tag tag) {
      // Set tag name
      Tag parentTag = tag.getParent();
      Tag previousTag = tag.getPreviousTag();
      Tag nextTag = tag.getNextTag();
      this.name = tag.getName();
     
      // Set tag parent
     
   
      // Set leading data
      if(previousTag == null) {
        if(parentTag == null) {
          this.start = 0;   // this should be impossible for field... should rewrite this bit
        } else {
            this.start = parentTag.getEndPosition()+1;
           
        }
      } else if(previousTag.getEndTag() == null) {
          this.start = previousTag.getEndPosition() + 1;
      } else {
          this.start = previousTag.getEndTag().getEndPosition() > tag.getStartPosition() ? previousTag.getEndPosition() : previousTag.getEndTag().getEndPosition();
        this.start +=1;
      }
     
      if(this.start < tag.getStartPosition()) {
          this.leadingData = htmlparser.getText(this.start, tag.getStartPosition()-1);

      } else {
          this.leadingData = "";
      }
           
      // If last tag on level set trailing data
      /* 19/1/08
      if(nextTag == null && !(tag.getEndPosition()+2 >= parentTag.getEndTag().getStartPosition()-2)) {
        this.trailingData = htmlparser.getText(tag.getEndPosition()+2, parentTag.getEndTag().getStartPosition()-1);
      } else {
        this.trailingData = "";
      }
      */
     
    }

    public String getName() { return this.name; }
    public String getLeadingData() { return this.leadingData; }
    public String getTrailingData() { return this.trailingData; }
    public TargetGroup getTargetGroup() { return this.targetGroup; }
   
    public int getType() { return InjectionStreamer.FIELD; }
    public int getStart() { return this.start; }
    public int getEnd() { return this.end; }
   
  }
}
TOP

Related Classes of vmcreative.injection.HTMLInjectionTarget$HTMLTargetField

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.