1 16 19 package org.apache.taglibs.string.util; 20 21 import org.apache.commons.lang.StringUtils; 22 23 29 final public class XmlW { 30 31 static public String escapeXml(String str) { 32 str = StringUtils.replace(str,"&","&"); 33 str = StringUtils.replace(str,"<","<"); 34 str = StringUtils.replace(str,">",">"); 35 str = StringUtils.replace(str,"\"","""); 36 str = StringUtils.replace(str,"'","'"); 37 return str; 38 } 39 40 static public String unescapeXml(String str) { 41 str = StringUtils.replace(str,"&","&"); 42 str = StringUtils.replace(str,"<","<"); 43 str = StringUtils.replace(str,">",">"); 44 str = StringUtils.replace(str,""","\""); 45 str = StringUtils.replace(str,"'","'"); 46 return str; 47 } 48 49 53 static public String removeXml(String str) { 54 int sz = str.length(); 55 StringBuffer buffer = new StringBuffer (sz); 56 boolean inString = false; 57 boolean inTag = false; 58 for(int i=0; i<sz; i++) { 59 char ch = str.charAt(i); 60 if(ch == '<') { 61 inTag = true; 62 } else 63 if(ch == '>') { 64 inTag = false; 65 continue; 66 } 67 if(!inTag) { 68 buffer.append(ch); 69 } 70 } 71 return buffer.toString(); 72 } 73 74 static public String getContent(String tag, String text) { 75 int idx = XmlW.getIndexOpeningTag(tag, text); 76 if(idx == -1) { 77 return ""; 78 } 79 text = text.substring(idx); 80 int end = XmlW.getIndexClosingTag(tag, text); 81 idx = text.indexOf('>'); 82 if(idx == -1) { 83 return ""; 84 } 85 return text.substring(idx+1, end); 86 } 87 88 static public int getIndexOpeningTag(String tag, String text) { 89 return getIndexOpeningTag(tag, text, 0); 90 } 91 static private int getIndexOpeningTag(String tag, String text, int start) { 92 int idx = text.indexOf("<"+tag, start); 94 if(idx == -1) { 95 return -1; 96 } 97 char next = text.charAt(idx+1+tag.length()); 98 if( (next == '>') || Character.isWhitespace(next) ) { 99 return idx; 100 } else { 101 return getIndexOpeningTag(tag, text, idx+1); 102 } 103 } 104 105 static public int getIndexClosingTag(String tag, String text) { 109 return getIndexClosingTag(tag, text, 0); 110 } 111 static public int getIndexClosingTag(String tag, String text, int start) { 112 String open = "<"+tag; 113 String close = "</"+tag+">"; 114 int closeSz = close.length(); 117 int nextCloseIdx = text.indexOf(close, start); 118 if(nextCloseIdx == -1) { 120 return -1; 121 } 122 int count = StringUtils.countMatches(text.substring(start, nextCloseIdx), open); 123 if(count == 0) { 125 return -1; } 127 int expected = 1; 128 while(count != expected) { 129 nextCloseIdx = text.indexOf(close, nextCloseIdx+closeSz); 130 if(nextCloseIdx == -1) { 131 return -1; 132 } 133 count = StringUtils.countMatches(text.substring(start, nextCloseIdx), open); 134 expected++; 135 } 136 return nextCloseIdx; 137 } 138 139 static public String getAttribute(String attribute, String text) { 140 return getAttribute(attribute, text, 0); 141 } 142 static public String getAttribute(String attribute, String text, int idx) { 143 int close = text.indexOf(">", idx); 144 int attrIdx = text.indexOf(attribute+"=\"", idx); 145 if(attrIdx == -1) { 146 return null; 147 } 148 if(attrIdx > close) { 149 return null; 150 } 151 int attrStartIdx = attrIdx + attribute.length() + 2; 152 int attrCloseIdx = text.indexOf("\"", attrStartIdx); 153 if(attrCloseIdx > close) { 154 return null; 155 } 156 return unescapeXml(text.substring(attrStartIdx, attrCloseIdx)); 157 } 158 159 } 160 | Popular Tags |