[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

Parse tag in string

I am looging for a utility to purge a tag from a string.

Example: "Today's date is <DATE FORMAT=MM/DD/YYYY>."

I need an object to store the tag type (Date) and the different properties and their values.

Can anyone help me?

Thanks!!!
0
afpcos
Asked:
afpcos
1 Solution
 
Igor BazarnyCommented:

Well, can't you use java.text formats?
0
 
rwinklerCommented:
I don't know how closely you're expecting your data to be like XML, but JDOM might be a good place to look.  http://www.jdom.org
0
 
rashidkamranbsCommented:
Well not 100% but StreamTokenizer could be a helpful utility to purify the date tag from the string..

0
 
OviCommented:
Here is a variant of your parser.

import java.util.*;

public class Tester {
  private String text;
  Vector tags;

  public Tester(String txt) {
    text = txt;
    tags = new Vector();
  }

  public void setText(String txt) {
    text = txt;
    tags = new Vector();
  }

  public void parse() {
    int idxBegin = -1;
    while((idxBegin = text.indexOf("<", idxBegin + 1)) != -1) {
      int idxEnd = text.indexOf(">", idxBegin);
      if(idxEnd != -1) {
        Tag tag = parseTag(text.substring(idxBegin + 1, idxEnd));
     if(tag != null)
       tags.addElement(tag);
      } else return;
    }
  }

  public Tag parseTag(String tagText) {
    Tag tag = new Tag();
    int spaceIdx = tagText.indexOf(" ");
    if(spaceIdx != -1) {
      tag.name = tagText.substring(0, spaceIdx);
      tagText = tagText.substring(spaceIdx + 1);
      int eqIdx = -1;
      while((eqIdx = tagText.indexOf("=")) != -1) {
        String attr = tagText.substring(0, eqIdx);    
     tagText = tagText.substring(eqIdx + 1).trim();
     int nextEq = tagText.indexOf("=");
     if(nextEq == -1) {
       tag.attributes.put(attr, tagText);
       break;
     } else {
       String tmp = tagText.substring(0, nextEq);
       spaceIdx = tmp.lastIndexOf(" ");
       if(spaceIdx != -1) {
         String attrValue = tagText.substring(0, spaceIdx);
         tag.attributes.put(attr, attrValue);
         tagText = tagText.substring(spaceIdx).trim();
       } else {
         tag.attributes.put(attr, tmp);
       }
     }
      }
      return(tag);
    }
    return(null);
  }
 
  public void dump() {
    System.out.println("Text : " + text);
    for(int i = 0; i<tags.size(); i++) {
      ((Tag) tags.elementAt(i)).dump();
    }
  }

  class Tag {
    public String name;
    public Hashtable attributes;

    public Tag() {
      name = "";
      attributes = new Hashtable();
    }
   
    public void dump() {
      System.out.println("******************Tag *************");
      System.out.println("Name : " + name);
      System.out.println("Attributes : ");
      Enumeration attrs = attributes.keys();
      while(attrs.hasMoreElements()) {
        String key = attrs.nextElement().toString();
     String value = attributes.get(key).toString();
     System.out.println("Attribute : " + key + "; Value : " + value);
      }
      System.out.println("***********************************");
    }
  }

  public static void main(String[] args) {
    Tester t = new Tester("Today's date is <DATE FORMAT=MM/DD/YYYY>");
    t.parse();
    t.dump();
    t.setText("Today's date is <DATE FORMAT=MM/DD/YYYY>; tomorrow will be <DATE FORMAT=DD/MM/YY VALUE=04/04/02>");
    t.parse();
    t.dump();
    t.setText("Today  <PERSON NAME=aName AGE=70 SEX=M>; tomorrow will be  <PERSON NAME=bName AGE=90 SEX=F>");
    t.parse();
    t.dump();
  }
}
0
 
OviCommented:
Forgot to answear ... :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now