// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov. // Jad home page: http://kpdus.tripod.com/jad.html // Decompiler options: packimports(3) // Source File Name: MergeTemplateXMLEngineImpl.java package net.opentrends.openframe.services.merging.impl; import java.io.*; import java.util.*; import net.opentrends.openframe.services.exceptions.ExceptionDetails; import net.opentrends.openframe.services.exceptions.Layer; import net.opentrends.openframe.services.logging.Log; import net.opentrends.openframe.services.logging.LoggingService; import net.opentrends.openframe.services.merging.MergeTemplateEngine; import net.opentrends.openframe.services.merging.exception.MergingServiceException; import org.dom4j.*; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class MergeTemplateXMLEngineImpl implements MergeTemplateEngine { public MergeTemplateXMLEngineImpl() { wTemplate = null; iniciBookmark = null; fiBookmark = null; wDocuments = null; logService = null; foundBeginMark = false; bookmarkBeforeReplace = ""; } public ByteArrayOutputStream[] mergeTemplate(InputStream pTemplate, List pDades) throws Exception { if(pTemplate == null || pDades == null) return null; wDocuments = new ByteArrayOutputStream[pDades.size()]; writeToLog("Iniciando proceso de validaci\363n de la plantilla XML"); isValidTemplate(pTemplate); writeToLog("Proceso de validaci\363n finalizado OK"); for(int i = 0; i < pDades.size(); i++) { Map wDadesDoc = (Map)pDades.get(i); wDocuments[i] = mergeTemplate((Document)wTemplate.clone(), wDadesDoc); writeToLog("Generado documento " + (i + 1) + " de " + pDades.size()); } return wDocuments; } private void writeToLog(String pMessage) { if(logService != null) logService.getLog(getClass()).info("SERVEI MERGING -- " + pMessage); } private boolean isValidTemplate(InputStream pTemplate) throws Exception { parseWithSAX(pTemplate); Element wRoot = wTemplate.getRootElement(); if(!wRoot.getName().equals("wordDocument")) { ExceptionDetails wDetails = new ExceptionDetails("net.opentrends.openFrame.services.merging.wrongXML", null, Layer.SERVICES); throw new MergingServiceException(wDetails); } else { return true; } } private void parseWithSAX(InputStream pTemplate) throws Exception { try { SAXReader xmlReader = new SAXReader(); wTemplate = xmlReader.read(pTemplate); } catch(DocumentException e) { ExceptionDetails wDetails = new ExceptionDetails("net.opentrends.openFrame.services.merging.gral", new Object[] { e.getMessage() }, Layer.SERVICES); throw new MergingServiceException(wDetails); } } private ByteArrayOutputStream mergeTemplate(Document pTemplate, Map pDades) throws Exception { ExceptionDetails wDetails; try { recorrerDoc(pTemplate, pDades); ByteArrayOutputStream wOut = new ByteArrayOutputStream(); XMLWriter xmlWriter = new XMLWriter(wOut); xmlWriter.write(pTemplate); xmlWriter.flush(); return wOut; } catch(UnsupportedEncodingException e) { wDetails = new ExceptionDetails("net.opentrends.openFrame.services.merging.gral", new Object[] { e.getMessage() }, Layer.SERVICES); throw new MergingServiceException(wDetails); } catch(IOException e) { wDetails = new ExceptionDetails("net.opentrends.openFrame.services.merging.gral", new Object[] { e.getMessage() }, Layer.SERVICES); } throw new MergingServiceException(wDetails); } private void recorrerDoc(Document pDocument, Map pDades) { recorrerDoc(pDocument.getRootElement(), pDades); } private void recorrerDoc(Element pElement, Map pDades) { if(pElement == null) return; int i = 0; for(int size = pElement.nodeCount(); i < size; i++) { Node wNode = pElement.node(i); if(wNode instanceof Element) { if(wNode.getName().equals("t")) { String sText = wNode.getText(); if(sText.indexOf(iniciBookmark) != -1 || sText.indexOf(fiBookmark) != -1 || foundBeginMark) ((Element)wNode).setText(replaceDadesBookmarks(sText, pDades)); } recorrerDoc((Element)wNode, pDades); } } } private String replaceDadesBookmarks(String pText, Map pDades) { String sText = pText; String sTextARetornar = ""; sTextARetornar = replaceBookmark(sText, pDades); if(foundBeginMark) { int iPosEnds = sTextARetornar.indexOf(fiBookmark); if(iPosEnds == -1) { bookmarkBeforeReplace += sTextARetornar; sTextARetornar = ""; } else { bookmarkBeforeReplace += sTextARetornar.substring(0, iPosEnds); sTextARetornar = (String)pDades.get(bookmarkBeforeReplace.toUpperCase()) + sTextARetornar.substring(iPosEnds + 2); foundBeginMark = false; bookmarkBeforeReplace = ""; } } int iPosBegin = sTextARetornar.indexOf(iniciBookmark); if(iPosBegin != -1) { foundBeginMark = true; bookmarkBeforeReplace = sTextARetornar.substring(iPosBegin + 2); sTextARetornar = sTextARetornar.substring(0, iPosBegin); } return sTextARetornar; } private String replaceBookmark(String pText, Map pDades) { if(pText == null) return null; String sTextFusionat = pText; for(Iterator it = pDades.keySet().iterator(); it.hasNext();) { String sKey = (String)it.next(); String sBookmark = iniciBookmark + sKey + fiBookmark; if(sTextFusionat.indexOf(sBookmark) != -1) sTextFusionat = sTextFusionat.replaceAll(sBookmark, (String)pDades.get(sKey)); } return sTextFusionat; } public String getFiBookmark() { return fiBookmark; } public void setFiBookmark(String fiBookmark) { this.fiBookmark = fiBookmark; } public String getIniciBookmark() { return iniciBookmark; } public void setIniciBookmark(String iniciBookmark) { this.iniciBookmark = iniciBookmark; } public LoggingService getLogService() { return logService; } public void setLogService(LoggingService logService) { this.logService = logService; } private final String sNom_Root_Element = "wordDocument"; private final String sNom_Text_Element = "t"; private Document wTemplate; public String iniciBookmark; public String fiBookmark; private ByteArrayOutputStream wDocuments[]; private LoggingService logService; private boolean foundBeginMark; private String bookmarkBeforeReplace; }