PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

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