1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 package com.sun.star.report.pentaho.parser.rpt; 24 25 import com.sun.star.report.OfficeToken; 26 import com.sun.star.report.pentaho.OfficeNamespaces; 27 import com.sun.star.report.pentaho.model.ImageElement; 28 import com.sun.star.report.pentaho.parser.ElementReadHandler; 29 import com.sun.star.report.pentaho.parser.xlink.XLinkReadHandler; 30 31 import org.jfree.report.expressions.FormulaExpression; 32 import org.jfree.report.structure.Element; 33 34 import org.pentaho.reporting.libraries.xmlns.parser.IgnoreAnyChildReadHandler; 35 import org.pentaho.reporting.libraries.xmlns.parser.XmlReadHandler; 36 37 import org.xml.sax.Attributes; 38 import org.xml.sax.SAXException; 39 40 /** 41 * Deals with Image-content. There are two ways to specify the image; 42 * as formula or as static image data. 43 * 44 * @author Thomas Morgner 45 */ 46 public class ImageReadHandler extends ElementReadHandler 47 { 48 49 private final ImageElement contentElement; 50 private XLinkReadHandler xLinkReadHandler; 51 ImageReadHandler()52 public ImageReadHandler() 53 { 54 contentElement = new ImageElement(); 55 } 56 57 /** 58 * Starts parsing. 59 * 60 * @param attrs the attributes. 61 * @throws org.xml.sax.SAXException if there is a parsing error. 62 */ startParsing(final Attributes attrs)63 protected void startParsing(final Attributes attrs) throws SAXException 64 { 65 super.startParsing(attrs); 66 final String formula = attrs.getValue(OfficeNamespaces.OOREPORT_NS, "formula"); 67 // final String preserveIRI = attrs.getValue(OfficeNamespaces.OOREPORT_NS, OfficeToken.PRESERVE_IRI); 68 if (formula != null && formula.length() != 0) 69 { 70 // now, the evaulated content ends up in the 'content' attribute of the 71 // element. 72 final FormulaExpression valueExpression = new FormulaExpression(); 73 valueExpression.setFormula(formula); 74 contentElement.setFormula(valueExpression); 75 } 76 77 contentElement.setNamespace(OfficeNamespaces.FORM_NS); 78 contentElement.setType(OfficeToken.IMAGE); 79 } 80 81 /** 82 * Returns the handler for a child element. 83 * 84 * @param tagName the tag name. 85 * @param atts the attributes. 86 * @return the handler or null, if the tagname is invalid. 87 * @throws org.xml.sax.SAXException if there is a parsing error. 88 */ getHandlerForChild(final String uri, final String tagName, final Attributes atts)89 protected XmlReadHandler getHandlerForChild(final String uri, 90 final String tagName, 91 final Attributes atts) 92 throws SAXException 93 { 94 if (OfficeNamespaces.DRAWING_NS.equals(uri) && OfficeToken.IMAGE_DATA.equals(tagName)) 95 { 96 xLinkReadHandler = new XLinkReadHandler(); 97 return xLinkReadHandler; 98 } 99 100 if (OfficeNamespaces.OOREPORT_NS.equals(uri)) 101 { 102 // expect a report control. The control will modifiy the current 103 // element (as we do not separate the elements that strictly ..) 104 if ("report-control".equals(tagName)) 105 { 106 return new IgnoreAnyChildReadHandler(); 107 } 108 if ("report-element".equals(tagName)) 109 { 110 return new ReportElementReadHandler(contentElement); 111 } 112 } 113 return null; 114 } 115 116 /** 117 * Done parsing. 118 * 119 * @throws org.xml.sax.SAXException if there is a parsing error. 120 */ doneParsing()121 protected void doneParsing() throws SAXException 122 { 123 // if we have static content (as well or only), that one goes into the 124 // alternate-content attribute right now. It is part of the output target 125 // and style rules to deal with them properly .. 126 if (xLinkReadHandler != null) 127 { 128 contentElement.setAttribute(OfficeNamespaces.OOREPORT_NS, 129 "alternate-content", xLinkReadHandler.getUri()); 130 } 131 } 132 getElement()133 public Element getElement() 134 { 135 return contentElement; 136 } 137 } 138