PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

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