Source for org.jfree.report.modules.factories.report.flow.StyleSheetReadHandler

   1: /**
   2:  * ========================================
   3:  * JFreeReport : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/
   7:  *
   8:  * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either 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, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * $Id: StyleSheetReadHandler.java 3525 2007-10-16 11:43:48Z tmorgner $
  27:  * ------------
  28:  * (C) Copyright 2000-2005, by Object Refinery Limited.
  29:  * (C) Copyright 2005-2007, by Pentaho Corporation.
  30:  */
  31: package org.jfree.report.modules.factories.report.flow;
  32: 
  33: import org.jfree.layouting.input.style.StyleSheet;
  34: import org.jfree.resourceloader.Resource;
  35: import org.jfree.resourceloader.ResourceCreationException;
  36: import org.jfree.resourceloader.ResourceKey;
  37: import org.jfree.resourceloader.ResourceKeyCreationException;
  38: import org.jfree.resourceloader.ResourceLoadingException;
  39: import org.jfree.resourceloader.ResourceManager;
  40: import org.jfree.resourceloader.ResourceException;
  41: import org.jfree.util.Log;
  42: import org.jfree.xmlns.parser.StringReadHandler;
  43: import org.jfree.xmlns.parser.ParseException;
  44: import org.xml.sax.Attributes;
  45: import org.xml.sax.SAXException;
  46: 
  47: /**
  48:  * Creation-Date: 12.04.2006, 14:53:29
  49:  *
  50:  * @author Thomas Morgner
  51:  */
  52: public class StyleSheetReadHandler extends StringReadHandler
  53: {
  54:   private StyleSheet styleSheet;
  55: 
  56:   public StyleSheetReadHandler()
  57:   {
  58:   }
  59: 
  60:   /**
  61:    * Starts parsing.
  62:    *
  63:    * @param attrs the attributes.
  64:    * @throws SAXException if there is a parsing error.
  65:    */
  66:   protected void startParsing(final Attributes attrs) throws SAXException
  67:   {
  68:     super.startParsing(attrs);
  69:     final String href = attrs.getValue(getUri(), "href");
  70:     if (href != null)
  71:     {
  72:       final ResourceKey key = getRootHandler().getSource();
  73:       final ResourceManager manager = getRootHandler().getResourceManager();
  74:       try
  75:       {
  76:         final ResourceKey derivedKey = manager.deriveKey(key, href);
  77:         final Resource resource = manager.create(derivedKey, null,
  78:                 StyleSheet.class);
  79:         getRootHandler().getDependencyCollector().add(resource);
  80:         styleSheet = (StyleSheet) resource.getResource();
  81:       }
  82:       catch (ResourceKeyCreationException e)
  83:       {
  84:         throw new ParseException
  85:             ("Unable to derive key for " + key + " and " + href, getLocator());
  86:       }
  87:       catch (ResourceCreationException e)
  88:       {
  89:         Log.warn("Unable to parse resource for " + key + " and " + href);
  90:       }
  91:       catch (ResourceLoadingException e)
  92:       {
  93:         Log.warn ("Unable to load resource data for " + key + " and " + href);
  94:       }
  95:       catch (ResourceException e)
  96:       {
  97:         Log.warn ("Unable to load resource for " + key + " and " + href);
  98:       }
  99:     }
 100:   }
 101: 
 102:   /**
 103:    * Done parsing.
 104:    *
 105:    * @throws SAXException       if there is a parsing error.
 106:    */
 107:   protected void doneParsing() throws SAXException
 108:   {
 109:     super.doneParsing();
 110:     if (this.styleSheet != null)
 111:     {
 112:       return;
 113:     }
 114: 
 115:     final String styleText = getResult();
 116:     if (styleText.trim().length() == 0)
 117:     {
 118:       return;
 119:     }
 120:     try
 121:     {
 122:       final byte[] bytes = styleText.getBytes("UTF-8");
 123: 
 124:       final ResourceKey baseKey = getRootHandler().getSource();
 125:       final ResourceManager resourceManager = getRootHandler().getResourceManager();
 126:       final ResourceKey rawKey = resourceManager.createKey(bytes);
 127: 
 128:       final Resource resource = resourceManager.create
 129:               (rawKey, baseKey, StyleSheet.class);
 130:       this.styleSheet = (StyleSheet) resource.getResource();
 131:     }
 132:     catch (Exception e)
 133:     {
 134:       e.printStackTrace();
 135:     }
 136:   }
 137: 
 138:   /**
 139:    * Returns the object for this element.
 140:    *
 141:    * @return the object.
 142:    */
 143:   public Object getObject()
 144:   {
 145:     return styleSheet;
 146:   }
 147: 
 148:   public StyleSheet getStyleSheet()
 149:   {
 150:     return styleSheet;
 151:   }
 152: }