Permalink
Browse files

Merge pull request #20 from adrnai/master

Updated snippet encoder
  • Loading branch information...
2 parents 5816540 + d12a8d9 commit 5743b432578748b296067b7b5df6161798f428c5 @iladriano iladriano committed Jan 16, 2012
View
@@ -102,11 +102,6 @@
<version>1.2</version>
</dependency>
<dependency>
- <groupId>commons-lang</groupId>
- <artifactId>commons-lang</artifactId>
- <version>2.6</version>
- </dependency>
- <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
@@ -29,14 +29,14 @@
import com.flaptor.indextank.query.Query;
import com.flaptor.indextank.query.TermQuery;
import com.flaptor.indextank.storage.alternatives.DocumentStorage;
-import com.flaptor.indextank.util.FlaptorHtmlEntities;
+import com.flaptor.indextank.util.CharacterTranslator;
import com.flaptor.util.Execute;
+import com.flaptor.util.Pair;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
-import com.flaptor.util.Pair;
public class SnippetSearcher extends AbstractDocumentSearcher {
private static final Logger logger = Logger.getLogger(Execute.whoAmI());
@@ -333,10 +333,9 @@ private void escapeAndAppend(StringBuilder dest, String str, int start, int offs
if (str == null) {
return;
}
-
- FlaptorHtmlEntities.HTML40.escape(dest, str, start, offset);
- }
+ CharacterTranslator.HTML4.escape(dest, str, start, offset);
+ }
}
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2011 LinkedIn, Inc
+ *
+ * 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 com.flaptor.indextank.util;
+
+import java.util.HashMap;
+
+import org.apache.commons.lang3.text.translate.EntityArrays;
+
+public class CharacterTranslator {
+
+ public static final CharacterTranslator HTML4 = new CharacterTranslator(
+ EntityArrays.BASIC_ESCAPE(),
+ EntityArrays.ISO8859_1_ESCAPE(),
+ EntityArrays.HTML40_EXTENDED_ESCAPE()
+ );
+
+ private HashMap<Character, CharSequence> lookupMap;
+
+ /**
+ * Merges parameter arrays into a map.
+ * Assumes that parameter arrays have a 1-char key.
+ * @param lookup
+ */
+ public CharacterTranslator(CharSequence[][] ... lookup) {
+ lookupMap = new HashMap<Character, CharSequence>();
+ for(CharSequence[][] l: lookup) {
+ for(CharSequence[] seq: l) {
+ lookupMap.put(seq[0].charAt(0), seq[1]);
+ }
+ }
+ }
+
+ public void escape(StringBuilder output, String input, int start, int offset) {
+ for (int i = start; i < offset; i++) {
+ Character c = input.charAt(i);
+ CharSequence seq = lookupMap.get(c);
+ if (seq != null) {
+ output.append(seq);
+ } else if (c > 0x7F) {
+ output.append("&#");
+ output.append(Integer.toString(c, 10));
+ output.append(';');
+ } else {
+ output.append(c);
+ }
+ }
+ }
+
+}
Oops, something went wrong.

0 comments on commit 5743b43

Please sign in to comment.