1 package org.apache.portals.applications.springmvc;
2
3 import java.io.InputStream;
4 import java.io.IOException;
5 import java.util.Iterator;
6 import java.util.List;
7 import java.util.Enumeration;
8 import java.util.ArrayList;
9 import java.util.SortedSet;
10 import java.util.TreeSet;
11 import java.util.regex.Pattern;
12 import java.util.regex.Matcher;
13
14 import javax.portlet.PortletPreferences;
15 import javax.portlet.PortletRequest;
16 import javax.portlet.PortletContext;
17 import javax.portlet.ReadOnlyException;
18 import javax.portlet.ValidatorException;
19 import javax.xml.parsers.DocumentBuilder;
20 import javax.xml.parsers.DocumentBuilderFactory;
21 import javax.xml.parsers.ParserConfigurationException;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.springframework.beans.BeansException;
26 import org.springframework.context.support.ApplicationObjectSupport;
27
28 import org.w3c.dom.Node ;
29 import org.w3c.dom.NodeList ;
30
31 public class DOMTreeService extends ApplicationObjectSupport
32 {
33 private static final String DOM_TREE_NO_PATH = "domtree_no_path";
34 private static final String DOM_TREE_NO_PARSE = "domtree_no_parse";
35 private static final Log log = LogFactory.getLog( DOMTreeService.class);
36
37 protected DocumentBuilderFactory domFactory = null;
38
39 public void initApplicationContext() throws BeansException
40 {
41 domFactory = DocumentBuilderFactory.newInstance();
42 domFactory.setValidating(false);
43 }
44
45 public DOMTree getDOMTree( String name, PortletRequest request )
46 {
47 if ( name == null ) name = "";
48 PortletPreferences prefs = request.getPreferences();
49 String path = prefs.getValue( name, "" );
50 return new DOMTree( name, path );
51 }
52
53 public void saveDOMTree( String name, String path, PortletRequest request )
54 {
55 DOMTree dt = new DOMTree( name, path );
56 saveDOMTree( dt, request );
57 }
58
59 public void saveDOMTree( DOMTree dt, PortletRequest request )
60 {
61 if ( dt == null ) return ;
62 PortletPreferences prefs = request.getPreferences();
63 try
64 {
65 prefs.setValue( dt.getName(), dt.getPath() );
66 prefs.store();
67 }
68 catch ( ReadOnlyException e ) { }
69 catch ( IOException e ) { }
70 catch ( ValidatorException e ) { }
71 }
72
73 public void deleteDOMTree( String name, PortletRequest request )
74 {
75 if ( name == null ) name = "";
76 PortletPreferences prefs = request.getPreferences();
77 try
78 {
79 prefs.reset( name );
80 prefs.store();
81 }
82 catch ( ReadOnlyException e ) { }
83 catch ( IOException e ) { }
84 catch ( ValidatorException e ) { }
85 }
86
87 public SortedSet getAllDOMTrees( PortletRequest request )
88 {
89 return getAllDOMTrees( request, null );
90 }
91 public SortedSet getAllDOMTrees( PortletRequest request, List addTo )
92 {
93 if ( addTo == null )
94 {
95 addTo = new ArrayList();
96 }
97 PortletPreferences prefs = request.getPreferences();
98 Enumeration e = prefs.getNames();
99 while ( e.hasMoreElements() )
100 {
101 String name = (String)e.nextElement();
102 String path = prefs.getValue( name, "" );
103 addTo.add( new DOMTree( name, path ) );
104 }
105 return (SortedSet) new TreeSet( addTo );
106 }
107
108 public SortedSet parseAllDOMTrees( PortletRequest request, PortletContext context, List addTo )
109 {
110 SortedSet domTreeSet = getAllDOMTrees( request, addTo );
111 Iterator domTreeSetIter = domTreeSet.iterator();
112 while ( domTreeSetIter.hasNext() )
113 {
114 DOMTree dt = (DOMTree)domTreeSetIter.next();
115 if ( dt.getPath() == null || dt.getPath().length() == 0 )
116 {
117 dt.setMessage( DOM_TREE_NO_PATH );
118 }
119 else
120 {
121 InputStream is = context.getResourceAsStream( dt.getPath() );
122 org.w3c.dom.Document doc = parseXml( is );
123 dt.setParsedDocument( doc );
124 if ( doc == null )
125 {
126 dt.setMessage( DOM_TREE_NO_PARSE );
127 }
128 }
129 }
130 return domTreeSet;
131 }
132
133
134
135
136 protected org.w3c.dom.Document parseXml( InputStream is )
137 {
138 DocumentBuilder docBuilder = null;
139 org.w3c.dom.Document doc = null;
140 try
141 {
142 docBuilder = domFactory.newDocumentBuilder();
143 }
144 catch (ParserConfigurationException e)
145 {
146 log.error( "Cannot create DocumentBuilder due to " + e.getClass().getName() + " " + e.getMessage() );
147 }
148 if ( docBuilder != null )
149 {
150 try
151 {
152 doc = docBuilder.parse(is);
153 }
154 catch (Exception e)
155 {
156 log.error( "Cannot parse due to " + e.getClass().getName() + " " + e.getMessage() );
157 }
158 }
159 return doc;
160 }
161 public static class DOMNodeHelper
162 {
163 public DOMNodeHelper()
164 {
165 }
166 public List createNodeList( NodeList nl )
167 {
168 List domNodeList = new ArrayList();
169 if ( nl != null )
170 {
171 for ( int i = 0 ; i < nl.getLength() ; i++ )
172 {
173 domNodeList.add( nl.item( i ) );
174 }
175 }
176 return domNodeList;
177 }
178 public boolean isElementNode( Node n )
179 {
180 return n != null && n.getNodeType() == Node.ELEMENT_NODE;
181 }
182 public boolean isTextNode( Node n )
183 {
184 return n != null && n.getNodeType() == Node.TEXT_NODE;
185 }
186 public boolean isNonEmptyTextNode( Node n )
187 {
188 if ( n != null && n.getNodeType() == Node.TEXT_NODE )
189 {
190 String nodeVal = n.getNodeValue();
191 if ( nodeVal != null && nodeVal.trim().length() > 0 )
192 {
193 return true ;
194 }
195 }
196 return false;
197 }
198 public boolean isAttributeNode( Node n )
199 {
200 return n != null && n.getNodeType() == Node.ATTRIBUTE_NODE;
201 }
202 public boolean isDocumentNode( Node n )
203 {
204 return n != null && n.getNodeType() == Node.DOCUMENT_NODE;
205 }
206 public String replaceLineBreaks( String s )
207 {
208 Pattern p = Pattern.compile( "//s*((//r//n)|//n)//s*" );
209 Matcher m = p.matcher( s );
210 return m.replaceAll( " " );
211 }
212 }
213 }