Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| docs:3500:0600:0650 [2023/04/05 06:37] – removed - external edit (Unknown date) 127.0.0.1 | docs:3500:0600:0650 [2025/06/12 02:36] (current) – Rename to AwareIM aware_support3 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | {{tag> | ||
| + | [< | ||
| + | ===== Document Engine Example ===== | ||
| + | The code snippet below represents essentially the full text of the document engine that handles the text document type in // | ||
| + | |||
| + | Client side component | ||
| + | |||
| + | <code java> | ||
| + | { | ||
| + | public TextEngineClient() | ||
| + | { | ||
| + | |||
| + | } | ||
| + | /** | ||
| + | *@see IDocEngineClient# | ||
| + | */ | ||
| + | public StringgetTemplateType() | ||
| + | { | ||
| + | return CommonConstants.TEMPLATE_TEXT; | ||
| + | } | ||
| + | /** | ||
| + | *@see IDocEngineClient# | ||
| + | */ | ||
| + | public FileToEditgetEditCommand(StringtemplateFilePath) | ||
| + | { | ||
| + | return Utils.isWindows()? | ||
| + | new FileToEdit(" | ||
| + | new FileToEdit("", | ||
| + | } | ||
| + | /** | ||
| + | *@see IDocEngineClient# | ||
| + | */ | ||
| + | public StringgetTemplateFileType() | ||
| + | { | ||
| + | return " | ||
| + | } | ||
| + | /** | ||
| + | *@see IDocEngineClient# | ||
| + | */ | ||
| + | public boolean canHaveExternalResources() | ||
| + | { | ||
| + | return false ; | ||
| + | } | ||
| + | /** | ||
| + | | ||
| + | */ | ||
| + | public void initialiseTemplate(DataPresentationTemplatetdef, | ||
| + | throws Exception | ||
| + | { | ||
| + | // determine attribute paths used | ||
| + | Stringtext=getTemplateText(tdef); | ||
| + | if (text!= null ) | ||
| + | { | ||
| + | // find if there are any tags | ||
| + | VectortaggedParams=Utils.extractParameters(text, | ||
| + | if (taggedParams!= null && | ||
| + | DocumentEngineUtils.setAttributePaths(taggedParams, | ||
| + | } | ||
| + | } | ||
| + | /** | ||
| + | * @see com.bas.shared.docengines.IDocEngineClient# | ||
| + | */ | ||
| + | public DataPresentationTemplateelementsRenamed( | ||
| + | DataPresentationTemplateoldTemplate, | ||
| + | RenamedElementsrenamedElements, | ||
| + | INodeChangeHelperhelper, | ||
| + | IDomainVersiondomainVersion) | ||
| + | { | ||
| + | Stringtext=getTemplateText(oldTemplate); | ||
| + | if (text== null ) | ||
| + | return null ; | ||
| + | // extract all parameters, convert them to ASTTagStatement | ||
| + | // expressions, | ||
| + | // and then replace parameters in the document | ||
| + | VectortaggedParams=Utils.extractParameters(text, | ||
| + | if (taggedParams== null ||taggedParams.size()==0) | ||
| + | return null ; | ||
| + | Vectormodified=DocumentEngineUtils.getModifiedParameters(taggedParams, | ||
| + | VectorwithTags=DocumentEngineUtils.appendTags(modified,"<<", | ||
| + | StringnewText=Utils.replaceParameters (text, | ||
| + | if (newText.equals(text)) | ||
| + | return null ; | ||
| + | DataPresentationTemplatenewTemplate = (DataPresentationTemplate)oldTemplate.deepCopy(); | ||
| + | try | ||
| + | { | ||
| + | DocumentEngineUtils.setAttributePaths(modified, | ||
| + | BinaryResourcebr= new BinaryResource | ||
| + | (oldTemplate.getTemplate().getName(), | ||
| + | newTemplate.setTemplate(br); | ||
| + | } | ||
| + | catch (Exception e) | ||
| + | { | ||
| + | return null ; | ||
| + | } // shouldn' | ||
| + | return newTemplate; | ||
| + | } | ||
| + | protected StringgetTemplateText(DataPresentationTemplatetemplate) | ||
| + | { | ||
| + | try | ||
| + | { | ||
| + | BinaryResourcetemplData=template.getTemplate(); | ||
| + | if (templData== null ) | ||
| + | return null ; | ||
| + | byte []data=templData.getData(); | ||
| + | if (data== null ) | ||
| + | return null ; | ||
| + | return new String(data); | ||
| + | } | ||
| + | catch (ResourceUnavailableExceptionre) | ||
| + | { | ||
| + | return null ; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // Server side component | ||
| + | public class TextDocumentEngine implements IDocumentEngine | ||
| + | { | ||
| + | public TextDocumentEngine() | ||
| + | { | ||
| + | } | ||
| + | /** | ||
| + | *@see IDocumentEngine# | ||
| + | */ | ||
| + | public IDocEngineClientgetClientEngine() | ||
| + | { | ||
| + | return new TextEngineClient(); | ||
| + | } | ||
| + | /** | ||
| + | *@see IDocumentEngine# | ||
| + | */ | ||
| + | public BinaryResource[]fillWithData(Stringpath, | ||
| + | throws DocumentException | ||
| + | { | ||
| + | BinaryResourcetemplate=tdef.getLoadedTemplate(); | ||
| + | if (template== null ) | ||
| + | throw new DocumentException(" | ||
| + | StringfileName=path+"/" | ||
| + | try | ||
| + | { | ||
| + | FileinputFile= new File(fileName); | ||
| + | Stringtext=FileUtils.readTextFile(fileName); | ||
| + | // find if there are any tags | ||
| + | VectortaggedParams= Utils.extractParameters(text); | ||
| + | if (taggedParams== null || taggedParams.size()==0 || dataSource== null ) | ||
| + | { | ||
| + | // just return the original template | ||
| + | BinaryResource[]results= new BinaryResource[1]; | ||
| + | results[0]= new BinaryResource(); | ||
| + | results[0].setName(inputFile.getName()); | ||
| + | results[0].setData(FileUtils.readFile (fileName)); | ||
| + | return results; | ||
| + | } | ||
| + | if (dataSource== null ) | ||
| + | throw new DocumentException(" | ||
| + | // prepare the data source | ||
| + | dataSource.reset(); | ||
| + | // for each group of records create resulting document | ||
| + | // and resolve tagged fields insidethe text | ||
| + | Vectorresources= new Vector(); | ||
| + | while (dataSource.next()) | ||
| + | { | ||
| + | VectortagValues= new Vector | ||
| + | (taggedParams.size()); | ||
| + | for (Iteratoriter=taggedParams.iterator(); | ||
| + | iter.hasNext(); | ||
| + | { | ||
| + | StringtagContents=(String)iter.next(); | ||
| + | Object o=dataSource.getExpressionValue (tagContents); | ||
| + | tagValues.add(o== null ? null : | ||
| + | } | ||
| + | StringnewText= Utils.replaceParameters(text, | ||
| + | if (printResults) | ||
| + | printText(newText); | ||
| + | BinaryResourcebr= new BinaryResource(); | ||
| + | br.setData(newText.getBytes()); | ||
| + | br.setName(inputFile.getName()); | ||
| + | resources.add(br); | ||
| + | } | ||
| + | if (resources.size()==0) | ||
| + | return null ; | ||
| + | BinaryResource[]results= new BinaryResource [resources.size()]; | ||
| + | int i=0; | ||
| + | for (Iteratoriter=resources.iterator(); | ||
| + | ++i) results[i]=(BinaryResource)iter.next(); | ||
| + | return results; | ||
| + | } | ||
| + | catch (DocumentException de) | ||
| + | { | ||
| + | throw de; | ||
| + | } | ||
| + | /** | ||
| + | *@see IDocumentEngine# | ||
| + | */ | ||
| + | public void printFile(FilefileToPrint) | ||
| + | throws DocumentException | ||
| + | { | ||
| + | try | ||
| + | { | ||
| + | Stringtext = FileUtils.readTextFile(fileToPrint.getPath()); | ||
| + | printText(text); | ||
| + | } | ||
| + | catch (Exception e) | ||
| + | { | ||
| + | throw new DocumentException(e.getMessage()); | ||
| + | } | ||
| + | } | ||
| + | protected void printText(Stringtext) | ||
| + | throws DocumentException | ||
| + | { | ||
| + | try | ||
| + | { | ||
| + | PlainDocumentdoc= new PlainDocument(); | ||
| + | doc.insertString(0, | ||
| + | TextDocumentPrintRendererprinter= new TextDocumentPrintRenderer(); | ||
| + | printer.print(doc, | ||
| + | } | ||
| + | catch (Exception e) | ||
| + | { | ||
| + | throw new DocumentException(e.getMessage()); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||