Coverage report

  %line %branch
org.apache.jetspeed.rewriter.JetspeedRewriterController
0% 
0% 

 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.jetspeed.rewriter;
 18  
 
 19  
 import java.io.File;
 20  
 import java.io.FileReader;
 21  
 import java.io.Reader;
 22  
 import java.util.ArrayList;
 23  
 import java.util.Collections;
 24  
 import java.util.HashMap;
 25  
 import java.util.List;
 26  
 import java.util.Map;
 27  
 
 28  
 import javax.xml.parsers.DocumentBuilder;
 29  
 import javax.xml.parsers.DocumentBuilderFactory;
 30  
 
 31  
 import org.apache.commons.logging.Log;
 32  
 import org.apache.commons.logging.LogFactory;
 33  
 import org.apache.jetspeed.rewriter.html.SwingParserAdaptor;
 34  
 import org.apache.jetspeed.rewriter.rules.Ruleset;
 35  
 import org.apache.jetspeed.rewriter.xml.SaxParserAdaptor;
 36  
 import org.exolab.castor.mapping.Mapping;
 37  
 import org.exolab.castor.xml.Unmarshaller;
 38  
 import org.w3c.dom.Document;
 39  
 import org.xml.sax.InputSource;
 40  
 
 41  
 /**
 42  
  * RewriterServiceImpl
 43  
  * 
 44  
  * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
 45  
  * @version $Id: JetspeedRewriterController.java,v 1.2 2004/03/08 00:44:40 jford
 46  
  *          Exp $
 47  
  */
 48  
 public class JetspeedRewriterController implements RewriterController
 49  
 {
 50  0
     protected final static Log log = LogFactory.getLog(JetspeedRewriterController.class);
 51  
     final static String CONFIG_MAPPING_FILE = "mapping";
 52  
     final static String CONFIG_BASIC_REWRITER = "basic.class";
 53  
     final static String CONFIG_RULESET_REWRITER = "ruleset.class";
 54  
     final static String CONFIG_ADAPTOR_HTML = "adaptor.html";
 55  
     final static String CONFIG_ADAPTOR_XML = "adaptor.xml";
 56  
 
 57  
     // configuration parameters
 58  0
     protected String mappingFile = null;
 59  
 
 60  
     /** the Castor mapping file name */
 61  0
     protected Mapping mapper = null;
 62  
 
 63  
     /** Collection of rulesets in the system */
 64  0
     protected Map rulesets = Collections.synchronizedMap(new HashMap());
 65  
 
 66  
     /** configured basic rewriter class */
 67  0
     protected Class basicRewriterClass = BasicRewriter.class;
 68  
 
 69  
     /** configured ruleset rewriter class */
 70  0
     protected Class rulesetRewriterClass = RulesetRewriterImpl.class;
 71  
 
 72  
     /** Adaptors */
 73  0
     protected Class adaptorHtmlClass = SwingParserAdaptor.class;
 74  0
     protected Class adaptorXmlClass = SaxParserAdaptor.class;
 75  
 
 76  
     public JetspeedRewriterController( String mappingFile ) throws RewriterException
 77  0
     {
 78  0
         this.mappingFile = mappingFile;
 79  0
         loadMapping();
 80  0
     }
 81  
 
 82  
     public JetspeedRewriterController( String mappingFile, List rewriterClasses, List adaptorClasses )
 83  
             throws RewriterException
 84  0
     {
 85  0
         this.mappingFile = mappingFile;
 86  0
         if (rewriterClasses.size() > 0)
 87  
         {
 88  0
             this.basicRewriterClass = (Class) rewriterClasses.get(0);
 89  0
             if (rewriterClasses.size() > 1)
 90  
             {
 91  0
                 this.rulesetRewriterClass = (Class) rewriterClasses.get(1);
 92  
             }
 93  
         }
 94  0
         if (adaptorClasses.size() > 0)
 95  
         {
 96  0
             this.adaptorHtmlClass = (Class) adaptorClasses.get(0);
 97  0
             if (adaptorClasses.size() > 1)
 98  
             {
 99  0
                 this.adaptorXmlClass = (Class) adaptorClasses.get(1);
 100  
             }
 101  
         }
 102  
 
 103  0
         loadMapping();
 104  0
     }
 105  
     
 106  
     public JetspeedRewriterController( String mappingFile, String basicRewriterClassName, String rulesetRewriterClassName, 
 107  
                     String adaptorHtmlClassName, String adaptorXmlClassName )
 108  
     throws RewriterException
 109  
     {
 110  0
         this(mappingFile, toClassList(basicRewriterClassName,rulesetRewriterClassName), toClassList(adaptorHtmlClassName,adaptorXmlClassName));
 111  0
     }
 112  
 
 113  
     protected static List toClassList(String classNameA, String classNameB)
 114  
     {
 115  
         try
 116  
         {
 117  0
             List list = new ArrayList(2);
 118  0
             if ( classNameA != null )
 119  
             {
 120  0
                 list.add(Class.forName(classNameA));
 121  
             }
 122  0
             if ( classNameB != null )
 123  
             {
 124  0
                 list.add(Class.forName(classNameB));
 125  
             }
 126  0
             return list;
 127  
         } 
 128  0
         catch (ClassNotFoundException e)
 129  
         {
 130  0
             throw new RuntimeException(e);
 131  
         }
 132  
     }    
 133  
 
 134  
     /*
 135  
      * (non-Javadoc)
 136  
      * 
 137  
      * @see org.apache.jetspeed.rewriter.RewriterService#createRewriter()
 138  
      */
 139  
     public Rewriter createRewriter() throws InstantiationException, IllegalAccessException
 140  
     {
 141  0
         return (Rewriter) basicRewriterClass.newInstance();
 142  
     }
 143  
 
 144  
     /*
 145  
      * (non-Javadoc)
 146  
      * 
 147  
      * @see org.apache.jetspeed.rewriter.RewriterService#createRewriter(org.apache.jetspeed.rewriter.rules.Ruleset)
 148  
      */
 149  
     public RulesetRewriter createRewriter( Ruleset ruleset ) throws RewriterException
 150  
     {
 151  
         try
 152  
         {
 153  0
             RulesetRewriter rewriter = (RulesetRewriter) rulesetRewriterClass.newInstance();
 154  0
             rewriter.setRuleset(ruleset);
 155  0
             return rewriter;
 156  
         }
 157  0
         catch (Exception e)
 158  
         {
 159  0
             log.error("Error creating rewriter class", e);
 160  
         }
 161  0
         return null;
 162  
     }
 163  
 
 164  
     /*
 165  
      * (non-Javadoc)
 166  
      * 
 167  
      * @see org.apache.jetspeed.rewriter.RewriterService#createParserAdaptor(java.lang.String)
 168  
      */
 169  
     public ParserAdaptor createParserAdaptor( String mimeType ) throws RewriterException
 170  
     {
 171  
         try
 172  
         {
 173  0
             if (mimeType.equals("text/html"))
 174  
             {
 175  0
                 return (ParserAdaptor) adaptorHtmlClass.newInstance();
 176  
             }
 177  0
             else if (mimeType.equals("text/xml"))
 178  
             {
 179  0
                 return (ParserAdaptor) adaptorXmlClass.newInstance();
 180  
             }
 181  
             else
 182  
             {
 183  
             }
 184  
         }
 185  0
         catch (Exception e)
 186  
         {
 187  0
             log.error("Error creating rewriter class", e);
 188  0
         }
 189  0
         return null;
 190  
     }
 191  
 
 192  
     /**
 193  
      * Load the mapping file for ruleset configuration
 194  
      *  
 195  
      */
 196  
     protected void loadMapping() throws RewriterException
 197  
     {
 198  
         try
 199  
         {
 200  0
             Reader reader = getReader(this.mappingFile);
 201  
             
 202  0
             this.mapper = new Mapping();
 203  0
             InputSource is = new InputSource(reader);
 204  0
             is.setSystemId(this.mappingFile);
 205  0
             this.mapper.loadMapping(is);
 206  
         }
 207  0
         catch (Exception e)
 208  
         {
 209  0
             e.printStackTrace();
 210  0
             String msg = "RewriterService: Error in castor mapping creation";
 211  0
             log.error(msg, e);
 212  0
             throw new RewriterException(msg, e);
 213  0
         }
 214  0
     }
 215  
 
 216  
     protected Reader getReader(String resource)
 217  
     throws RewriterException
 218  
     {
 219  0
         File file = new File(resource);
 220  0
         if (file.exists() && file.isFile() && file.canRead())
 221  
         {
 222  
             try
 223  
             {
 224  0
                 return new FileReader(file);
 225  
             }
 226  0
             catch (Exception e)
 227  
             {
 228  0
                 throw new RewriterException("could not open rewriter file " + resource, e);
 229  
             }
 230  
         }
 231  0
         throw new RewriterException("could not access rewriter file " + resource);
 232  
     }
 233  
         
 234  
     /*
 235  
      * (non-Javadoc)
 236  
      * 
 237  
      * @see org.apache.jetspeed.rewriter.RewriterService#lookupRuleset(java.lang.String)
 238  
      */
 239  
     public Ruleset lookupRuleset( String id )
 240  
     {
 241  0
         return (Ruleset) rulesets.get(id);
 242  
     }
 243  
 
 244  
     /*
 245  
      * (non-Javadoc)
 246  
      * 
 247  
      * @see org.apache.jetspeed.rewriter.RewriterService#loadRuleset(java.io.Reader)
 248  
      */
 249  
     public Ruleset loadRuleset( Reader reader )
 250  
     {
 251  0
         Ruleset ruleset = null;
 252  
         try
 253  
         {
 254  0
             DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
 255  0
             DocumentBuilder builder = dbfactory.newDocumentBuilder();
 256  
 
 257  0
             InputSource source = new InputSource(reader);
 258  
 
 259  0
             Document doc = builder.parse(source);
 260  
 
 261  0
             Unmarshaller unmarshaller = new Unmarshaller(this.mapper);
 262  
 
 263  0
             ruleset = (Ruleset) unmarshaller.unmarshal(doc);
 264  0
             ruleset.sync();
 265  0
             rulesets.put(ruleset.getId(), ruleset);
 266  
 
 267  
         }
 268  0
         catch (Throwable t)
 269  
         {
 270  0
             log.error("ForwardService: Could not unmarshal: " + reader, t);
 271  0
         }
 272  
 
 273  0
         return ruleset;
 274  
     }
 275  
 
 276  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.