📄 svgimagemanipulator.java
字号:
/************************************************************************* * * * EJBCA: The OpenSource Certificate Authority * * * * This software is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public * * License as published by the Free Software Foundation; either * * version 2.1 of the License, or any later version. * * * * See terms of license at gnu.org. * * * *************************************************************************/ package se.anatom.ejbca.hardtoken.hardtokenprofiles;import java.awt.image.BufferedImage;import java.awt.image.RenderedImage;import java.awt.print.Printable;import java.awt.print.PrinterException;import java.io.ByteArrayOutputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.Reader;import java.text.DateFormat;import java.util.ArrayList;import java.util.Collection;import java.util.Date;import java.util.Iterator;import java.util.regex.Pattern;import org.apache.batik.dom.GenericText;import org.apache.batik.dom.svg.SAXSVGDocumentFactory;import org.apache.batik.dom.svg.SVGDOMImplementation;import org.apache.batik.dom.svg.SVGOMDocument;import org.apache.batik.dom.svg.SVGOMImageElement;import org.apache.batik.dom.svg.SVGOMTSpanElement;import org.apache.batik.svggen.ImageHandlerBase64Encoder;import org.apache.batik.svggen.SVGGeneratorContext;import org.apache.batik.svggen.SimpleImageHandler;import org.apache.batik.transcoder.TranscoderInput;import org.apache.batik.transcoder.TranscoderOutput;import org.apache.batik.transcoder.print.PrintTranscoder;import org.apache.batik.util.XMLResourceDescriptor;import org.apache.commons.lang.StringUtils;import org.apache.log4j.Logger;import org.w3c.dom.Document;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.w3c.dom.svg.SVGRectElement;import org.w3c.dom.svg.SVGTextElement;import se.anatom.ejbca.common.UserDataVO;import se.anatom.ejbca.ra.raadmin.DNFieldExtractor;/** * This is a help class used to manipulate SVG images. * It replaces all occurrenses of specified variables in the images * with the corresponding userdata. * * @version $Id: SVGImageManipulator.java,v 1.12.2.3 2005/10/28 20:10:34 primelars Exp $ */public class SVGImageManipulator { /** * For logging */ private static final Logger log = Logger.getLogger(SVGImageManipulator.class); /** * Availabe vairables used to replace text in a printlayout * Variable text are case-insensitive. */ private static final Pattern USERNAME = Pattern.compile("\\$USERNAME", Pattern.CASE_INSENSITIVE); private static final Pattern UID = Pattern.compile("\\$UID", Pattern.CASE_INSENSITIVE); private static final Pattern CN = Pattern.compile("\\$CN", Pattern.CASE_INSENSITIVE); private static final Pattern SN = Pattern.compile("\\$SN", Pattern.CASE_INSENSITIVE); private static final Pattern GIVENNAME= Pattern.compile("\\$GIVENNAME", Pattern.CASE_INSENSITIVE); private static final Pattern INITIALS = Pattern.compile("\\$INITIALS", Pattern.CASE_INSENSITIVE); private static final Pattern SURNAME = Pattern.compile("\\$SURNAME", Pattern.CASE_INSENSITIVE); private static final Pattern O = Pattern.compile("\\$O", Pattern.CASE_INSENSITIVE); private static final Pattern OU = Pattern.compile("\\$OU", Pattern.CASE_INSENSITIVE); private static final Pattern C = Pattern.compile("\\$C", Pattern.CASE_INSENSITIVE); private static final Pattern LOCATION = Pattern.compile("\\$LOCATION", Pattern.CASE_INSENSITIVE); private static final Pattern TITLE = Pattern.compile("\\$TITLE", Pattern.CASE_INSENSITIVE); /** * Indicates the start date of the tokens validity. */ private static final Pattern STARTDATE = Pattern.compile("\\$STARTDATE", Pattern.CASE_INSENSITIVE); /** * Indicates the end date of the tokens validity. */ private static final Pattern ENDDATE = Pattern.compile("\\$ENDDATE", Pattern.CASE_INSENSITIVE); private static final Pattern HARDTOKENSN = Pattern.compile("\\$HARDTOKENSN", Pattern.CASE_INSENSITIVE); private static final Pattern HARDTOKENSNWITHOUTPREFIX = Pattern.compile("\\$HARDTOKENSNWITHOUTPREFIX", Pattern.CASE_INSENSITIVE); /** * Constants used for pin and puk codes. */ private static final Pattern[] PINS; private static final Pattern[] PUKS; /** * Constants reserved for future use. */// private static final Pattern CUSTOMTEXTROW1 = Pattern.compile("\\$CUSTOMTEXTROW1", Pattern.CASE_INSENSITIVE); // private static final Pattern CUSTOMTEXTROW2 = Pattern.compile("\\$CUSTOMTEXTROW2", Pattern.CASE_INSENSITIVE);// private static final Pattern CUSTOMTEXTROW3 = Pattern.compile("\\$CUSTOMTEXTROW3", Pattern.CASE_INSENSITIVE);// private static final Pattern CUSTOMTEXTROW4 = Pattern.compile("\\$CUSTOMTEXTROW4", Pattern.CASE_INSENSITIVE);// private static final Pattern CUSTOMTEXTROW5 = Pattern.compile("\\$CUSTOMTEXTROW5", Pattern.CASE_INSENSITIVE);// private static final Pattern COPYOFSN = Pattern.compile("\\$COPYOFSN", Pattern.CASE_INSENSITIVE);// private static final Pattern COPYOFSNWITHOUTPREFIX = Pattern.compile("\\$COPYOFSNWITHOUTPREFIX", Pattern.CASE_INSENSITIVE); static { PINS = new Pattern[0x50]; PUKS = new Pattern[PINS.length]; for (int i=0; i<PINS.length; i++) { final int pinNr = i+1; PINS[i] = Pattern.compile("\\$PIN"+pinNr, Pattern.CASE_INSENSITIVE); PUKS[i] = Pattern.compile("\\$PUK"+pinNr, Pattern.CASE_INSENSITIVE); } } /** * Constructor for the SVGImageManipulator object * * @param svgdata the xlm data to parse * @param validity the validity of the card i days. * @param hardtokensnprefix the prefix of all hard tokens generated with this profile. * @param imagex x-position for image, reserved for future use * @param imagey y-position for image, reserved for future use * @param imageheight heigth of image, reserved for future use * @param imagewidth width of image, reserved for future use * @param unit units used, reserved for future use * @throws IOException */ public SVGImageManipulator(Reader svgdata, int validity, String hardtokensnprefix) throws IOException { this.validityms = ( ((long)validity) * 1000 * 3600 * 24); // Validity i ms this.hardtokensnprefix = hardtokensnprefix; String parser = XMLResourceDescriptor.getXMLParserClassName(); SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser); String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI; Document doc = f.createDocument(svgNS, svgdata); svgdoc = ((SVGOMDocument) doc); } /** * Returns the message with userspecific data replaced. * * * @return A processed notification message. * */ public Printable print(UserDataVO userdata, String[] pincodes, String[] pukcodes, String hardtokensn, String copyoftokensn) throws IOException, PrinterException { // Initialize DNFieldExtractor dnfields = new DNFieldExtractor(userdata.getDN(), DNFieldExtractor.TYPE_SUBJECTDN); // DNFieldExtractor subaltnamefields = new DNFieldExtractor(dn,DNFieldExtractor.TYPE_SUBJECTALTNAME); Date currenttime = new Date(); String startdate = DateFormat.getDateInstance(DateFormat.SHORT).format(currenttime); String enddate = DateFormat.getDateInstance(DateFormat.SHORT).format(new Date(currenttime.getTime() + (this.validityms))); String hardtokensnwithoutprefix = hardtokensn.substring(this.hardtokensnprefix.length()); String copyoftokensnwithoutprefix = copyoftokensn.substring(this.hardtokensnprefix.length()); // Clone document Node originaldokument = svgdoc.cloneNode(true); // Get Text rows process( "text", userdata, dnfields, pincodes, pukcodes, hardtokensn, hardtokensnwithoutprefix, copyoftokensn, copyoftokensnwithoutprefix, startdate, enddate); process( "svg:text", userdata, dnfields, pincodes, pukcodes,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -