插件下载地址:https://github.com/zry963/plugin_obfus
该插件的主要功能为模糊字段值,如:李四---李某, 18081198871--180811xxxxx等等
Obfus.java 代码
/********************************************************************** ** ** ** This code belongs to the KETTLE project. ** ** ** ** Kettle, from version 2.2 on, is released into the public domain ** ** under the Lesser GNU Public License (LGPL). ** ** ** ** For more details, please read the document LICENSE.txt, included ** ** in this project ** ** ** ** http://www.kettle.be ** ** [email protected] ** ** ** **********************************************************************/ package com.aofei.kettleplugin.obfus; import org.pentaho.di.core.Const; import org.pentaho.di.core.exception.KettleException; import org.pentaho.di.core.row.RowDataUtil; import org.pentaho.di.core.row.RowMetaInterface; import org.pentaho.di.trans.Trans; import org.pentaho.di.trans.TransMeta; import org.pentaho.di.trans.step.BaseStep; import org.pentaho.di.trans.step.StepDataInterface; import org.pentaho.di.trans.step.StepInterface; import org.pentaho.di.trans.step.StepMeta; import org.pentaho.di.trans.step.StepMetaInterface; /** * * @author * @since */ public class Obfus extends BaseStep implements StepInterface { private ObfusMeta meta; private ObfusData data; public Obfus(StepMeta stepMeta, StepDataInterface stepDataInterface, int copyNr, TransMeta transMeta, Trans trans) { super(stepMeta, stepDataInterface, copyNr, transMeta, trans); } public boolean init(StepMetaInterface smi, StepDataInterface sdi) { meta=(ObfusMeta)smi; data=(ObfusData)sdi; super.init(smi, sdi); return true; } // // Run is were the action happens! public void run() { try { logBasic(Messages.getString("Obfus.Log.StartingToRun")); //$NON-NLS-1$ while (processRow(meta, data) && !isStopped()) ; } catch (Exception e) { logError(Messages.getString("Obfus.Log.UnexpectedError") + " : " + e.toString()); //$NON-NLS-1$ //$NON-NLS-2$ logError(Const.getStackTracker(e)); setErrors(1); stopAll(); } finally { dispose(meta, data); logSummary(); markStop(); } } public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException { meta=(ObfusMeta)smi; data=(ObfusData)sdi; //从上个 步骤获取一行 Object[] r=getRow(); //查找要混淆数据的字段位置 int fieldIndex = this.getInputRowMeta().indexOfValue(meta.getFieldName()); //前面步骤的数据都读完,则返回 if (r==null) { setOutputDone(); return false; } //对于独到的第一行数据,构建输出行的元数据 if (first) { first=false; data.outputRowMeta = getInputRowMeta().clone(); meta.getFields(data.outputRowMeta, getStepname(), null, null, this); } //定义输出数据行 Object[] outputRowData = null; //混淆数据 String value = getNewValue(r[fieldIndex].toString()); //构造输出行数据 , 方法1 outputRowData = RowDataUtil.addValueData(r, getInputRowMeta().size(), value); //构造输出行数据 方法 2 //outputRowData = r; //outputRowData[data.outputRowMeta.size()-1]=value; putRow(data.outputRowMeta, outputRowData); return true; } private String getNewValue( String value) { String result; if(value.length()<meta.getNumberN()) result= value; else { int leng = value.length(); result = value.substring(0,meta.getNumberN()); while(result.length()<leng) { result+=meta.getReplaceChars().charAt(0); } } return result; } }
/********************************************************************** ** ** ** This code belongs to the KETTLE project. ** ** ** ** Kettle, from version 2.2 on, is released into the public domain ** ** under the Lesser GNU Public License (LGPL). ** ** ** ** For more details, please read the document LICENSE.txt, included ** ** in this project ** ** ** ** http://www.kettle.be ** ** [email protected] ** ** ** **********************************************************************/ package com.aofei.kettleplugin.obfus; import org.pentaho.di.core.database.Database; import org.pentaho.di.core.row.RowMetaInterface; import org.pentaho.di.trans.step.BaseStepData; import org.pentaho.di.trans.step.StepDataInterface; /** * @author * @since * */ public class ObfusData extends BaseStepData implements StepDataInterface { public RowMetaInterface outputRowMeta; /** * */ public ObfusData() { super(); } }
/********************************************************************** ** ** ** This code belongs to the KETTLE project. ** ** ** ** Kettle, from version 2.2 on, is released into the public domain ** ** under the Lesser GNU Public License (LGPL). ** ** ** ** For more details, please read the document LICENSE.txt, included ** ** in this project ** ** ** ** http://www.kettle.be ** ** [email protected] ** ** ** **********************************************************************/ package com.aofei.kettleplugin.obfus; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.eclipse.swt.widgets.Shell; import org.pentaho.di.core.CheckResult; import org.pentaho.di.core.CheckResultInterface; import org.pentaho.di.core.Const; import org.pentaho.di.core.exception.KettleException; import org.pentaho.di.core.exception.KettleStepException; import org.pentaho.di.core.exception.KettleXMLException; import org.pentaho.di.core.row.RowMeta; import org.pentaho.di.core.row.RowMetaInterface; import org.pentaho.di.core.row.ValueMeta; import org.pentaho.di.core.row.ValueMetaInterface; import org.pentaho.di.core.variables.VariableSpace; import org.pentaho.di.core.xml.XMLHandler; import org.pentaho.di.repository.ObjectId; import org.pentaho.di.repository.Repository; import org.pentaho.di.trans.Trans; import org.pentaho.di.trans.TransMeta; import org.pentaho.di.trans.step.BaseStepMeta; import org.pentaho.di.trans.step.StepDataInterface; import org.pentaho.di.trans.step.StepDialogInterface; import org.pentaho.di.trans.step.StepInterface; import org.pentaho.di.trans.step.StepMeta; import org.pentaho.di.trans.step.StepMetaInterface; import org.w3c.dom.Node; public class ObfusMeta extends BaseStepMeta implements StepMetaInterface { /** the field need to be obfuscated */ private String fieldName; /** new obfuscated field name */ private String newFieldName; /**obfuscation method */ private boolean isFixPreN; /**Number N */ private int numberN; /**Replace chars*/ private String replaceChars; public ObfusMeta() { super(); // allocate BaseStepMeta } public void loadXML(Node stepnode, List databases, Map counters) throws KettleXMLException { readData(stepnode, databases); } public Object clone() { ObfusMeta retval = (ObfusMeta) super.clone(); return retval; } private void readData(Node stepnode, List databases) throws KettleXMLException { try { fieldName = XMLHandler.getTagValue(stepnode, "FIELD_NAME"); //$NON-NLS-1$ newFieldName = XMLHandler.getTagValue(stepnode, "NEW_FIELD_NAME"); //$NON-NLS-1$ isFixPreN = "Y".equalsIgnoreCase(XMLHandler.getTagValue(stepnode, "FIX_PRE_N"))?new Boolean("true"):new Boolean("false"); //$NON-NLS-1$ numberN = Const.toInt(XMLHandler.getTagValue(stepnode, "N"), 0); //$NON-NLS-1$ replaceChars = XMLHandler.getTagValue(stepnode, "REPALCE_CHARS"); //$NON-NLS-1$ } catch (Exception e) { throw new KettleXMLException( Messages .getString("ObfusMeta.Exception.UnableToReadStepInfoFromXML"), e); //$NON-NLS-1$ } } public void setDefault() { numberN = 1; } public String getXML() { StringBuffer retval = new StringBuffer(); retval.append(" " + XMLHandler.addTagValue("FIELD_NAME", fieldName)); //$NON-NLS-1$ //$NON-NLS-2$ retval.append(" " + XMLHandler.addTagValue("NEW_FIELD_NAME", newFieldName)); //$NON-NLS-1$ //$NON-NLS-2$ retval.append(" " + XMLHandler.addTagValue("FIX_PRE_N", isFixPreN?"Y":"N")); //$NON-NLS-1$ //$NON-NLS-2$ retval.append(" " + XMLHandler.addTagValue("N", numberN)); //$NON-NLS-1$ //$NON-NLS-2$ retval.append(" " + XMLHandler.addTagValue("REPALCE_CHARS", replaceChars)); //$NON-NLS-1$ //$NON-NLS-2$ return retval.toString(); } public void readRep(Repository rep, ObjectId id_step, List databases, Map counters) throws KettleException { try { fieldName =rep.getStepAttributeString(id_step, "FIELD_NAME"); //$NON-NLS-1$ newFieldName =rep.getStepAttributeString(id_step, "NEW_FIELD_NAME"); //$NON-NLS-1$ isFixPreN = "Y".equals(rep.getStepAttributeString(id_step, "FIX_PRE_N"))?new Boolean("true"):new Boolean("false"); //$NON-NLS-1$ numberN = (int) rep.getStepAttributeInteger(id_step, "N"); //$NON-NLS-1$ replaceChars =rep.getStepAttributeString(id_step, "REPALCE_CHARS"); //$NON-NLS-1$ } catch (Exception e) { throw new KettleException( Messages .getString("ObfusMeta.Exception.UnexpectedErrorInReadingStepInfo"), e); //$NON-NLS-1$ } } public void saveRep(Repository rep, ObjectId id_transformation, ObjectId id_step) throws KettleException { try { rep.saveStepAttribute(id_transformation, id_step, "FIELD_NAME", fieldName); //$NON-NLS-1$ rep.saveStepAttribute(id_transformation, id_step, "NEW_FIELD_NAME", newFieldName); //$NON-NLS-1$ rep.saveStepAttribute(id_transformation, id_step, "FIX_PRE_N", isFixPreN?"Y":"N"); //$NON-NLS-1$ rep.saveStepAttribute(id_transformation, id_step, "commit", numberN); //$NON-NLS-1$ rep.saveStepAttribute(id_transformation, id_step, "REPALCE_CHARS", replaceChars); //$NON-NLS-1$ } catch (Exception e) { throw new KettleException( Messages .getString("ObfusMeta.Exception.UnableToSaveStepInfo") + id_step, e); //$NON-NLS-1$ } } public void getFields(RowMetaInterface row, String origin, RowMetaInterface[] info, StepMeta nextStep, VariableSpace space) throws KettleStepException { int size = row.size(); ValueMetaInterface v = new ValueMeta(space.environmentSubstitute(newFieldName), ValueMeta.TYPE_STRING); v.setOrigin(origin); row.addValueMeta(v); } public void check(ArrayList remarks, StepMeta stepinfo, RowMeta prev, String input[], String output[], RowMeta info) { CheckResult cr; String error_message = ""; //$NON-NLS-1$ } public void analyseImpact(ArrayList impact, TransMeta transMeta, StepMeta stepMeta, RowMeta prev, String[] input, String[] output, RowMeta info) throws KettleStepException { } public StepDialogInterface getDialog(Shell shell, StepMetaInterface info, TransMeta transMeta, String name) { return new ObfusDialog(shell, info, transMeta, name); } public StepInterface getStep(StepMeta stepMeta, StepDataInterface stepDataInterface, int cnr, TransMeta tr, Trans trans) { return new Obfus(stepMeta, stepDataInterface, cnr, tr, trans); } public StepDataInterface getStepData() { return new ObfusData(); } public void check(List<CheckResultInterface> arg0, TransMeta arg1, StepMeta arg2, RowMetaInterface arg3, String[] arg4, String[] arg5, RowMetaInterface arg6) { // TODO Auto-generated method stub } public int getNumberN() { return numberN; } public void setNumberN(int n) { numberN = n; } public String getReplaceChars() { return replaceChars; } public void setReplaceChars(String replaceChars) { this.replaceChars = replaceChars; } public boolean isFixPreN() { return isFixPreN; } public void setFixPreN(boolean fixPreN) { this.isFixPreN = fixPreN; } public String getFieldName() { return fieldName; } public void setFieldName(String fieldName) { this.fieldName = fieldName; } public String getNewFieldName() { return newFieldName; } public void setNewFieldName(String newFieldName) { this.newFieldName = newFieldName; } }
/********************************************************************** ** ** ** This code belongs to the KETTLE project. ** ** ** ** Kettle, from version 2.2 on, is released into the public domain ** ** under the Lesser GNU Public License (LGPL). ** ** ** ** For more details, please read the document LICENSE.txt, included ** ** in this project ** ** ** ** http://www.kettle.be ** ** [email protected] ** ** ** **********************************************************************/ /* * Created on 18-mei-2003 * */ package com.aofei.kettleplugin.obfus; import java.util.ArrayList; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.CCombo; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.ShellAdapter; import org.eclipse.swt.events.ShellEvent; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import org.pentaho.di.core.Const; import org.pentaho.di.core.row.RowMeta; import org.pentaho.di.core.row.RowMetaInterface; import org.pentaho.di.i18n.BaseMessages; import org.pentaho.di.trans.TransMeta; import org.pentaho.di.trans.step.BaseStepMeta; import org.pentaho.di.trans.step.StepDialogInterface; import org.pentaho.di.ui.trans.step.BaseStepDialog; public class ObfusDialog extends BaseStepDialog implements StepDialogInterface { private CCombo wConnection; private Label wlFieldName; private Text wFieldName; private FormData fdlFieldName, fdFieldName; private Label wlNewFieldName; private Text wNewFieldName; private FormData fdlNewFieldName, fdNewFieldName; private Label wlFixPreN; private Button wFixPreN; private FormData fdlFixPreN, fdFixPreN; private Label wlNumberN; private Text wNumberN; private FormData fdlN, fdN; private Label wlReplaceChars; private Text wReplaceChars; private FormData fdlReplaceChars, fdReplaceChars; private ObfusMeta meta; public ObfusDialog(Shell parent, Object in, TransMeta tr, String sname) { super(parent, (BaseStepMeta)in, tr, sname); meta=(ObfusMeta)in; } public String open() { Shell parent = getParent(); Display display = parent.getDisplay(); shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MAX | SWT.MIN); props.setLook(shell); ModifyListener lsMod = new ModifyListener() { public void modifyText(ModifyEvent e) { meta.setChanged(); } }; changed = meta.hasChanged(); FormLayout formLayout = new FormLayout (); formLayout.marginWidth = Const.FORM_MARGIN; formLayout.marginHeight = Const.FORM_MARGIN; shell.setLayout(formLayout); shell.setText(Messages.getString("ObfusDialog.Shell.Title")); //$NON-NLS-1$ int middle = props.getMiddlePct(); int margin = Const.MARGIN; // Stepname line wlStepname=new Label(shell, SWT.RIGHT); wlStepname.setText(Messages.getString("ObfusDialog.Stepname.Label")); //$NON-NLS-1$ props.setLook(wlStepname); fdlStepname=new FormData(); fdlStepname.left = new FormAttachment(0, 0); fdlStepname.top = new FormAttachment(0, margin); fdlStepname.right= new FormAttachment(middle, -margin); wlStepname.setLayoutData(fdlStepname); wStepname=new Text(shell, SWT.SINGLE | SWT.LEFT | SWT.BORDER); wStepname.setText(stepname); props.setLook(wStepname); wStepname.addModifyListener(lsMod); fdStepname=new FormData(); fdStepname.left = new FormAttachment(middle, 0); fdStepname.top = new FormAttachment(0, margin); fdStepname.right= new FormAttachment(100, 0); wStepname.setLayoutData(fdStepname); // FieldName wlFieldName = new Label(shell, SWT.RIGHT); wlFieldName.setText(Messages.getString("ObfusDialog.fieldName.Label")); //$NON-NLS-1$ props.setLook(wlFieldName); fdlFieldName = new FormData(); fdlFieldName.left = new FormAttachment(0, 0); fdlFieldName.top = new FormAttachment(wStepname, margin); fdlFieldName.right = new FormAttachment(middle, -margin); wlFieldName.setLayoutData(fdlFieldName); wFieldName = new Text(shell, SWT.SINGLE | SWT.LEFT | SWT.BORDER); props.setLook(wFieldName); fdFieldName = new FormData(); fdFieldName.left = new FormAttachment(middle, 0); fdFieldName.top = new FormAttachment(wStepname, margin); fdFieldName.right = new FormAttachment(100, 0); wFieldName.setLayoutData(fdFieldName); // NewFieldName wlNewFieldName = new Label(shell, SWT.RIGHT); wlNewFieldName.setText(Messages.getString("ObfusDialog.newFieldName.Label")); //$NON-NLS-1$ props.setLook(wlNewFieldName); fdlNewFieldName = new FormData(); fdlNewFieldName.left = new FormAttachment(0, 0); fdlNewFieldName.top = new FormAttachment(wFieldName, margin); fdlNewFieldName.right = new FormAttachment(middle, -margin); wlNewFieldName.setLayoutData(fdlNewFieldName); wNewFieldName = new Text(shell, SWT.SINGLE | SWT.LEFT | SWT.BORDER); props.setLook(wNewFieldName); fdNewFieldName = new FormData(); fdNewFieldName.left = new FormAttachment(middle, 0); fdNewFieldName.top = new FormAttachment(wFieldName, margin); fdNewFieldName.right = new FormAttachment(100, 0); wNewFieldName.setLayoutData(fdNewFieldName); // Fix pre N check box wlFixPreN = new Label(shell, SWT.RIGHT); wlFixPreN.setText(Messages.getString("ObfusDialog.FixPreN.label")); //$NON-NLS-1$ props.setLook(wlFixPreN); fdlFixPreN = new FormData(); fdlFixPreN.left = new FormAttachment(0, 0); fdlFixPreN.top = new FormAttachment(wNewFieldName, margin); fdlFixPreN.right = new FormAttachment(middle, -margin); wlFixPreN.setLayoutData(fdlFixPreN); wFixPreN = new Button(shell, SWT.CHECK); props.setLook(wFixPreN); fdFixPreN= new FormData(); fdFixPreN.left = new FormAttachment(middle, 0); fdFixPreN.top = new FormAttachment(wNewFieldName, margin); fdFixPreN.right = new FormAttachment(100, 0); wFixPreN.setLayoutData(fdFixPreN); wFixPreN.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent arg0) { meta.setChanged(); wlNumberN.setEnabled(wFixPreN.getSelection()); wNumberN.setEnabled(wFixPreN.getSelection()); wlReplaceChars.setEnabled(wFixPreN.getSelection()); wReplaceChars.setEnabled(wFixPreN.getSelection()); } } ); //N wlNumberN = new Label(shell, SWT.RIGHT); wlNumberN.setText(Messages.getString("ObfusDialog.N.Label")); //$NON-NLS-1$ props.setLook(wlNumberN); fdlN = new FormData(); fdlN.left = new FormAttachment(0, 0); fdlN.top = new FormAttachment(wFixPreN, margin); fdlN.right = new FormAttachment(middle, -margin); wlNumberN.setLayoutData(fdlN); wNumberN = new Text(shell, SWT.SINGLE | SWT.LEFT | SWT.BORDER); props.setLook(wNumberN); fdN = new FormData(); fdN.left = new FormAttachment(middle, 0); fdN.top = new FormAttachment(wFixPreN, margin); fdN.right = new FormAttachment(100, 0); wNumberN.setLayoutData(fdN); // ReplaceChars wlReplaceChars = new Label(shell, SWT.RIGHT); wlReplaceChars.setText(Messages.getString("ObfusDialog.replaceChars.Label")); //$NON-NLS-1$ props.setLook(wlReplaceChars); fdlReplaceChars = new FormData(); fdlReplaceChars.left = new FormAttachment(0, 0); fdlReplaceChars.top = new FormAttachment(wNumberN, margin); fdlReplaceChars.right = new FormAttachment(middle, -margin); wlReplaceChars.setLayoutData(fdlReplaceChars); wReplaceChars = new Text(shell, SWT.SINGLE | SWT.LEFT | SWT.BORDER); props.setLook(wReplaceChars); fdReplaceChars = new FormData(); fdReplaceChars.left = new FormAttachment(middle, 0); fdReplaceChars.top = new FormAttachment(wNumberN, margin); fdReplaceChars.right = new FormAttachment(70, 0); wReplaceChars.setLayoutData(fdReplaceChars); // THE BUTTONS wOK=new Button(shell, SWT.PUSH); wOK.setText(Messages.getString("System.Button.OK")); //$NON-NLS-1$ wCancel=new Button(shell, SWT.PUSH); wCancel.setText(Messages.getString("System.Button.Cancel")); //$NON-NLS-1$ setButtonPositions(new Button[] { wOK, wCancel }, margin, null); // Add listeners lsOK = new Listener() { public void handleEvent(Event e) { ok(); } }; lsCancel = new Listener() { public void handleEvent(Event e) { cancel(); } }; wOK.addListener (SWT.Selection, lsOK ); wCancel.addListener(SWT.Selection, lsCancel); lsDef=new SelectionAdapter() { public void widgetDefaultSelected(SelectionEvent e) { ok(); } }; wStepname.addSelectionListener( lsDef ); // Detect X or ALT-F4 or something that kills this window... shell.addShellListener( new ShellAdapter() { public void shellClosed(ShellEvent e) { cancel(); } } ); // Set the shell size, based upon previous time... setSize(); getData(); meta.setChanged(changed); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } return stepname; } public void setFlags() { } /** * Copy information from the meta-data input to the dialog fields. */ public void getData() { log.logDebug(toString(), Messages.getString("ObfusDialog.Log.GettingKeyInfo")); //$NON-NLS-1$ wStepname.selectAll(); if(!Const.isEmpty(meta.getFieldName())) wFieldName.setText(meta.getFieldName()); if(!Const.isEmpty(meta.getNewFieldName())) wNewFieldName.setText(meta.getNewFieldName()); if(!Const.isEmpty(meta.getReplaceChars())) wReplaceChars.setText(meta.getReplaceChars()); if(meta.getNumberN()!=0) wNumberN.setText(new Integer(meta.getNumberN()).toString()); wFixPreN.setSelection(meta.isFixPreN()); setFlags(); } private void cancel() { stepname=null; meta.setChanged(changed); dispose(); } private void getInfo(ObfusMeta meta) { meta.setFieldName(wFieldName.getText()); meta.setNewFieldName(wNewFieldName.getText()); meta.setFixPreN(wFixPreN.isEnabled()); meta.setReplaceChars(wReplaceChars.getText()); try{ meta.setNumberN(new Integer(wNumberN.getText()).intValue()); }catch (Exception ex) { log.logError(toString(),wNumberN.getText()+ " cant conver to int"); //$NON-NLS-1$ MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR ); mb.setMessage("cant conver to int"); //$NON-NLS-1$ mb.setText("cant conver to int"); //$NON-NLS-1$ mb.open(); } stepname = wStepname.getText(); // return value } private void ok() { // Get the information for the dialog into the input structure. getInfo(meta); if (wFixPreN.isEnabled()) { if((Const.isEmpty(wNumberN.getText()) || Const.isEmpty(wReplaceChars.getText()))) { MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR ); mb.setMessage("not be null"); //$NON-NLS-1$ mb.setText("not be null"); //$NON-NLS-1$ mb.open(); return; } } dispose(); } public String toString() { return this.getClass().getName(); } }