package org.greenstone.gsdl3.gs3build.util; public class HTMLTag implements HTMLObject { String tagtext; String name; int docpos; int endpos; public static final String HTML_ELEMENT_TYPE = "Element"; public HTMLTag(String tagtext, int docpos, int endpos) { this.tagtext = tagtext; this.name = null; this.docpos = docpos; if (this.docpos < 0) { this.endpos = -1; } else { this.endpos = endpos; } } public HTMLTag(String tagtext) { this.tagtext = tagtext; this.docpos = -1; this.name = null; } public String getHTMLType() { return HTML_ELEMENT_TYPE; } public HTMLTag endTag() { HTMLTag end; String endtext; endtext = ""; end = new HTMLTag(endtext); return end; } public boolean tagIsOpening() { return (this.tagtext.charAt(1) != '/'); } /** * Tells you if a tag is a singleton tag - TODO: complete list */ public boolean tagIsSingleton() { String tagName = this.tagName(); return (tagName.equals("img")); } /** * @return: the name of a tag including any leading '/' */ public String tagName() { int offset, start, end; String reply; if (this.name != null) { return this.name; } // NB: a starting '<' and an ending '>' are guaranteed offset = 1; // skip < while (this.tagtext.charAt(offset) <= 32 || this.tagtext.charAt(offset) == 127) { offset ++; } start = offset; while (this.tagtext.charAt(offset) > 32 && this.tagtext.charAt(offset) != '>' && this.tagtext.charAt(offset) != 127) { offset ++; } end = offset; reply = this.tagtext.substring(start, end); reply = reply.toLowerCase(); this.name = reply; return reply; } // Returns the name of the tag *minus* any leading / for // closing tags public String xtagName() { String reply; reply = this.tagName(); if (reply == null || reply.length() == 0) { return null; } if (reply.charAt(0) == '/') { if (reply.length() > 1) { reply = reply.substring(1, reply.length()); } else { reply = ""; } } return reply; } public String endTagName() { String reply; reply = "/" + xtagName(); return reply; } public boolean isClosing() { String name; name = this.tagName(); if (name.charAt(0) == '/') { return true; } return false; } public int tagLevel() { String name; int level; level = 7; name = this.xtagName(); if (name.charAt(0) == 'h') { level = Integer.parseInt(name.substring(1, 2)); } return level; } public int startPos() { return this.docpos; } public int endPos() { return this.endpos; } static public boolean isTag(String text) { if (text.charAt(0) == '<') { return true; } return false; } public String toString() { return this.tagtext; } // // Returns the value of the identifier given as a string // public String idValue(String idName) { int offset, start, end; int idstart, idend, valuestart, valueend; boolean isvalue, isquoted, isstopped; char quotechar; String reply; // NB: a starting '<' and an ending '>' are guaranteed offset = 1; // skip < while (this.tagtext.charAt(offset) <= 32 || this.tagtext.charAt(offset) == 127) { offset ++; } start = offset; while (this.tagtext.charAt(offset) > 32 && this.tagtext.charAt(offset) != '>' && this.tagtext.charAt(offset) != 127) { offset ++; } end = offset; // End of tag name if (this.tagtext.charAt(offset) == '>') { return null; } isstopped = false; while (!isstopped) { isvalue = false; isquoted = false; valuestart = -1; valueend = -1; while (this.tagtext.charAt(offset) <= 32 || this.tagtext.charAt(offset) == 127) { offset ++; } idstart = offset; if (this.tagtext.charAt(offset) == '>') { break; } while (this.tagtext.charAt(offset) > 32 && this.tagtext.charAt(offset) != '=' && this.tagtext.charAt(offset) != '>' && this.tagtext.charAt(offset) != 127) { offset ++; } idend = offset; while (this.tagtext.charAt(offset) <= 32 || this.tagtext.charAt(offset) == 127) { offset ++; } if (this.tagtext.charAt(offset) == '=') { isvalue = true; quotechar = ' '; offset ++; while (this.tagtext.charAt(offset) <= 32 || this.tagtext.charAt(offset) == 127) { offset ++; } if (this.tagtext.charAt(offset) == '"' || this.tagtext.charAt(offset) == '\'') { quotechar = this.tagtext.charAt(offset); isquoted = true; offset ++; } valuestart = offset; while (offset < this.tagtext.length() && (isquoted && this.tagtext.charAt(offset) != quotechar) || (!isquoted && this.tagtext.charAt(offset) > 32 && this.tagtext.charAt(offset) != '>')) { offset ++; } valueend = offset; } if (offset >= this.tagtext.length() || this.tagtext.charAt(offset) == '>') { isstopped = true; } offset ++; reply = this.tagtext.substring(idstart, idend); reply = reply.toLowerCase(); if (reply.equalsIgnoreCase(idName)) { if (isvalue) { return this.tagtext.substring(valuestart, valueend); } else { return reply; } } } return null; } }