1 package prefuse.data.search; 2 3 import java.util.Iterator ; 4 import java.util.LinkedHashMap ; 5 import java.util.logging.Logger ; 6 import java.util.regex.Pattern ; 7 8 import prefuse.data.Tuple; 9 import prefuse.data.tuple.DefaultTupleSet; 10 import prefuse.data.tuple.TupleSet; 11 import prefuse.util.StringLib; 12 13 25 public class RegexSearchTupleSet extends SearchTupleSet { 26 27 private String m_query = ""; 28 private boolean m_caseSensitive; 29 private LinkedHashMap m_source = new LinkedHashMap (); 30 31 34 public RegexSearchTupleSet() { 35 this(false); 36 } 37 38 43 public RegexSearchTupleSet(boolean caseSensitive) { 44 m_caseSensitive = caseSensitive; 45 } 46 47 50 public String getQuery() { 51 return m_query; 52 } 53 54 57 public void search(String query) { 58 if ( query == null ) 59 query = ""; 60 if ( !m_caseSensitive ) 61 query = query.toLowerCase(); 62 if ( query.equals(m_query) ) 63 return; 64 65 Pattern pattern = null; 66 try { 67 pattern = Pattern.compile(query); 68 } catch ( Exception e ) { 69 Logger logger = Logger.getLogger(this.getClass().getName()); 70 logger.warning("Pattern compile failed." 71 + "\n" + StringLib.getStackTrace(e)); 72 return; 73 } 74 75 Tuple[] rem = clearInternal(); 76 m_query = query; 77 Iterator fields = m_source.keySet().iterator(); 78 while ( fields.hasNext() ) { 79 String field = (String )fields.next(); 80 TupleSet ts = (TupleSet)m_source.get(field); 81 82 Iterator tuples = ts.tuples(); 83 while ( tuples.hasNext() ) { 84 Tuple t = (Tuple)tuples.next(); 85 String text = t.getString(field); 86 if ( !m_caseSensitive ) 87 text = text.toLowerCase(); 88 89 if ( pattern.matcher(text).matches() ) 90 addInternal(t); 91 } 92 } 93 Tuple[] add = getTupleCount() > 0 ? toArray() : null; 94 fireTupleEvent(add, rem); 95 } 96 97 100 public void index(Tuple t, String field) { 101 TupleSet ts = (TupleSet)m_source.get(field); 102 if ( ts == null ) { 103 ts = new DefaultTupleSet(); 104 m_source.put(field, ts); 105 } 106 ts.addTuple(t); 107 } 108 109 112 public void unindex(Tuple t, String field) { 113 TupleSet ts = (TupleSet)m_source.get(field); 114 if ( ts != null ) { 115 ts.removeTuple(t); 116 } 117 } 118 119 123 public boolean isUnindexSupported() { 124 return true; 125 } 126 127 131 public void clear() { 132 m_source.clear(); 133 super.clear(); 134 } 135 136 } | Popular Tags |