All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.opensagres.xdocreport.document.docx.preprocessor.sax.PBufferedRegion Maven / Gradle / Ivy

There is a newer version: 2.0.6
Show newest version
/**
 * Copyright (C) 2011-2015 The XDocReport Team 
 *
 * All rights reserved.
 *
 * Permission is hereby granted, free  of charge, to any person obtaining
 * a  copy  of this  software  and  associated  documentation files  (the
 * "Software"), to  deal in  the Software without  restriction, including
 * without limitation  the rights to  use, copy, modify,  merge, publish,
 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
 * permit persons to whom the Software  is furnished to do so, subject to
 * the following conditions:
 *
 * The  above  copyright  notice  and  this permission  notice  shall  be
 * included in all copies or substantial portions of the Software.
 *
 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package fr.opensagres.xdocreport.document.docx.preprocessor.sax;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.xml.sax.Attributes;

import fr.opensagres.xdocreport.document.preprocessor.sax.BufferedElement;
import fr.opensagres.xdocreport.document.preprocessor.sax.ISavable;

/**
 * 
 * 
 *  
 *  
 *    
 *   
 *   
 *  
 *  
 *    
 *   
 *    
 *   
 *  
 *  
 *    
 *   
 *   MERGEFIELD Titre 
 *   
 *  
 *  
 *    
 *   
 *    
 *   
 *  
 *  
 *    
 *    
 *   
 *   �Titre� 
 *   
 *  
 *  
 *    
 *   
 *    
 *   
 *   
 * 
 * 
*/ public class PBufferedRegion extends BufferedElement { private static final String BEGIN = "begin"; private static final String SEPARATE = "separate"; private static final String END = "end"; private List rBufferedRegions = new ArrayList(); private final DocxBufferedDocument document; private boolean containsField; public PBufferedRegion( DocxBufferedDocument document, BufferedElement parent, String uri, String localName, String name, Attributes attributes ) { super( parent, uri, localName, name, attributes ); this.document = document; this.containsField = false; } @Override public void addRegion( ISavable region ) { if ( region instanceof RBufferedRegion ) { rBufferedRegions.add( (RBufferedRegion) region ); } else { super.addRegion( region ); } } public void process() { Collection toRemove = new ArrayList(); boolean remove = false; boolean fieldNameSetted = false; String fieldName = null; boolean rReseted = false; for ( int i = 0; i < rBufferedRegions.size(); i++ ) { RBufferedRegion rBufferedRegion = rBufferedRegions.get( i ); if ( BEGIN.equals( rBufferedRegion.getFldCharType() ) ) { int nextIndexAfterBegin = i + 1; RBufferedRegion nextR = rBufferedRegions.get( nextIndexAfterBegin ); // Remove the begin w:r Next w:r // 1) has fieldName : MERGEFIELD // Titre // 2) was reseted (if @before-row, @after-row ... if the // MERGEFIELD name starts with thoses tokens. rReseted = nextR.isReseted(); if ( rReseted ) { toRemove.add( rBufferedRegion ); toRemove.add( nextR ); remove = true; } else { // Test if there are several // MERGEFIELD // ${ctx.serviceclientdeparture} \* M // // // ERGEFORMAT // // List rMerged = new ArrayList(); StringBuilder mergedInstrText = new StringBuilder(); for ( int j = nextIndexAfterBegin; j < rBufferedRegions.size(); j++ ) { RBufferedRegion r = rBufferedRegions.get( j ); if ( r.hasInstrText() ) { rMerged.add( r ); mergedInstrText.append( r.getOriginalInstrText() ); } else { break; } } if ( rMerged.size() > 0 ) { RBufferedRegion firstR = rMerged.get( 0 ); firstR.setInstrText( mergedInstrText.toString(), firstR.getFieldAsTextStyling() ); fieldName = firstR.getFieldName(); if ( fieldName != null ) { if ( document.processScriptBeforeAfter( firstR ) ) { rReseted = true; } for ( RBufferedRegion r : rMerged ) { toRemove.add( r ); i++; } toRemove.add( rBufferedRegion ); remove = true; } } } } else if ( SEPARATE.equals( rBufferedRegion.getFldCharType() ) && remove ) { // begin w:r was removed, remove the separate w:r. toRemove.add( rBufferedRegion ); } else if ( END.equals( rBufferedRegion.getFldCharType() ) && remove ) { // begin w:r was removed, remove the end w:r. toRemove.add( rBufferedRegion ); remove = false; fieldName = null; fieldNameSetted = false; rReseted = false; } else if ( fieldName != null || rReseted ) { // other w:r if ( !fieldNameSetted && !rReseted ) { // fieldName was not setted and w:r was not rested, modify // the t content with fieldName rBufferedRegion.setTContent(0, fieldName ); fieldNameSetted = true; containsField = true; } else { // remove if (!rBufferedRegion.isContainsNote()) { toRemove.add( rBufferedRegion ); } } } } rBufferedRegions.removeAll( toRemove ); super.removeAll( toRemove ); } public boolean isContainsField() { // TODO Auto-generated method stub return false; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy