1 package org.jahia.utils.xml; 2 3 import org.apache.commons.lang.StringUtils; 4 5 58 59 65 final public class XmlUtils { 66 67 static public String escapeXml(String str) { 68 str = StringUtils.replace(str,"&","&"); 69 str = StringUtils.replace(str,"<","<"); 70 str = StringUtils.replace(str,">",">"); 71 str = StringUtils.replace(str,"\"","""); 72 str = StringUtils.replace(str,"'","'"); 73 return str; 74 } 75 76 static public String unescapeXml(String str) { 77 str = StringUtils.replace(str,"&","&"); 78 str = StringUtils.replace(str,"<","<"); 79 str = StringUtils.replace(str,">",">"); 80 str = StringUtils.replace(str,""","\""); 81 str = StringUtils.replace(str,"'","'"); 82 return str; 83 } 84 85 89 static public String removeXml(String str) { 90 int sz = str.length(); 91 StringBuffer buffer = new StringBuffer (sz); 92 boolean inString = false; 93 boolean inTag = false; 94 for(int i=0; i<sz; i++) { 95 char ch = str.charAt(i); 96 if(ch == '<') { 97 inTag = true; 98 } else 99 if(ch == '>') { 100 inTag = false; 101 continue; 102 } 103 if(!inTag) { 104 buffer.append(ch); 105 } 106 } 107 return buffer.toString(); 108 } 109 110 static public String getContent(String tag, String text) { 111 int idx = XmlUtils.getIndexOpeningTag(tag, text); 112 if(idx == -1) { 113 return ""; 114 } 115 text = text.substring(idx); 116 int end = XmlUtils.getIndexClosingTag(tag, text); 117 idx = text.indexOf('>'); 118 if(idx == -1) { 119 return ""; 120 } 121 return text.substring(idx+1, end); 122 } 123 124 static public int getIndexOpeningTag(String tag, String text) { 125 return getIndexOpeningTag(tag, text, 0); 126 } 127 static private int getIndexOpeningTag(String tag, String text, int start) { 128 int idx = text.indexOf("<"+tag, start); 130 if(idx == -1) { 131 return -1; 132 } 133 char next = text.charAt(idx+1+tag.length()); 134 if( (next == '>') || Character.isWhitespace(next) ) { 135 return idx; 136 } else { 137 return getIndexOpeningTag(tag, text, idx+1); 138 } 139 } 140 141 static public int getIndexClosingTag(String tag, String text) { 145 return getIndexClosingTag(tag, text, 0); 146 } 147 static public int getIndexClosingTag(String tag, String text, int start) { 148 String open = "<"+tag; 149 String close = "</"+tag+">"; 150 int closeSz = close.length(); 153 int nextCloseIdx = text.indexOf(close, start); 154 if(nextCloseIdx == -1) { 156 return -1; 157 } 158 int count = StringUtils.countMatches(text.substring(start, nextCloseIdx), open); 159 if(count == 0) { 161 return -1; } 163 int expected = 1; 164 while(count != expected) { 165 nextCloseIdx = text.indexOf(close, nextCloseIdx+closeSz); 166 if(nextCloseIdx == -1) { 167 return -1; 168 } 169 count = StringUtils.countMatches(text.substring(start, nextCloseIdx), open); 170 expected++; 171 } 172 return nextCloseIdx; 173 } 174 175 static public String getAttribute(String attribute, String text) { 176 return getAttribute(attribute, text, 0); 177 } 178 static public String getAttribute(String attribute, String text, int idx) { 179 int close = text.indexOf(">", idx); 180 int attrIdx = text.indexOf(attribute+"=\"", idx); 181 if(attrIdx == -1) { 182 return null; 183 } 184 if(attrIdx > close) { 185 return null; 186 } 187 int attrStartIdx = attrIdx + attribute.length() + 2; 188 int attrCloseIdx = text.indexOf("\"", attrStartIdx); 189 if(attrCloseIdx > close) { 190 return null; 191 } 192 return unescapeXml(text.substring(attrStartIdx, attrCloseIdx)); 193 } 194 195 } 196 | Popular Tags |