Filter sensitive word DFA Java implementation

Source: Internet
Author: User

Tag: ber false tostring TNO trim private list slf4j byte

 PackageCom.member.schedule;Importjava.io.UnsupportedEncodingException;ImportJava.nio.ByteBuffer;ImportJava.nio.charset.Charset;Importjava.util.List;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;/** * @authorXu Liangyong * @Date 2011-10-13 a.m. 9:23:43*/ Public classDFA {Private Static FinalLogger Logger = Loggerfactory.getlogger (DFA.class); /*** Root node*/    PrivateTreeNode RootNode =NewTreeNode (); /*** Keyword Cache*/    PrivateBytebuffer Keywordbuffer = bytebuffer.allocate (1024); /*** Keyword Code*/    PrivateString charset = "GBK"; /*** Create DFA * *@paramKeywordList *@throwsunsupportedencodingexception*/     Public voidCreatekeywordtree (list<string> keywordlist)throwsunsupportedencodingexception { for(String keyword:keywordlist) {if(keyword = =NULL)                Continue; Keyword=Keyword.trim (); byte[] bytes =keyword.getbytes (CharSet); TreeNode Tempnode=RootNode; //loop per byte             for(inti = 0; i < bytes.length; i++) {                intindex = bytes[i] & 0xff;//convert characters into numbersTreeNode node =Tempnode.getsubnode (index); if(node = =NULL) {//not initializednode =NewTreeNode ();                Tempnode.setsubnode (Index, node); } Tempnode=node; if(i = = Bytes.length-1) {tempnode.setkeywordend (true);//end of keyword, set end flagLogger.debug ("dfa:{}", keyword); }            } //End for}//End for    }    /*** Search Keywords*/     PublicString SearchKeyword (String text)throwsunsupportedencodingexception {returnSearchKeyword (Text.getbytes (charset)); }    /*** Search Keywords*/     PublicString SearchKeyword (byte[] bytes) {StringBuilder words=NewStringBuilder (); if(bytes = =NULL|| Bytes.length = = 0) {            returnwords.tostring (); } TreeNode Tempnode=RootNode; intRollback = 0;//Roll Back number        intPosition = 0;//location of the current comparison         while(Position <bytes.length) {intindex = bytes[position] & 0xFF; Keywordbuffer.put (Bytes[position]); //Write keyword CacheTempnode =Tempnode.getsubnode (index); //match end of current position            if(Tempnode = =NULL) {Position= Position-rollback;//fallback and test the next byteRollback = 0; Tempnode= RootNode;//state Machine ResetKeywordbuffer.clear ();//Clear}Else if(Tempnode.iskeywordend ()) {//is the end point record keywordKeywordbuffer.flip (); String keyword=charset.forname (Charset). Decode (Keywordbuffer). toString (); Logger.debug ("Find keyword:{}", keyword);                Keywordbuffer.limit (Keywordbuffer.capacity ()); if(words.length () = = 0) words.append (keyword); ElseWords.append (":"). append (keyword); Rollback= 1;//encounter end point rollback to 1}Else{rollback++;//non-end-point fallback number plus 1} position++; }        returnwords.tostring (); }     Public voidSetcharset (String charset) { This. CharSet =CharSet; }}
 PackageCom.member.schedule;Importjava.util.ArrayList;Importjava.util.List;/*** Tree nodes Each node contains an array of length 256 * *@authorXu Liangyong * @Date 2011-10-12 a.m. 3:11:24*/ Public classTreeNode {Private Static Final intNode_len = 256; /*** true keywords end; false continue*/    Private BooleanEnd =false; Privatelist<treenode> subnodes =NewArraylist<treenode>(Node_len);  PublicTreeNode () { for(inti = 0; i < Node_len; i++) {Subnodes.add (I,NULL); }    }    /*** Add a node tree to a specified location * *@paramIndex *@paramnode*/     Public voidSetsubnode (intindex, TreeNode node)    {Subnodes.set (Index, node); }     PublicTreeNode Getsubnode (intindex) {        returnSubnodes.get (index); }     Public BooleanIskeywordend () {returnend; }     Public voidSetkeywordend (Booleanend) {         This. end =end; }}

link:http://www.iteye.com/topic/1116520

Filter sensitive word DFA Java implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.