1 28 package net.sf.jguard.ext.util; 29 30 import java.io.BufferedOutputStream ; 31 import java.io.FileOutputStream ; 32 import java.io.IOException ; 33 import java.net.MalformedURLException ; 34 import java.net.URL ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.logging.Level ; 38 import java.util.logging.Logger ; 39 import java.util.regex.Matcher ; 40 import java.util.regex.Pattern ; 41 42 import net.sf.jguard.core.principals.RolePrincipal; 43 44 import org.dom4j.Document; 45 import org.dom4j.DocumentException; 46 import org.dom4j.Element; 47 import org.dom4j.io.OutputFormat; 48 import org.dom4j.io.SAXReader; 49 import org.dom4j.io.XMLWriter; 50 51 56 public class XMLUtils { 57 58 private static final String UTF_8 = "UTF-8"; 59 60 61 private static Logger logger = Logger.getLogger(XMLUtils.class.getName()); 62 63 64 private static final String DOUBLE_ANTI_SLASH = "\\\\"; 65 private static final String SLASH = "/"; 66 private static Pattern doubleAntiSlashPattern = Pattern.compile(XMLUtils.DOUBLE_ANTI_SLASH); 67 68 private static final String ESCAPED_BLANK = "%20"; 69 private static final String BLANK = " "; 70 private static Pattern blankPattern = Pattern.compile(XMLUtils.BLANK); 71 72 private static final String FILE_SLASH_AND_PLUS = "file:/+"; 73 private static Pattern slashAndPlusPattern = Pattern.compile(XMLUtils.FILE_SLASH_AND_PLUS); 74 private static final String FILE_AND_SIX_SLASH = "file://////"; 75 76 77 82 public static Document read(String location) { 83 84 location = resolveLocation(location); 85 SAXReader reader = new SAXReader(true); 86 Document document = null; 87 88 URL url = null; 89 try { 90 url = new URL (location); 91 } catch (MalformedURLException e) { 92 logger.severe("read(String) : " + e); 93 if (logger.isLoggable(Level.FINEST)) { 94 logger.finest("read() - the 'fileLocation' value ( " + location 95 + " ) present in the web.xml file is a malformed URL "); 96 } 97 } 98 99 try { 100 document = reader.read(url); 101 } catch (DocumentException e1) { 102 logger.severe("read(String) : " + e1); 103 } 104 105 if(document==null){ 106 throw new IllegalArgumentException (location+" does not point to an XML document "); 107 } 108 return document; 109 } 110 111 116 public static String resolveLocation(String location) { 117 if(location==null){ 118 throw new IllegalArgumentException (" location is null "); 119 } 120 location = location.trim(); 122 123 Matcher blankMatcher = blankPattern.matcher(location); 125 location = blankMatcher.replaceAll(XMLUtils.ESCAPED_BLANK); 126 127 Matcher antiSlashMatcher = doubleAntiSlashPattern.matcher(location); 129 location = antiSlashMatcher.replaceAll(XMLUtils.SLASH); 130 131 if(-1==location.indexOf(':')||1==location.indexOf(':')){ 133 location = new StringBuffer ("file://////").append(location).toString(); 134 } 135 136 137 if (logger.isLoggable(Level.FINEST)) { 138 logger.finest("location="+location); 139 } 140 return location; 141 } 142 143 public static void write(String fileLocation,Document document) throws IOException { 144 XMLUtils.write(new URL (fileLocation),document); 145 } 146 147 153 public static void write(URL url,Document document) throws IOException { 154 OutputFormat outFormat = OutputFormat.createPrettyPrint(); 155 if(document.getXMLEncoding()!=null){ 156 outFormat.setEncoding(document.getXMLEncoding()); 157 }else{ 158 outFormat.setEncoding(UTF_8); 159 } 160 XMLWriter out = new XMLWriter(new BufferedOutputStream (new FileOutputStream (url.getPath())),outFormat); 161 out.write(document); 162 out.flush(); 163 out.close(); 164 } 165 166 172 public static void deletePrincipalRefs(Element root, RolePrincipal principal){ 173 Element principalsElement = root.element("principals"); 174 List principalsRefList = principalsElement.selectNodes("//principalRef[@name='"+principal.getLocalName()+"']"); 175 Iterator descendantsRefIt = principalsRefList.iterator(); 176 while(descendantsRefIt.hasNext()){ 177 Element principalRef = (Element)descendantsRefIt.next(); 178 Element principalElement = principalRef.getParent(); 179 principalElement.remove(principalRef); 180 if(principalElement.elements("principalRef").size()==0){ 181 principalElement.getParent().remove(principalElement); 182 } 183 } 184 } 185 186 187 } 188 | Popular Tags |