Skip to content
Permalink
Browse files
core: Split out util.MojiBakeMapper and Core::MojiBake module for reuse
  • Loading branch information
dekellum committed Jun 22, 2011
1 parent 288277e commit ee85ac1684639745e0a767b97a236eead8c4db31
@@ -17,22 +17,16 @@
require 'iudex-core'
require 'java'

module Iudex::Core::Filters
import 'iudex.core.filters.MojiBakeFilter'

# Re-open iudex.core.filters.MojiBakeFilter to add config file
# based initialization.
class MojiBakeFilter
module Iudex::Core

module MojiBake
DEFAULT_CONFIG = File.join( File.dirname( __FILE__ ),
'..', '..', 'config', 'mojibake' )

# Alt constructor taking a configuration file in `mojibake -t`
# format.
def initialize( key, config_file = DEFAULT_CONFIG )
def self.load_config( file = DEFAULT_CONFIG )
regex = nil
mojis = []
File.open( config_file ) do |fin|
File.open( file ) do |fin|
fin.each do |line|
case line
when %r{^/([^/]+)/$}
@@ -47,15 +41,33 @@ def initialize( key, config_file = DEFAULT_CONFIG )
mojis.each do | moji, rpl |
mh.put( jstring( moji ), jstring( rpl ) )
end
super( key, regex, mh )
[ regex, mh ]
end

private

def jstring( cps )
def self.jstring( cps )
cs = cps.map { |cp| cp.hex }.to_java( :char )
Java::java.lang.String.new( cs )
end

end

module Filters
import 'iudex.core.filters.MojiBakeFilter'

# Re-open iudex.core.filters.MojiBakeFilter to add config file
# based initialization.
class MojiBakeFilter

# Alt constructor taking a configuration file in `mojibake -t`
# format.
def initialize( key, config_file = MojiBake::DEFAULT_CONFIG )
super( key, *MojiBake.load_config( config_file ) )
end

end

end

end
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011 David Kellum
* Copyright (c) 2011 David Kellum
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You may
@@ -17,17 +17,15 @@
package iudex.core.filters;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.gravitext.htmap.Key;
import com.gravitext.htmap.UniMap;

import iudex.filter.Described;
import iudex.filter.Filter;
import iudex.util.MojiBakeMapper;

public class MojiBakeFilter
implements Filter, Described
@@ -37,8 +35,7 @@ public MojiBakeFilter( Key<CharSequence> field,
Map<String,String> mojis )
{
_field = field;
_mojiPattern = Pattern.compile( regex );
_mojis = new HashMap<String,String>( mojis );
_mapper = new MojiBakeMapper( regex, mojis );
}

@Override
@@ -52,34 +49,12 @@ public boolean filter( UniMap content )
{
CharSequence in = content.get( _field );
if( in != null ) {
content.set( _field, recover( in ) );
content.set( _field, _mapper.recover( in ) );
}

return true;
}
private CharSequence recover( CharSequence in )
{
Matcher m = _mojiPattern.matcher( in );
StringBuilder out = new StringBuilder( in.length() );
int last = 0;
while( m.find() ) {
out.append( in, last, m.start() );
String moji = in.subSequence( m.start(), m.end() ).toString();
out.append( _mojis.get( moji ) );
last = m.end();
}
out.append( in, last, in.length() );

if( out.length() < in.length() ) {
return recover( out );
}
else {
return out;
}
}

private final Key<CharSequence> _field;

private final Pattern _mojiPattern;
private final HashMap<String, String> _mojis;
private final MojiBakeMapper _mapper;
}
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2011 David Kellum
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package iudex.util;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MojiBakeMapper
{
public MojiBakeMapper( String regex,
Map<String,String> mojis )
{
_mojiPattern = Pattern.compile( regex );
_mojis = new HashMap<String,String>( mojis );
}

public CharSequence recover( CharSequence in )
{
Matcher m = _mojiPattern.matcher( in );
StringBuilder out = new StringBuilder( in.length() );
int last = 0;
while( m.find() ) {
out.append( in, last, m.start() );
String moji = in.subSequence( m.start(), m.end() ).toString();
out.append( _mojis.get( moji ) );
last = m.end();
}
out.append( in, last, in.length() );

if( out.length() < in.length() ) {
return recover( out );
}
else {
return out;
}
}

private final Pattern _mojiPattern;
private final HashMap<String, String> _mojis;
}

0 comments on commit ee85ac1

Please sign in to comment.