/** * Copyright 2006 by Know-Center, Graz, Austria * PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a * joint initiative of the Federal Chancellery Austria and Graz University of * Technology. * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://www.osor.eu/eupl/ * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. * * $Id: SplitStrings.java,v 1.1 2006/08/30 14:02:35 wprinz Exp $ */ package at.knowcenter.wag.egov.egiz.pdf; import java.util.List; import org.apache.commons.lang.ArrayUtils; /** * Class that helps filling out the placeholders. * *

* This class treats a sequence of placeholder StringInfos like a continuous * data area that can be filled out regarding the boundaries. *

* * @author wprinz */ public class SplitStrings { /** * The byte used to fill unused bytes in the placeholders. */ public static final byte FILL_BYTE = ' '; /** * The underlying PDF. */ protected byte[] pdf = null; /** * The strings to be filled out. */ protected StringInfo[] strings = null; /** * The current string which is written to. */ protected int cur_string = 0; /** * The current write position within the current string. */ protected int cur_pos = 0; /** * Constructor. * * @param pdf * The underlying PDF. * @param strings * The strings to be filled out. */ public SplitStrings(byte[] pdf, List strings) { this.pdf = pdf; this.strings = new StringInfo[strings.size()]; for (int i = 0; i < strings.size(); i++) { StringInfo si = (StringInfo) strings.get(i); this.strings[i] = si; } } /** * Returns how many bytes are still available in the current string. * * @return Returns the number of bytes that are still available. (positive * integer, or zero if none are available) */ public int getAvailable() { return this.strings[this.cur_string].string_length - this.cur_pos; } /** * Tells, if the whole data would fit into the current string. * * @param data * The data to be matched for fitting * @return Returns true, if the whole data fits, false otherwise. */ public boolean fits(byte[] data) { return getAvailable() >= data.length; } /** * Writes the data into the current string. * *

* Note that the data must fit in. *

* @param data The data to be written. */ public void write(byte[] data) { if (!fits(data)) { throw new IllegalArgumentException("The data doesn't fit in."); } System.arraycopy(data, 0, this.pdf, this.strings[this.cur_string].string_start + this.cur_pos, data.length); this.cur_pos += data.length; if (data[data.length-1] == '\n') { this.cur_pos -= 1; // remove \n from output newline(); } } /** * Fills the current string with the fill character and moves on to the next * string. * */ public void newline() { int end = this.strings[this.cur_string].string_start + this.strings[this.cur_string].string_length; for (int i = this.strings[this.cur_string].string_start + this.cur_pos; i < end; i++) { pdf[i] = FILL_BYTE; } this.cur_string++; this.cur_pos = 0; } /** * Fills all rest bytes with the fill character. * *

* This should be called when everything is finished to fill all strings properly. *

*/ public void fillRest() { while (this.cur_string < this.strings.length) { newline(); } } /** * Tells, if the current line is valid. * @return Returns true, if this is a line that can be written to. */ public boolean isValidLine () { return this.cur_string < this.strings.length; } }