/** * 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: SignatureBlock.java,v 1.4 2006/10/31 08:18:56 wprinz Exp $ */ package at.knowcenter.wag.egov.egiz.sig; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Vector; import at.knowcenter.wag.egov.egiz.exceptions.SignatureException; import at.knowcenter.wag.egov.egiz.exceptions.SignatureTypesException; /** * This method is to analyse a signature block string. It searches for * configured signature types while compairing defined key words with the text. * * @deprecated Use AbsoluteTextSignature instead. * * @author wlackner */ public class SignatureBlock { /** * Start index of the signature block text. */ private int startIndex_ = -1; /** * End index of the signature block text. */ private int endIndex_ = -1; /** * The type of the signature block. */ private String type_ = null; /** * The signature type definition object. */ private SignatureTypeDefinition sigTypeDef_ = null; /** * The signature block string. */ private String signatureString_ = null; /** * The signature object build by the signature string using the signture * definition. */ private SignatureObject signatureObject_ = null; /** * A list of configured signature types. */ List signatureTypes_ = null; /** * A list of found keys in the signature block string. */ Map foundKeys_ = new HashMap(); int maxSize_ = -1; /** * The default constructor to analyse a signature block string. It uses a * predefined signature type list to assign the text block to signature type. * The analyse method have to be call separately --> * separateBlockFromRawText() * * @param signatureTypes */ public SignatureBlock(List signatureTypes) { signatureTypes_ = signatureTypes; } /** * This method checks if all required keys are found in the signature block * string. * * @param foundKeys * the keys that are found in the singnature block string * @return true if all required keys are found, false otherwise */ private boolean checkRequiredFields(Map foundKeys) { String[] req_keys = SignatureTypes.REQUIRED_SIG_KEYS; for (int req_idx = 0; req_idx < req_keys.length; req_idx++) { String key = req_keys[req_idx]; // SIG_ID could be optional --> only set in BKU signed documents if (key.equals(SignatureTypes.SIG_ID)) { continue; } // logger.debug("check:" + key + "=" + foundKeys.get(key)); if (foundKeys.get(key) == null) { return false; } } return true; } /** * This method is the base method to analyse a raw text separating a signature * block string from the raw text. It searches for corresponding signature * types from back to front in the raw text. Therefore a revert list of * multiple signations can be extracted calling this method more than one * times. The method extracts the start and end postition of a found signature * block and extracts all keys used in that block. If all required fields are * found a successful separation is done and can be access calling the method * getStartIndex, getEndIndex, getType, getSignatureObject. * * @param rawText * the raw text to separate a signature block from * @return true if a separation has done successful false if no signature * block can be found */ public boolean separateBlockFromRawText(String rawText, boolean old_style) { endIndex_ = rawText.length(); boolean found_type = false; for (int sig_type_idx = 0; sig_type_idx < signatureTypes_.size(); sig_type_idx++) { int last_index = endIndex_; SignatureTypeDefinition sig_type_def = (SignatureTypeDefinition) signatureTypes_.get(sig_type_idx); //logger.debug("Try sep type:" + sig_type_def.getType()); Vector keys = sig_type_def.getRevertSortedKeys(); Vector captions = sig_type_def.getRevertSortedCaptions(); Map found_keys = new HashMap(); for (int key_idx = 0; key_idx < keys.size(); key_idx++) { String key = (String) keys.get(key_idx); if (old_style && key.equals(SignatureTypes.SIG_KZ)) { // If separating the old style way - skip The "Kennzeichnung" // key, because it wasn't present in old profiles. continue; } String caption = (String) captions.get(key_idx); int found_idx = rawText.lastIndexOf(caption); //logger.debug("Try find:" + sig_type_def.getType() + "." + key + "." + caption + " at=" + found_idx); if (found_idx >= 0 && found_idx < last_index) { if (key.equals(SignatureTypes.SIG_ID)) { //logger.debug("store SIG_ID, but don't decrease last index:" + sig_type_def.getType() + "." + key + "." + caption + " at=" + found_idx); found_keys.put(key, new Integer(found_idx)); // don't decrease last index as SIG_ID is not necessarily persistent } else { //logger.debug("store:" + sig_type_def.getType() + "." + key + "." + caption + " at=" + found_idx); found_keys.put(key, new Integer(found_idx)); last_index = found_idx; } } } if (checkRequiredFields(found_keys) && found_keys.size() > maxSize_) { foundKeys_ = found_keys; sigTypeDef_ = sig_type_def; type_ = sig_type_def.getType(); startIndex_ = last_index; signatureString_ = rawText.substring(startIndex_, endIndex_); maxSize_ = found_keys.size(); found_type = true; } } return found_type; } /** * @return Returns the endIndex. */ public int getEndIndex() { return endIndex_; } /** * @return Returns the signatureObject of the separated signature block. * @throws SignatureException */ public SignatureObject getSignatureObject() throws SignatureException { if (signatureObject_ == null && foundKeys_ != null) { signatureObject_ = new SignatureObject(); try { signatureObject_.setSigType(type_); signatureObject_.initByType(); } catch (SignatureTypesException e) { SignatureException se = new SignatureException(101, "Can ot set signation type:" + type_, e); throw se; } String sig_text = signatureString_; Vector revert_keys = sigTypeDef_.getRevertSortedKeys(); Vector revert_captions = sigTypeDef_.getRevertSortedCaptions(); for (int key_idx = 0; key_idx < revert_keys.size(); key_idx++) { String key = (String) revert_keys.get(key_idx); String caption = (String) revert_captions.get(key_idx); int start_idx = sig_text.lastIndexOf(caption); if (start_idx >= 0) { int sep_idx = start_idx + caption.length(); // logger.debug(sig_text); // logger.debug("caption:" + caption + " start_idx:" + start_idx // + " length:" + // sig_text.length()); String value = sig_text.substring(sep_idx); // logger.debug("key:" + key + " value:" + value); signatureObject_.setSigValueCaption(key, value, caption); sig_text = sig_text.substring(0, start_idx); } } } return signatureObject_; } /** * @return Returns the startIndex. */ public int getStartIndex() { return startIndex_; } /** * @return Returns the type. */ public String getType() { return type_; } // /** // * @param endIndex // * The endIndex to set. // */ // private void setEndIndex(int endIndex) // { // endIndex_ = endIndex; // } // // /** // * @param startIndex // * The startIndex to set. // */ // private void setStartIndex(int startIndex) // { // startIndex_ = startIndex; // } // // /** // * @param type // * The type to set. // */ // private void setType(String type) // { // type_ = type; // } /** * The standard toString method. Used for interal tests only. */ public String toString() { String strg = ""; strg += "Type:" + type_ + "\n"; strg += "Start index:" + startIndex_ + "\n"; strg += "End index:" + endIndex_ + "\n"; strg += signatureString_ + "\n"; strg += sigTypeDef_ + "\n"; try { strg += getSignatureObject().toString(); } catch (SignatureException e) { } return strg; } }