001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/framework/trigger/TriggerConfigurationDocument.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2007 by:
006     EXSE, Department of Geography, University of Bonn
007     http://www.giub.uni-bonn.de/deegree/
008     lat/lon GmbH
009     http://www.lat-lon.de
010    
011     This library is free software; you can redistribute it and/or
012     modify it under the terms of the GNU Lesser General Public
013     License as published by the Free Software Foundation; either
014     version 2.1 of the License, or (at your option) any later version.
015    
016     This library is distributed in the hope that it will be useful,
017     but WITHOUT ANY WARRANTY; without even the implied warranty of
018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019     Lesser General Public License for more details.
020    
021     You should have received a copy of the GNU Lesser General Public
022     License along with this library; if not, write to the Free Software
023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024    
025     Contact:
026    
027     Andreas Poth
028     lat/lon GmbH
029     Aennchenstr. 19
030     53115 Bonn
031     Germany
032     E-Mail: poth@lat-lon.de
033    
034     Prof. Dr. Klaus Greve
035     Department of Geography
036     University of Bonn
037     Meckenheimer Allee 166
038     53115 Bonn
039     Germany
040     E-Mail: greve@giub.uni-bonn.de
041    
042     ---------------------------------------------------------------------------*/
043    package org.deegree.framework.trigger;
044    
045    import java.io.File;
046    import java.io.IOException;
047    import java.net.MalformedURLException;
048    import java.net.URI;
049    import java.net.URISyntaxException;
050    import java.net.URL;
051    import java.util.ArrayList;
052    import java.util.Date;
053    import java.util.HashMap;
054    import java.util.List;
055    import java.util.Map;
056    
057    import org.deegree.framework.log.ILogger;
058    import org.deegree.framework.log.LoggerFactory;
059    import org.deegree.framework.util.TimeTools;
060    import org.deegree.framework.xml.NamespaceContext;
061    import org.deegree.framework.xml.XMLFragment;
062    import org.deegree.framework.xml.XMLParsingException;
063    import org.deegree.framework.xml.XMLTools;
064    import org.deegree.i18n.Messages;
065    import org.w3c.dom.Element;
066    import org.w3c.dom.Node;
067    import org.xml.sax.SAXException;
068    
069    /**
070     * 
071     * 
072     *
073     * @version $Revision: 7094 $
074     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
075     * @author last edited by: $Author: apoth $
076     *
077     * @version 1.0. $Revision: 7094 $, $Date: 2007-05-10 20:05:13 +0200 (Do, 10 Mai 2007) $
078     *
079     * @since 2.0
080     */
081    public class TriggerConfigurationDocument extends XMLFragment {
082    
083        private ILogger LOG = LoggerFactory.getLogger( TriggerConfigurationDocument.class );
084    
085        private static NamespaceContext nsc = new NamespaceContext();
086        static {
087            try {
088                nsc.addNamespace( "dgTr", new URI( "http://www.deegree.org/trigger" ) );
089            } catch ( URISyntaxException e ) {
090                // should never happen
091                e.printStackTrace();
092            }
093        }
094        
095        
096        /**
097         * default constructor
098         */
099        public TriggerConfigurationDocument() {   
100            
101        }
102    
103        /**
104         * initialized the class by assigning a XML file
105         * @param file
106         * @throws IOException
107         * @throws SAXException
108         */
109        public TriggerConfigurationDocument( File file ) throws IOException, SAXException {
110            super( file.toURL() );        
111        }
112    
113        /**
114         * 
115         * @return TriggerCapabilities
116         * @throws XMLParsingException 
117         * @throws TriggerException 
118         */
119        public TriggerCapabilities parseTriggerCapabilities()
120                                throws XMLParsingException, TriggerException {
121    
122            List list = XMLTools.getNodes( getRootElement(), "dgTr:class", nsc );
123            Map<String,TargetClass> targetClasses = new HashMap<String,TargetClass>( list.size() );
124            for ( int i = 0; i < list.size(); i++ ) {
125                TargetClass tc = parserTargetClass( (Element) list.get( i ) );
126                targetClasses.put( tc.getName(), tc );
127            }
128    
129            return new TriggerCapabilities( targetClasses );
130        }
131    
132        /**
133         * 
134         * @param element
135         * @return TargetClass
136         * @throws XMLParsingException
137         * @throws TriggerException 
138         */
139        private TargetClass parserTargetClass( Element element )
140                                throws XMLParsingException, TriggerException {
141    
142            String clName = XMLTools.getRequiredNodeAsString( element, "dgTr:name/text()", nsc );
143    
144            List list = XMLTools.getNodes( element, "dgTr:method", nsc );
145            Map<String,TargetMethod> targetMethods = new HashMap<String,TargetMethod>( list.size() );
146            for ( int i = 0; i < list.size(); i++ ) {            
147                TargetMethod tm = parseTargetMethod( (Element) list.get( i ) );
148                targetMethods.put( tm.getName(), tm );
149            }
150    
151            return new TargetClass( clName, targetMethods );
152        }
153    
154        /**
155         * 
156         * @param element
157         * @return TargetMethod
158         * @throws XMLParsingException 
159         * @throws TriggerException 
160         */
161        private TargetMethod parseTargetMethod( Element element )
162                                throws XMLParsingException, TriggerException {
163    
164            String mName = XMLTools.getRequiredNodeAsString( element, "dgTr:name/text()", nsc );
165    
166            TriggerCapability preTrigger = null;
167            TriggerCapability postTrigger = null;
168    
169            //it is possible that no trigger is assigned to a method
170            // in this case the present of a method just indicates that
171            // it that Triggers can be assigned to it
172            Node node = XMLTools.getNode( element, "dgTr:preTrigger/dgTr:trigger", nsc );
173            if ( node != null ) {
174                preTrigger = parseTriggerCapability( (Element) node );
175            }
176            node = XMLTools.getNode( element, "dgTr:postTrigger/dgTr:trigger", nsc );
177            if ( node != null ) {
178                postTrigger = parseTriggerCapability( (Element) node );
179            }
180    
181            return new TargetMethod( mName, preTrigger, postTrigger );
182        }
183    
184        /**
185         * 
186         * @param element
187         * @return trigger capability
188         * @throws XMLParsingException 
189         * @throws TriggerException 
190         */
191        private TriggerCapability parseTriggerCapability( Element element )
192                                throws XMLParsingException, TriggerException {
193            
194            // a node (if not null) may represents a simple Trigger or a 
195            // TriggerChain (which is a Trigger too)    
196            String trName = XMLTools.getRequiredNodeAsString( element, "dgTr:name/text()", nsc );
197            String clName = XMLTools.getRequiredNodeAsString( element, "dgTr:performingClass/text()", nsc );
198            Class clss = null;
199            try {
200                clss = Class.forName( clName );
201            } catch ( ClassNotFoundException e ) {
202                LOG.logError( e.getMessage(), e );
203                throw new XMLParsingException( Messages.getMessage( "FRAMEWORK_UNKNOWN_TRIGGERCLASS",
204                                                                    clName ) );
205            }
206            
207            if ( !Trigger.class.isAssignableFrom( clss ) ) {
208                // class read from the configuration must be an implementation
209                // of org.deegree.framework.trigger.Trigger
210                throw new TriggerException( Messages.getMessage( "FRAMEWORK_INVALID_TRIGGERCLASS",
211                                                                 clName ) );
212            }
213            
214            Map<String, Class> paramTypes = new HashMap<String, Class>();
215            Map<String, Object> paramValues = new HashMap<String, Object>();
216            List<String> paramNames = new ArrayList<String>();
217            List initParams = XMLTools.getNodes( element, "dgTr:initParam", nsc );
218            parseInitParams( paramTypes, paramValues, paramNames, initParams );
219            
220            // get nested Trigger capabilities if available
221            List nested = XMLTools.getNodes( element, "dgTr:trigger/dgTr:trigger", nsc );
222            List<TriggerCapability> nestedList = new ArrayList<TriggerCapability>( nested.size() );
223            for ( int i = 0; i < nested.size(); i++ ) {
224                nestedList.add( parseTriggerCapability( (Element)nested.get( i ) ) );
225            }
226            
227            return new TriggerCapability( trName, clss, paramNames, paramTypes, paramValues, nestedList );
228        
229        }
230    
231        /**
232         * 
233         * @param paramTypes
234         * @param paramValues
235         * @param paramNames
236         * @param initParams
237         * @throws XMLParsingException
238         */
239        private void parseInitParams( Map<String, Class> paramTypes, Map<String, Object> paramValues, 
240                                      List<String> paramNames, List initParams )
241                                throws XMLParsingException {
242            for ( int i = 0; i < initParams.size(); i++ ) {
243                String name = XMLTools.getRequiredNodeAsString( (Node)initParams.get( i ),
244                                                                "dgTr:name/text()", nsc );
245                paramNames.add( name );
246                String tmp = XMLTools.getRequiredNodeAsString( (Node)initParams.get( i ),
247                                                                "dgTr:type/text()", nsc );
248                Class cl = null;
249                try {
250                    cl = Class.forName( tmp );
251                } catch ( ClassNotFoundException e ) {
252                    LOG.logError( e.getMessage(), e );
253                    throw new XMLParsingException( Messages.getMessage( "FRAMEWORK_UNKNOWN_INITPARAMCLASS",
254                                                                        tmp ) );
255                } 
256                tmp = XMLTools.getRequiredNodeAsString( (Node)initParams.get( i ),
257                                                        "dgTr:value/text()", nsc );
258                Object value = null;
259                try {
260                    value = getValue( cl, tmp );
261                } catch ( MalformedURLException e ) {               
262                    LOG.logError( e.getMessage(), e );
263                    throw new XMLParsingException( Messages.getMessage( "FRAMEWORK_TRIGGER_INITPARAM_PARSING",
264                                                                        tmp, cl.getName() ) );
265                }
266                paramTypes.put( name, cl );
267                paramValues.put( name, value );
268            }
269        }
270    
271        /**
272         * 
273         * @param type
274         * @param tmp
275         * @return parameter value
276         * @throws MalformedURLException
277         */
278        private Object getValue( Class type, String tmp ) throws MalformedURLException {
279            Object value = null;        
280            if ( type.equals( Integer.class ) ) {
281                value = Integer.parseInt( tmp );
282            } else if ( type.equals( Double.class ) ) {
283                value = Double.parseDouble( tmp );
284            } else  if ( type.equals( Float.class ) ) {
285                value = Float.parseFloat( tmp );
286            } else if ( type.equals( URL.class ) ) {
287                value = new URL( tmp );
288            } else if ( type.equals( Date.class ) ) {
289                value = TimeTools.createCalendar( tmp ).getTime();
290            } else if ( type.equals( String.class ) ) {
291                value = tmp;
292            }  
293            return value;
294        }
295    
296    }