View Javadoc

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