KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jguard > ext > util > XMLUtils


1 /*
2 jGuard is a security framework based on top of jaas (java authentication and authorization security).
3 it is written for web applications, to resolve simply, access control problems.
4 version $Name: $
5 http://sourceforge.net/projects/jguard/
6
7 Copyright (C) 2004 Charles GAY
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23
24 jGuard project home page:
25 http://sourceforge.net/projects/jguard/
26
27 */

28 package net.sf.jguard.ext.util;
29
30 import java.io.BufferedOutputStream JavaDoc;
31 import java.io.FileOutputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.net.MalformedURLException JavaDoc;
34 import java.net.URL JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.logging.Level JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39 import java.util.regex.Matcher JavaDoc;
40 import java.util.regex.Pattern JavaDoc;
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 /**
52  * Utility class to handle XML documents with DOM4J.
53  * @author <a HREF="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
54  *
55  */

56 public class XMLUtils {
57
58     private static final String JavaDoc UTF_8 = "UTF-8";
59
60
61     private static Logger JavaDoc logger = Logger.getLogger(XMLUtils.class.getName());
62
63
64     private static final String JavaDoc DOUBLE_ANTI_SLASH = "\\\\";
65     private static final String JavaDoc SLASH = "/";
66     private static Pattern JavaDoc doubleAntiSlashPattern = Pattern.compile(XMLUtils.DOUBLE_ANTI_SLASH);
67
68     private static final String JavaDoc ESCAPED_BLANK = "%20";
69     private static final String JavaDoc BLANK = " ";
70     private static Pattern JavaDoc blankPattern = Pattern.compile(XMLUtils.BLANK);
71
72     private static final String JavaDoc FILE_SLASH_AND_PLUS = "file:/+";
73     private static Pattern JavaDoc slashAndPlusPattern = Pattern.compile(XMLUtils.FILE_SLASH_AND_PLUS);
74     private static final String JavaDoc FILE_AND_SIX_SLASH = "file://////";
75
76     
77     /**
78     * read the xml data storage file for users and associated principals.
79     * @param location
80     * @return xml content
81     */

82     public static Document read(String JavaDoc location) {
83
84         location = resolveLocation(location);
85         SAXReader reader = new SAXReader(true);
86         Document document = null;
87         
88             URL JavaDoc url = null;
89             try {
90                 url = new URL JavaDoc(location);
91             } catch (MalformedURLException JavaDoc 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 JavaDoc(location+" does not point to an XML document ");
107         }
108         return document;
109     }
110
111     /**
112      * resolve cleanly location.
113      * @param location
114      * @return location clean
115      */

116     public static String JavaDoc resolveLocation(String JavaDoc location) {
117         if(location==null){
118             throw new IllegalArgumentException JavaDoc(" location is null ");
119         }
120         //remove trailing space character
121
location = location.trim();
122
123         //replace remaining blank character by '%20'
124
Matcher JavaDoc blankMatcher = blankPattern.matcher(location);
125         location = blankMatcher.replaceAll(XMLUtils.ESCAPED_BLANK);
126
127         //replace '\\' by '/' (avoid windows stuff)
128
Matcher JavaDoc antiSlashMatcher = doubleAntiSlashPattern.matcher(location);
129         location = antiSlashMatcher.replaceAll(XMLUtils.SLASH);
130
131          //if it does not contains a ':' which is used with a protocol
132
if(-1==location.indexOf(':')||1==location.indexOf(':')){
133             location = new StringBuffer JavaDoc("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 JavaDoc fileLocation,Document document) throws IOException JavaDoc{
144         XMLUtils.write(new URL JavaDoc(fileLocation),document);
145     }
146
147     /**
148      * write the updated configuration to the XML file in the UTF-8 format.
149      * @param url - URL of the file to write
150      * @param document dom4j Document
151      * @throws IOException
152      */

153     public static void write(URL JavaDoc url,Document document) throws IOException JavaDoc{
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 JavaDoc(new FileOutputStream JavaDoc(url.getPath())),outFormat);
161         out.write(document);
162         out.flush();
163         out.close();
164     }
165
166      /**
167      * remove in the ascendants elements all the <i>principalRef</i> references with the name of the
168      * principal.
169      * @param root DOM4J Element of the document
170      * @param principal
171      */

172     public static void deletePrincipalRefs(Element root, RolePrincipal principal){
173         Element principalsElement = root.element("principals");
174         List JavaDoc principalsRefList = principalsElement.selectNodes("//principalRef[@name='"+principal.getLocalName()+"']");
175         Iterator JavaDoc 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