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: */package org.jfree.report.modules.factories.report.flow;
031:
032: import org.jfree.layouting.input.style.StyleSheet;
033: import org.jfree.resourceloader.Resource;
034: import org.jfree.resourceloader.ResourceCreationException;
035: import org.jfree.resourceloader.ResourceKey;
036: import org.jfree.resourceloader.ResourceKeyCreationException;
037: import org.jfree.resourceloader.ResourceLoadingException;
038: import org.jfree.resourceloader.ResourceManager;
039: import org.jfree.resourceloader.ResourceException;
040: import org.jfree.util.Log;
041: import org.jfree.xmlns.parser.StringReadHandler;
042: import org.jfree.xmlns.parser.ParseException;
043: import org.xml.sax.Attributes;
044: import org.xml.sax.SAXException;
045:
046: /**
047: * Creation-Date: 12.04.2006, 14:53:29
048: *
049: * @author Thomas Morgner
050: */
051: public class StyleSheetReadHandler extends StringReadHandler {
052: private StyleSheet styleSheet;
053:
054: public StyleSheetReadHandler() {
055: }
056:
057: /**
058: * Starts parsing.
059: *
060: * @param attrs the attributes.
061: * @throws SAXException if there is a parsing error.
062: */
063: protected void startParsing(final Attributes attrs)
064: throws SAXException {
065: super .startParsing(attrs);
066: final String href = attrs.getValue(getUri(), "href");
067: if (href != null) {
068: final ResourceKey key = getRootHandler().getSource();
069: final ResourceManager manager = getRootHandler()
070: .getResourceManager();
071: try {
072: final ResourceKey derivedKey = manager.deriveKey(key,
073: href);
074: final Resource resource = manager.create(derivedKey,
075: null, StyleSheet.class);
076: getRootHandler().getDependencyCollector().add(resource);
077: styleSheet = (StyleSheet) resource.getResource();
078: } catch (ResourceKeyCreationException e) {
079: throw new ParseException("Unable to derive key for "
080: + key + " and " + href, getLocator());
081: } catch (ResourceCreationException e) {
082: Log.warn("Unable to parse resource for " + key
083: + " and " + href);
084: } catch (ResourceLoadingException e) {
085: Log.warn("Unable to load resource data for " + key
086: + " and " + href);
087: } catch (ResourceException e) {
088: Log.warn("Unable to load resource for " + key + " and "
089: + href);
090: }
091: }
092: }
093:
094: /**
095: * Done parsing.
096: *
097: * @throws SAXException if there is a parsing error.
098: */
099: protected void doneParsing() throws SAXException {
100: super .doneParsing();
101: if (this .styleSheet != null) {
102: return;
103: }
104:
105: final String styleText = getResult();
106: if (styleText.trim().length() == 0) {
107: return;
108: }
109: try {
110: final byte[] bytes = styleText.getBytes("UTF-8");
111:
112: final ResourceKey baseKey = getRootHandler().getSource();
113: final ResourceManager resourceManager = getRootHandler()
114: .getResourceManager();
115: final ResourceKey rawKey = resourceManager.createKey(bytes);
116:
117: final Resource resource = resourceManager.create(rawKey,
118: baseKey, StyleSheet.class);
119: this .styleSheet = (StyleSheet) resource.getResource();
120: } catch (Exception e) {
121: e.printStackTrace();
122: }
123: }
124:
125: /**
126: * Returns the object for this element.
127: *
128: * @return the object.
129: */
130: public Object getObject() {
131: return styleSheet;
132: }
133:
134: public StyleSheet getStyleSheet() {
135: return styleSheet;
136: }
137: }
|