rmuir commented on a change in pull request #15: URL: https://github.com/apache/lucene/pull/15#discussion_r600505701
########## File path: lucene/analysis/icu/src/java/org/apache/lucene/analysis/icu/ICUTransformCharFilterFactory.java ########## @@ -0,0 +1,338 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.lucene.analysis.icu; + +import com.ibm.icu.impl.Utility; +import com.ibm.icu.text.Normalizer2; +import com.ibm.icu.text.Transliterator; +import com.ibm.icu.text.UTF16; +import java.io.Reader; +import java.util.Arrays; +import java.util.Locale; +import java.util.Map; +import org.apache.lucene.analysis.CharFilterFactory; + +/** + * Factory for {@link ICUTransformCharFilter}. + * + * <p>Supports the following attributes: + * + * <ul> + * <li>id (mandatory): A Transliterator ID, one from {@link Transliterator#getAvailableIDs()} + * <li>direction (optional): Either 'forward' or 'reverse'. Default is forward. + * </ul> + * + * @see Transliterator + * @since 8.3.0 + * @lucene.spi {@value #NAME} + */ +public class ICUTransformCharFilterFactory extends CharFilterFactory { + + /** SPI name */ + public static final String NAME = "icuTransform"; + + static final String MAX_ROLLBACK_BUFFER_CAPACITY_ARGNAME = "maxRollbackBufferCapacity"; + static final String FAIL_ON_ROLLBACK_BUFFER_OVERFLOW_ARGNAME = "failOnRollbackBufferOverflow"; + private final NormType leading; + private final Transliterator transliterator; + private final NormType trailing; + private final int maxRollbackBufferCapacity; + private final boolean failOnRollbackBufferOverflow; + + // TODO: add support for custom rules + /** Creates a new ICUTransformFilterFactory */ + public ICUTransformCharFilterFactory(Map<String, String> args) { + this(args, false); + } + + /** package access, to allow tests to suppress unicode normalization externalization */ + ICUTransformCharFilterFactory( + Map<String, String> args, boolean suppressUnicodeNormalizationExternalization) { + super(args); + String id = require(args, "id"); + String direction = + get(args, "direction", Arrays.asList("forward", "reverse"), "forward", false); + int dir = "forward".equals(direction) ? Transliterator.FORWARD : Transliterator.REVERSE; + int tmpCapacityHint = + getInt( + args, + MAX_ROLLBACK_BUFFER_CAPACITY_ARGNAME, + ICUTransformCharFilter.DEFAULT_MAX_ROLLBACK_BUFFER_CAPACITY); + this.maxRollbackBufferCapacity = tmpCapacityHint == -1 ? Integer.MAX_VALUE : tmpCapacityHint; + this.failOnRollbackBufferOverflow = + getBoolean( + args, + FAIL_ON_ROLLBACK_BUFFER_OVERFLOW_ARGNAME, + ICUTransformCharFilter.DEFAULT_FAIL_ON_ROLLBACK_BUFFER_OVERFLOW); + Transliterator stockTransliterator = Transliterator.getInstance(id, dir); + if (suppressUnicodeNormalizationExternalization) { + this.leading = null; + this.transliterator = stockTransliterator; + this.trailing = null; + } else { + ExternalNormalization ext = externalizeUnicodeNormalization(stockTransliterator); + this.leading = ext.leading; + this.transliterator = ext.t; + this.trailing = ext.trailing; + } + if (!args.isEmpty()) { + throw new IllegalArgumentException("Unknown parameters: " + args); + } + } + + /** + * for tests; check whether unicode normalization externalization optimization is actually applied + */ + boolean externalizedUnicodeNormalization() { + return leading != null || trailing != null; + } + + private static final Reader wrapReader(NormType normType, Reader r) { + if (normType == null) { + return r; + } + switch (normType) { + case NFC: + return new ICUNormalizer2CharFilter(r, Normalizer2.getNFCInstance()); + case NFD: + return new ICUNormalizer2CharFilter(r, Normalizer2.getNFDInstance()); + case NFKC: + return new ICUNormalizer2CharFilter(r, Normalizer2.getNFKCInstance()); + case NFKD: + return new ICUNormalizer2CharFilter(r, Normalizer2.getNFKDInstance()); + default: + throw new UnsupportedOperationException( + "test not yet able to compensate externally for normalization type \"" + + normType + + "\""); + } + } + + /** Default ctor for compatibility with SPI */ + public ICUTransformCharFilterFactory() { + throw defaultCtorException(); + } + + @Override + public Reader create(Reader input) { + input = wrapReader(leading, input); + input = + new ICUTransformCharFilter( + input, transliterator, maxRollbackBufferCapacity, failOnRollbackBufferOverflow); + return wrapReader(trailing, input); + } + + private static final boolean ESCAPE_UNPRINTABLE = true; + + /** + * Attempts to detect and externalize any leading or trailing top-level Unicode normalization. In + * the event that such normalization is detected, a new "core" Transliterator (with any detected + * pre-/post-normalization removed) is created and returned. + * + * <p>The creation of any new "core" Transliterator (and much of the actual code in this method) + * is based on the {@link com.ibm.icu.text.CompoundTransliterator#toRules(boolean)} method (with + * the boolean arg replaced by {@link #ESCAPE_UNPRINTABLE} -- always <code>true</code> in this + * context). + * + * @param t the Transliterator to base modified rules on. + * @return simple ExternalNormalization struct containing a non-null Transliterator, if possible + * with any leading and trailing Unicode normalization externalized. The effect of applying + * the resulting leading Unicode norm, Transliterator, and trailing Unicode norm, should be + * equivalent to the effect of applying the input Transliterator t. + */ + private static ExternalNormalization externalizeUnicodeNormalization(Transliterator t) { + final Transliterator[] trans = t.getElements(); + final String topLevelId = t.getID(); Review comment: this line is dead code (if you merge up to `main` the ECJ compiler will find it) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org