Document Engine Example

The code snippet below represents essentially the full text of the document engine that handles the text document type in AwareIM:

Client side component

  1. public class TextEngineClient implements IDocEngineClient
  2. {
  3. public TextEngineClient()
  4. {
  5.  
  6. }
  7. /**
  8.   *@see IDocEngineClient#getTemplateType()
  9.   */
  10. public StringgetTemplateType()
  11. {
  12. return CommonConstants.TEMPLATE_TEXT;
  13. }
  14. /**
  15.   *@see IDocEngineClient#getEditCommand(String)
  16.   */
  17. public FileToEditgetEditCommand(StringtemplateFilePath)
  18. {
  19. return Utils.isWindows()?
  20. new FileToEdit("cmd.exe /k","\""+templateFilePath+"\""):
  21. new FileToEdit("",templateFilePath);
  22. }
  23. /**
  24.   *@see IDocEngineClient#getTemplateFileType()
  25.   */
  26. public StringgetTemplateFileType()
  27. {
  28. return "txt";
  29. }
  30. /**
  31.   *@see IDocEngineClient#canHaveExternalResources()
  32.   */
  33. public boolean canHaveExternalResources()
  34. {
  35. return false ;
  36. }
  37. /**
  38.   * @see IDocEngineClient#initialiseTemplate(DataPresentationTemplate,IDomainVersion)
  39.   */
  40. public void initialiseTemplate(DataPresentationTemplatetdef,IDomainVersiondomainVersion)
  41. throws Exception
  42. {
  43. // determine attribute paths used
  44. Stringtext=getTemplateText(tdef);
  45. if (text!= null )
  46. {
  47. // find if there are any tags
  48. VectortaggedParams=Utils.extractParameters(text,“<<”, “>>”);
  49. if (taggedParams!= null &&taggedParams.size()>0)
  50. DocumentEngineUtils.setAttributePaths(taggedParams,tdef);
  51. }
  52. }
  53. /**
  54.   * @see com.bas.shared.docengines.IDocEngineClient#elementsRenamed(com.bas.shared.domain.configuration.elements.DataPresentationTemplate,com.bas.shared.domain.configuration.misc.RenamedElements,com.bas.shared.ruleparser.INodeChangeHelper,IDomainVersion)
  55.   */
  56. public DataPresentationTemplateelementsRenamed(
  57. DataPresentationTemplateoldTemplate,
  58. RenamedElementsrenamedElements,
  59. INodeChangeHelperhelper,
  60. IDomainVersiondomainVersion)
  61. {
  62. Stringtext=getTemplateText(oldTemplate);
  63. if (text== null )
  64. return null ;
  65. // extract all parameters, convert them to ASTTagStatement
  66. // expressions, apply changes
  67. // and then replace parameters in the document
  68. VectortaggedParams=Utils.extractParameters(text,“<<”,“>>”);
  69. if (taggedParams== null ||taggedParams.size()==0)
  70. return null ;
  71. Vectormodified=DocumentEngineUtils.getModifiedParameters(taggedParams, renamedElements,helper);
  72. VectorwithTags=DocumentEngineUtils.appendTags(modified,"<<", ">>");
  73. StringnewText=Utils.replaceParameters (text,withTags,“<<”, “>>”);
  74. if (newText.equals(text))
  75. return null ;
  76. DataPresentationTemplatenewTemplate = (DataPresentationTemplate)oldTemplate.deepCopy();
  77. try
  78. {
  79. DocumentEngineUtils.setAttributePaths(modified, newTemplate);
  80. BinaryResourcebr= new BinaryResource
  81. (oldTemplate.getTemplate().getName(),newText.getBytes());
  82. newTemplate.setTemplate(br);
  83. }
  84. catch (Exception e)
  85. {
  86. return null ;
  87. } // shouldn't happen
  88. return newTemplate;
  89. }
  90. protected StringgetTemplateText(DataPresentationTemplatetemplate)
  91. {
  92. try
  93. {
  94. BinaryResourcetemplData=template.getTemplate();
  95. if (templData== null )
  96. return null ;
  97. byte []data=templData.getData();
  98. if (data== null )
  99. return null ;
  100. return new String(data);
  101. }
  102. catch (ResourceUnavailableExceptionre)
  103. {
  104. return null ;
  105. }
  106. }
  107. }
  108.  
  109. // Server side component
  110. public class TextDocumentEngine implements IDocumentEngine
  111. {
  112. public TextDocumentEngine()
  113. {
  114. }
  115. /**
  116.   *@see IDocumentEngine#getClientEngine()
  117.   */
  118. public IDocEngineClientgetClientEngine()
  119. {
  120. return new TextEngineClient();
  121. }
  122. /**
  123.   *@see IDocumentEngine#fillWithData(String,Map,IDocumentDataSource,DataPresentationTemplate,IDocEngineHelper,IDomainVersion,int,boolean)
  124.   */
  125. public BinaryResource[]fillWithData(Stringpath, Mapparams, IDocumentDataSourcedataSource, DataPresentationTemplatetdef, IDocEngineHelperhelper, DomainVersiondomainVersion, int outputFormat, boolean printResults)
  126. throws DocumentException
  127. {
  128. BinaryResourcetemplate=tdef.getLoadedTemplate();
  129. if (template== null )
  130. throw new DocumentException("No data in the presentation template");
  131. StringfileName=path+"/"+template.getName();
  132. try
  133. {
  134. FileinputFile= new File(fileName);
  135. Stringtext=FileUtils.readTextFile(fileName);
  136. // find if there are any tags
  137. VectortaggedParams= Utils.extractParameters(text);
  138. if (taggedParams== null || taggedParams.size()==0 || dataSource== null )
  139. {
  140. // just return the original template
  141. BinaryResource[]results= new BinaryResource[1];
  142. results[0]= new BinaryResource();
  143. results[0].setName(inputFile.getName());
  144. results[0].setData(FileUtils.readFile (fileName));
  145. return results;
  146. }
  147. if (dataSource== null )
  148. throw new DocumentException("Document template requires data, but the data source is not provided");
  149. // prepare the data source
  150. dataSource.reset();
  151. // for each group of records create resulting document
  152. // and resolve tagged fields insidethe text
  153. Vectorresources= new Vector();
  154. while (dataSource.next())
  155. {
  156. VectortagValues= new Vector
  157. (taggedParams.size());
  158. for (Iteratoriter=taggedParams.iterator();
  159. iter.hasNext();)
  160. {
  161. StringtagContents=(String)iter.next();
  162. Object o=dataSource.getExpressionValue (tagContents);
  163. tagValues.add(o== null ? null :o.toString());
  164. }
  165. StringnewText= Utils.replaceParameters(text, tagValues);
  166. if (printResults)
  167. printText(newText);
  168. BinaryResourcebr= new BinaryResource();
  169. br.setData(newText.getBytes());
  170. br.setName(inputFile.getName());
  171. resources.add(br);
  172. }
  173. if (resources.size()==0)
  174. return null ;
  175. BinaryResource[]results= new BinaryResource [resources.size()];
  176. int i=0;
  177. for (Iteratoriter=resources.iterator();iter.hasNext();
  178. ++i) results[i]=(BinaryResource)iter.next();
  179. return results;
  180. }
  181. catch (DocumentException de)
  182. {
  183. throw de;
  184. }
  185. /**
  186.   *@see IDocumentEngine#printFile()
  187.   */
  188. public void printFile(FilefileToPrint)
  189. throws DocumentException
  190. {
  191. try
  192. {
  193. Stringtext = FileUtils.readTextFile(fileToPrint.getPath());
  194. printText(text);
  195. }
  196. catch (Exception e)
  197. {
  198. throw new DocumentException(e.getMessage());
  199. }
  200. }
  201. protected void printText(Stringtext)
  202. throws DocumentException
  203. {
  204. try
  205. {
  206. PlainDocumentdoc= new PlainDocument();
  207. doc.insertString(0,text, null );
  208. TextDocumentPrintRendererprinter= new TextDocumentPrintRenderer();
  209. printer.print(doc, false );
  210. }
  211. catch (Exception e)
  212. {
  213. throw new DocumentException(e.getMessage());
  214. }
  215. }
  216. }
  217. }