svgimagemanipulator.java

来自「一套JAVA的CA证书签发系统.」· Java 代码 · 共 351 行 · 第 1/2 页

JAVA
351
字号
/************************************************************************* *                                                                       * *  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.FileInputStream;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 javax.imageio.ImageIO;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.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.ra.UserAdminData;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.6 2004/04/16 07:39:00 anatom Exp $ */public class SVGImageManipulator {	/**     * 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 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 PIN1 = Pattern.compile("\\$PIN1", Pattern.CASE_INSENSITIVE);	private static final Pattern PIN2 = Pattern.compile("\\$PIN2", Pattern.CASE_INSENSITIVE);	private static final Pattern PIN3 = Pattern.compile("\\$PIN3", Pattern.CASE_INSENSITIVE);	private static final Pattern PIN4 = Pattern.compile("\\$PIN4", Pattern.CASE_INSENSITIVE);	private static final Pattern PIN5 = Pattern.compile("\\$PIN5", Pattern.CASE_INSENSITIVE);	    private static final Pattern[] PINS = {PIN1, PIN2, PIN3, PIN4, PIN5};        	private static final Pattern PUK1 = Pattern.compile("\\$PUK1", Pattern.CASE_INSENSITIVE);	private static final Pattern PUK2 = Pattern.compile("\\$PUK2", Pattern.CASE_INSENSITIVE);	private static final Pattern PUK3 = Pattern.compile("\\$PUK3", Pattern.CASE_INSENSITIVE);	private static final Pattern PUK4 = Pattern.compile("\\$PUK4", Pattern.CASE_INSENSITIVE);	private static final Pattern PUK5 = Pattern.compile("\\$PUK5", Pattern.CASE_INSENSITIVE);		private static final Pattern[] PUKS = {PUK1, PUK2, PUK3, PUK4, PUK5};			    /**     *  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);    /**     * 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(UserAdminData 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());

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?