001 /** 002 * ======================================== 003 * JFreeReport : a free Java report library 004 * ======================================== 005 * 006 * Project Info: http://reporting.pentaho.org/ 007 * 008 * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors. 009 * 010 * This library is free software; you can redistribute it and/or modify it under the terms 011 * of the GNU Lesser General Public License as published by the Free Software Foundation; 012 * either version 2.1 of the License, or (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 015 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 016 * See the GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public License along with this 019 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, 020 * Boston, MA 02111-1307, USA. 021 * 022 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 023 * in the United States and other countries.] 024 * 025 * ------------ 026 * $Id: StyleSheetReadHandler.java 3525 2007-10-16 11:43:48Z tmorgner $ 027 * ------------ 028 * (C) Copyright 2000-2005, by Object Refinery Limited. 029 * (C) Copyright 2005-2007, by Pentaho Corporation. 030 */ 031 package org.jfree.report.modules.factories.report.flow; 032 033 import org.jfree.layouting.input.style.StyleSheet; 034 import org.jfree.resourceloader.Resource; 035 import org.jfree.resourceloader.ResourceCreationException; 036 import org.jfree.resourceloader.ResourceKey; 037 import org.jfree.resourceloader.ResourceKeyCreationException; 038 import org.jfree.resourceloader.ResourceLoadingException; 039 import org.jfree.resourceloader.ResourceManager; 040 import org.jfree.resourceloader.ResourceException; 041 import org.jfree.util.Log; 042 import org.jfree.xmlns.parser.StringReadHandler; 043 import org.jfree.xmlns.parser.ParseException; 044 import org.xml.sax.Attributes; 045 import org.xml.sax.SAXException; 046 047 /** 048 * Creation-Date: 12.04.2006, 14:53:29 049 * 050 * @author Thomas Morgner 051 */ 052 public class StyleSheetReadHandler extends StringReadHandler 053 { 054 private StyleSheet styleSheet; 055 056 public StyleSheetReadHandler() 057 { 058 } 059 060 /** 061 * Starts parsing. 062 * 063 * @param attrs the attributes. 064 * @throws SAXException if there is a parsing error. 065 */ 066 protected void startParsing(final Attributes attrs) throws SAXException 067 { 068 super.startParsing(attrs); 069 final String href = attrs.getValue(getUri(), "href"); 070 if (href != null) 071 { 072 final ResourceKey key = getRootHandler().getSource(); 073 final ResourceManager manager = getRootHandler().getResourceManager(); 074 try 075 { 076 final ResourceKey derivedKey = manager.deriveKey(key, href); 077 final Resource resource = manager.create(derivedKey, null, 078 StyleSheet.class); 079 getRootHandler().getDependencyCollector().add(resource); 080 styleSheet = (StyleSheet) resource.getResource(); 081 } 082 catch (ResourceKeyCreationException e) 083 { 084 throw new ParseException 085 ("Unable to derive key for " + key + " and " + href, getLocator()); 086 } 087 catch (ResourceCreationException e) 088 { 089 Log.warn("Unable to parse resource for " + key + " and " + href); 090 } 091 catch (ResourceLoadingException e) 092 { 093 Log.warn ("Unable to load resource data for " + key + " and " + href); 094 } 095 catch (ResourceException e) 096 { 097 Log.warn ("Unable to load resource for " + key + " and " + href); 098 } 099 } 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 }