<img src="xyz001.jpg" alt="xyz image" align="right">
<g n="index">l</g>
The tag, <g>
, is not an HTML tag, but is our very own tag that our server-side script recognises.Let's examine the various parts of our tag for this example:<g> </g>
n="index"
<g n="index">l</g>
'l'
between the <g></g>
is an alignment indicator which asks the image to align left ('l'
) or right ('r'
).<img>
tag.Just a small clarification here, that i decided to add after reading some of
the comments below :<g>
in this example, some people might consider that too cryptic, I could have very well used something like <image name="x">left</image>
, The parser described below will handle something like that by just changing the input parametersX company reached IPO on Feb 14th , but the CEO was disbarred from attending the conference by the police as he had 6 armsThe parsing program goes through this and transforms the<g n="CEO">l</g>
. But federal investigator Mr. Mulder came up with an alternate theory…. Blah… blah… Blah… blah… Blah… blah… Blah… blah… Blah… blah…which conclusively<g n="Mulder">r</g>
proved that the CEO was an alien.
<g>
tags to meaningful HTML <img>
tags with the correct image name, which is then rendered in the browser, so the converted output would be:X company reached IPO on Feb 14th, but the CEO was disbarred from attending the conference by the police as he had 6 armsThe two main components that accomplish this are:<img src="path/ceo.180x100.jpg" align="left" alt="CEOs at Lunch">
. But federal investigator Mr. Mulder came up with an alternate theory…. Blah… blah… Blah… blah… Blah… blah… Blah… blah… Blah… blah… which conclusively<img src="path/mulder.jpg" align="right" alt="Agent Mulder">
proved that the CEO was an alien.
<g n='indexname'>
.Img_index | Image | Image_AltText | Image_desc |
---|---|---|---|
Fishcatch | 8.jpg | Fish catch at hemingway | Fishing for sailfish at hemingways in the bay of biscay |
Ceo | Ceo180x100.jpg | Mr. Ceo Laughing | CEO of X corporation, Mr CEO |
<g>
tags, but also a variety of other custom tags. I have one for handling links and one for embedded tables. So it is a generic parser in Java which has generic routines from which other specialised parsers were extended.<g>
tag, it extracts the tag attributes.n=""
to locate the image name in the image store.<g>
and </g>
(the "l" or "r") is used to set the align="right"
or align="left"
attributes for the <img>
tag.<img>
tag is finally built.<g></g>
tags in the content string with the <img>
tag.<g></g>
tags and the same process happens again.<img>
tag).public class baseParser{private int nPreParseLength =0 ;private String strBeginp;private String strEndp;private String strRefp;private String str;private int lnkBeginLength;private int lnkRefLength;private int lnkEndLength;//these 2 variables are used to keep track of tag position in the content string//during an iterative scan through the content stringprivate int m_nLastIndex=0;private int m_nPrevIndex=0;public baseParser(){strBeginp="";strEndp="";strRefp="";str="";lnkBeginLength=0;lnkRefLength=0;lnkEndLength=0;}//String toBeParsed - string with content+custom tags which requires parsing//beginP - beginning tag e.g. <g//endP - ending tag e.g. </g>//refP - ref.attribute tag n="public baseParser(String toBeParsed, String beginP, String endP, String refP){str = toBeParsed;strBeginp = beginP;strEndp = endP;strRefp = refP;lnkBeginLength = strBeginp.length();lnkRefLength = strRefp.length();lnkEndLength = strEndp.length();} //iterator function which scans for tags sequentiallypublic String parseUnit(int nBeginIndex){//look for beginningint nPrevIndex = str.indexOf(strBeginp,nBeginIndex);//beginining tag not found..so end itif (nPrevIndex == -1)return "";//look for endingm_nPrevIndex = nPrevIndex;int nLastIndex = str.indexOf(strEndp,nPrevIndex);m_nLastIndex = nLastIndex+lnkEndLength;return str.substring(nPrevIndex, nLastIndex+lnkEndLength);} //helper function for iterator public String parseUnit(){return parseUnit(lastUnit());} //parses out the reference attribute of the tag// in <tag ref="refattrib">prop</tag>//this parses out refattrib...public String parseRef(String strCur){int nRefBegin=0;int nRefEnd = 0;nRefBegin=strCur.indexOf(strRefp);nRefEnd = strCur.indexOf('"', nRefBegin+lnkRefLength);return strCur.substring(nRefBegin+lnkRefLength,nRefEnd);} //parses out the property of the tag // in <tag ref="refattrib">prop</tag>//this parses out prop...public String parseProp(String strCur){ int nLnkEnd = 0;int n = 0;nLnkEnd = strCur.lastIndexOf(strEndp);for (n=nLnkEnd; strCur.charAt(n) != '>' && n >= 0; n--);if (strCur.charAt(n) != '>')return "";String strLnk = strCur.substring(n, nLnkEnd);nLnkEnd = strLnk.indexOf(">");strLnk = strLnk.substring(nLnkEnd+1); return strLnk;} //helper function for iterator public int prevUnit(){return m_nPrevIndex; }//helper function for iteratorpublic int lastUnit(){if (str.indexOf(strBeginp, m_nLastIndex) == -1)return -1;elsereturn m_nLastIndex ;}};
import java.util.Vector;public class imgParser{private baseParser m_spa; private String m_strMain="";private String m_strKey="";public imgParser(){m_strMain = "";m_strKey = "";}//in reality i was also passing the db connection from the page
//script to the parser...//this allowed me to reuse the connection i made to the db for //displaying the page//instead of creating new connections for each instance of the parserpublic imgParser(String toBeParsed){m_strMain = toBeParsed;//seed the base parser with our required tagsm_spa = new baseParser(toBeParsed,"<g","</g>","n=\"");}private Vector lookupImageDb(String sImageNo )
{ // this was a lotus domino routine which looked up the // index name in the database and returned a row of //information as a java Vector //for illustrative purposes...i am just returning a dummy vector //but your routine to query the db would come over here... //this returned a vector with 2 columns // 1st col - image filename // 2nd col - image alt text Vector v = new Vector(2); v.addElement(new String("image.jpg")); v.addElement(new String("just a dummy image")); return v; } private String getPath() { //i didnt hard code any paths... //this function simply calculated the path to the image inside the //database...since i was storing the image in the database //you could use it to calculate paths... //for illustrative purposes, i just return a dummy path... return new String("/images") ; } private int IMG_FILE_NAME_COL=0; private int IMG_ALT_NAME_COL=1; private String ParseImageTag(String sImageNo, String sAlign) { String sTemp = ""; try{ //lookup image info from image store Vector v = lookupImageDb(sImageNo); //string used to store alt tag String strAltTag=new String(""); //string use to store image file name or handle in database String strFileName = new String(""); //get the alt tage and file name strAltTag = (String)v.elementAt(IMG_ALT_NAME_COL); strFileName = (String)v.elementAt(IMG_FILE_NAME_COL); //build the image tag sTemp+="<img src=\""+getPath()+"/"+strFileName +"\""; //set alignment depending on l or r properties if (sAlign.equals("l")) sTemp+=" align=\"left\""; else sTemp+=" align=\"right\""; //other tags //apply alt tag only if it exists if (strAltTag.length() != 0) sTemp += " alt=\""+strAltTag+"\""; sTemp+=" hspace=\"8\" vspace=\"8\" "; sTemp+="border=0 />"; } catch(Exception e) { e.printStackTrace(); } finally{ return sTemp; } } //the only function called externally for this parser public String ParsedString() { String strTemp=""; int nBegin=0; int nPrev = 0; int nLast = 0; //call the base parser routine to iteratively scan for <g> tags String sRet = m_spa.parseUnit(nBegin); if (sRet.equals("")) //no image tags....so return string as is return m_strMain; //<g> tag found parse out attributes and properties String sRef = m_spa.parseRef(sRet); String sTxt = m_spa.parseProp(sRet); nPrev = m_spa.prevUnit(); if (nPrev != -1) { //skip string to position at the end of <g></g> strTemp = m_strMain.substring(0,nPrev); //now build the <img> tag from the <g> tag strTemp+=ParseImageTag(sRef, sTxt); } nLast = m_spa.lastUnit(); //check if end while (nLast!=-1) { //parse the next <g> tag set sRet = m_spa.parseUnit(); //<g> tag found parse out attributes and properties sRef = m_spa.parseRef(sRet); sTxt = m_spa.parseProp(sRet); nPrev = m_spa.prevUnit(); if (nPrev != -1) { //skip string to position at the end of current <g></g> strTemp+= m_strMain.substring(nLast,nPrev); //parse out next image tag... strTemp+=ParseImageTag(sRef, sTxt); } nLast = m_spa.lastUnit(); } strTemp+=m_strMain.substring(nPrev+sRet.length()); return strTemp; } };
//..other code on pageString strContent;String strParsedContent;strContent = get_Content_As_String_From_Content_Table_Field();//in reality i pass my database connection handle of the page script
//to the parser, so i can reuse it//create our image parser object and pass the content string to itimgParser ipObj = new imgParser(strContent);//strParsedContent contains the transformed string now...
strParsedContent = ipObj.ParsedString();//...output parsed content to page
printwriterObj.print(strParsedContent);
<g n="">
) was by design. I wanted to make the tags as simple as possible for the user entering content. In almost all the cases where I used the parser, I never had to use more than a single attribute. Also I never check for incorrect tags. This was because I was checking it at the client side at the point of data entry of the content.Using this technique certainly saved time and heartburn for me. Other than dealing with clients, I also had to deal with a copy writer who liked getting acquainted with Gilbey's dry gin more than with "complicated" HTML tags. But, he actually enjoyed entering these custom tags along with his copy, since for the first time he had complete control over where to position images in the copy.