PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/SpudSoft BIRT Excel Emitters/src/uk/co/spudsoft/birt/emitters/excel/handlers/AbstractHandler.java

https://bitbucket.org/xunchangguo/spudsoft-birt-excel-emitters
Java | 343 lines | 273 code | 36 blank | 34 comment | 46 complexity | 8100753484ab7d06af0302bceb61252a MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. /*************************************************************
  2. * (C) Copyright 2011, 2012 James Talbut.
  3. * jim-emitters@spudsoft.co.uk
  4. *
  5. * This file is part of The SpudSoft BIRT Excel Emitters.
  6. * The SpudSoft BIRT Excel Emitters are free software: you can
  7. * redistribute them and/or modify them under the terms of the
  8. * GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * The SpudSoft BIRT Excel Emitters are distributed in the hope
  13. * that they will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with the SpudSoft BIRT Excel Emitters.
  20. * If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. *************************************************************/
  23. package uk.co.spudsoft.birt.emitters.excel.handlers;
  24. import org.apache.poi.ss.SpreadsheetVersion;
  25. import org.apache.poi.ss.formula.FormulaParseException;
  26. import org.apache.poi.ss.usermodel.Name;
  27. import org.apache.poi.ss.util.CellReference;
  28. import org.apache.poi.ss.util.CellReference.NameType;
  29. import org.eclipse.birt.core.exception.BirtException;
  30. import org.eclipse.birt.report.engine.content.IAutoTextContent;
  31. import org.eclipse.birt.report.engine.content.ICellContent;
  32. import org.eclipse.birt.report.engine.content.IContainerContent;
  33. import org.eclipse.birt.report.engine.content.IContent;
  34. import org.eclipse.birt.report.engine.content.IDataContent;
  35. import org.eclipse.birt.report.engine.content.IForeignContent;
  36. import org.eclipse.birt.report.engine.content.IGroupContent;
  37. import org.eclipse.birt.report.engine.content.IImageContent;
  38. import org.eclipse.birt.report.engine.content.ILabelContent;
  39. import org.eclipse.birt.report.engine.content.IListBandContent;
  40. import org.eclipse.birt.report.engine.content.IListContent;
  41. import org.eclipse.birt.report.engine.content.IListGroupContent;
  42. import org.eclipse.birt.report.engine.content.IPageContent;
  43. import org.eclipse.birt.report.engine.content.IRowContent;
  44. import org.eclipse.birt.report.engine.content.IStyledElement;
  45. import org.eclipse.birt.report.engine.content.ITableBandContent;
  46. import org.eclipse.birt.report.engine.content.ITableContent;
  47. import org.eclipse.birt.report.engine.content.ITableGroupContent;
  48. import org.eclipse.birt.report.engine.content.ITextContent;
  49. import org.eclipse.birt.report.engine.css.engine.StyleConstants;
  50. import org.eclipse.birt.report.engine.css.engine.value.css.CSSConstants;
  51. import org.w3c.dom.css.CSSValue;
  52. import uk.co.spudsoft.birt.emitters.excel.HandlerState;
  53. import uk.co.spudsoft.birt.emitters.excel.framework.Logger;
  54. public class AbstractHandler implements IHandler {
  55. protected Logger log;
  56. protected IContent element;
  57. protected IHandler parent;
  58. private CSSValue backgroundColour;
  59. public AbstractHandler(Logger log, IHandler parent, IContent element) {
  60. this.log = log;
  61. this.parent = parent;
  62. this.element = element;
  63. }
  64. public void notifyHandler(HandlerState state) {
  65. }
  66. public String getPath() {
  67. if( parent != null ) {
  68. return this.getClass().getSimpleName() + "/" + parent.getPath();
  69. } else {
  70. return this.getClass().getSimpleName();
  71. }
  72. }
  73. public IHandler getParent() {
  74. return parent;
  75. }
  76. @SuppressWarnings("unchecked")
  77. public <T extends IHandler> T getAncestor(Class<T> clazz) {
  78. if( parent != null ) {
  79. if( clazz.isInstance(parent) ) {
  80. return (T)parent;
  81. } else {
  82. return parent.getAncestor(clazz);
  83. }
  84. }
  85. return null;
  86. }
  87. public CSSValue getBackgroundColour() {
  88. if( backgroundColour != null ) {
  89. return backgroundColour;
  90. }
  91. if( element != null ) {
  92. CSSValue elemColour = element.getComputedStyle().getProperty( StyleConstants.STYLE_BACKGROUND_COLOR );
  93. if( ( elemColour != null ) && ! CSSConstants.CSS_TRANSPARENT_VALUE.equals( elemColour.getCssText() ) ) {
  94. backgroundColour = elemColour;
  95. }
  96. }
  97. if( ( parent != null ) && ( backgroundColour == null ) ) {
  98. backgroundColour = parent.getBackgroundColour();
  99. }
  100. return backgroundColour;
  101. }
  102. protected static String getStyleProperty( IStyledElement element, int property, String defaultValue ) {
  103. CSSValue value = element.getComputedStyle().getProperty(property);
  104. if( value != null ) {
  105. return value.getCssText();
  106. } else {
  107. return defaultValue;
  108. }
  109. }
  110. protected static String prepareName( String name ) {
  111. char c = name.charAt(0);
  112. boolean requirePreparation = (!(c == '_' || Character.isLetter(c)) || name.indexOf(' ') != -1);
  113. if( !requirePreparation ) {
  114. for( int i = 1; i < name.length(); ++i ) {
  115. c = name.charAt(i);
  116. if(!(Character.isLetter(c) || Character.isDigit(c) || c == '_' )) {
  117. requirePreparation = true;
  118. break;
  119. }
  120. }
  121. }
  122. if( requirePreparation ) {
  123. name = name.trim();
  124. char chars[] = name.toCharArray();
  125. for( int i = 0; i < name.length(); ++i ) {
  126. c = chars[i];
  127. if(!(Character.isLetter(c) || Character.isDigit(c) || c == '_' )) {
  128. chars[i] = '_';
  129. }
  130. }
  131. name = new String(chars);
  132. }
  133. NameType refType = CellReference.classifyCellReference( name, SpreadsheetVersion.EXCEL2007 );
  134. if( ( NameType.CELL == refType ) || ( NameType.COLUMN == refType ) || ( NameType.ROW == refType ) ) {
  135. name = "_" + name;
  136. }
  137. return name;
  138. }
  139. protected void createName(HandlerState state, String bookmark, int row1, int col1, int row2, int col2 ) {
  140. CellReference crFirst = new CellReference( state.currentSheet.getSheetName(), row1, col1, true, true );
  141. CellReference crLast = new CellReference( row2, col2, true, true );
  142. String formula = crFirst.formatAsString() + ":" + crLast.formatAsString();
  143. Name name = state.currentSheet.getWorkbook().getName(bookmark);
  144. if( name == null ) {
  145. name = state.currentSheet.getWorkbook().createName();
  146. name.setNameName( bookmark );
  147. name.setRefersToFormula( formula );
  148. } else {
  149. String existingFormula = name.getRefersToFormula();
  150. try {
  151. name.setRefersToFormula(existingFormula + "," + formula);
  152. } catch( FormulaParseException ex ) {
  153. log.warn( 0, "Unable to add \"" + formula + "\" to name (\"" + bookmark + "\") with existing formula: " + existingFormula, ex );
  154. }
  155. }
  156. }
  157. public void startPage(HandlerState state, IPageContent page) throws BirtException {
  158. NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".startPage" );
  159. log.error(0, "Method not implemented", ex);
  160. throw ex;
  161. }
  162. public void endPage(HandlerState state, IPageContent page) throws BirtException{
  163. NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".endPage" );
  164. log.error(0, "Method not implemented", ex);
  165. throw ex;
  166. }
  167. public void startTable(HandlerState state, ITableContent table) throws BirtException {
  168. NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".startTable" );
  169. log.error(0, "Method not implemented", ex);
  170. throw ex;
  171. }
  172. public void endTable(HandlerState state, ITableContent table) throws BirtException {
  173. NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".endTable" );
  174. log.error(0, "Method not implemented", ex);
  175. throw ex;
  176. }
  177. public void startTableBand(HandlerState state, ITableBandContent band) throws BirtException {
  178. // NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".startTableBand" );
  179. // log.error(0, "Method not implemented", ex);
  180. // throw ex;
  181. }
  182. public void endTableBand(HandlerState state, ITableBandContent band) throws BirtException {
  183. // NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".endTableBand" );
  184. // log.error(0, "Method not implemented", ex);
  185. // throw ex;
  186. }
  187. public void startRow(HandlerState state, IRowContent row) throws BirtException {
  188. NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".startRow" );
  189. log.error(0, "Method not implemented", ex);
  190. throw ex;
  191. }
  192. public void endRow(HandlerState state, IRowContent row) throws BirtException {
  193. NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".endRow" );
  194. log.error(0, "Method not implemented", ex);
  195. throw ex;
  196. }
  197. public void startCell(HandlerState state, ICellContent cell) throws BirtException {
  198. NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".startCell" );
  199. log.error(0, "Method not implemented", ex);
  200. throw ex;
  201. }
  202. public void endCell(HandlerState state, ICellContent cell) throws BirtException {
  203. NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".endCell" );
  204. log.error(0, "Method not implemented", ex);
  205. throw ex;
  206. }
  207. public void startList(HandlerState state, IListContent list) throws BirtException {
  208. NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".startList" );
  209. log.error(0, "Method not implemented", ex);
  210. throw ex;
  211. }
  212. public void endList(HandlerState state, IListContent list) throws BirtException {
  213. NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".endList" );
  214. log.error(0, "Method not implemented", ex);
  215. throw ex;
  216. }
  217. public void startListBand(HandlerState state, IListBandContent listBand) throws BirtException {
  218. NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".startListBand" );
  219. log.error(0, "Method not implemented", ex);
  220. throw ex;
  221. }
  222. public void endListBand(HandlerState state, IListBandContent listBand) throws BirtException {
  223. NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".endListBand" );
  224. log.error(0, "Method not implemented", ex);
  225. throw ex;
  226. }
  227. public void startContainer(HandlerState state, IContainerContent container) throws BirtException {
  228. // NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".startContainer" );
  229. // log.error(0, "Method not implemented", ex);
  230. // throw ex;
  231. }
  232. public void endContainer(HandlerState state, IContainerContent container) throws BirtException {
  233. // NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".endContainer" );
  234. // log.error(0, "Method not implemented", ex);
  235. // throw ex;
  236. }
  237. public void startContent(HandlerState state, IContent content) throws BirtException {
  238. NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".startContent" );
  239. log.error(0, "Method not implemented", ex);
  240. throw ex;
  241. }
  242. public void endContent(HandlerState state, IContent content) throws BirtException {
  243. NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".endContent" );
  244. log.error(0, "Method not implemented", ex);
  245. throw ex;
  246. }
  247. public void startGroup(HandlerState state, IGroupContent group) throws BirtException {
  248. NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".startGroup" );
  249. log.error(0, "Method not implemented", ex);
  250. throw ex;
  251. }
  252. public void endGroup(HandlerState state, IGroupContent group) throws BirtException {
  253. NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".endGroup" );
  254. log.error(0, "Method not implemented", ex);
  255. throw ex;
  256. }
  257. public void startTableGroup(HandlerState state, ITableGroupContent group) throws BirtException {
  258. NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".startTableGroup" );
  259. log.error(0, "Method not implemented", ex);
  260. throw ex;
  261. }
  262. public void endTableGroup(HandlerState state, ITableGroupContent group) throws BirtException {
  263. NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".endTableGroup" );
  264. log.error(0, "Method not implemented", ex);
  265. throw ex;
  266. }
  267. public void startListGroup(HandlerState state, IListGroupContent group) throws BirtException {
  268. NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".startListGroup" );
  269. log.error(0, "Method not implemented", ex);
  270. throw ex;
  271. }
  272. public void endListGroup(HandlerState state, IListGroupContent group) throws BirtException {
  273. NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".endListGroup" );
  274. log.error(0, "Method not implemented", ex);
  275. throw ex;
  276. }
  277. public void emitText(HandlerState state, ITextContent text) throws BirtException {
  278. NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".emitText" );
  279. log.error(0, "Method not implemented", ex);
  280. throw ex;
  281. }
  282. public void emitData(HandlerState state, IDataContent data) throws BirtException {
  283. NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".emitData" );
  284. log.error(0, "Method not implemented", ex);
  285. throw ex;
  286. }
  287. public void emitLabel(HandlerState state, ILabelContent label) throws BirtException {
  288. NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".emitLabel" );
  289. log.error(0, "Method not implemented", ex);
  290. throw ex;
  291. }
  292. public void emitAutoText(HandlerState state, IAutoTextContent autoText) throws BirtException {
  293. NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".emitAutoText" );
  294. log.error(0, "Method not implemented", ex);
  295. throw ex;
  296. }
  297. public void emitForeign(HandlerState state, IForeignContent foreign) throws BirtException {
  298. NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".emitForeign" );
  299. log.error(0, "Method not implemented", ex);
  300. throw ex;
  301. }
  302. public void emitImage(HandlerState state, IImageContent image) throws BirtException {
  303. NoSuchMethodError ex = new NoSuchMethodError( "Method not implemented: " + this.getClass().getSimpleName() + ".emitImage" );
  304. log.error(0, "Method not implemented", ex);
  305. throw ex;
  306. }
  307. }