MatchPattern.java revision 8f8adeb1540d89588dba4c26d00a6ed6887cf6de
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync/**
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync *
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync *
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * The contents of this file are subject to the terms
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * of the Common Development and Distribution License
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * (the License). You may not use this file except in
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * compliance with the License.
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync *
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * You can obtain a copy of the License at
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * https://opensso.dev.java.net/public/CDDLv1.0.html or
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * opensso/legal/CDDLv1.0.txt
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * See the License for the specific language governing
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * permission and limitations under the License.
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync *
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * When distributing Covered Code, include this CDDL
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * Header Notice in each file and include the License file
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * at opensso/legal/CDDLv1.0.txt.
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * If applicable, add the following below the CDDL Header,
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * with the fields enclosed by brackets [] replaced by
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * your own identifying information:
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * "Portions Copyrighted [year] [name of copyright owner]"
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync *
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * $Id: MatchPattern.java,v 1.2 2008/06/25 05:51:29 qcheng Exp $
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync *
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync */
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync/*
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * Portions Copyrighted 2014 ForgeRock AS.
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync * Portions Copyrighted 2014 Nomura Research Institute, Ltd.
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync */
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsyncpackage com.sun.identity.install.tools.util;
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsyncpublic class MatchPattern {
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync public MatchPattern(String pattern, int matchType) {
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync setPattern(pattern);
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync setIsActiveFlag(true);
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync setMatchType(matchType);
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync setIgnoreCaseFlag(false);
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync setLastOccurranceInFile(false);
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync }
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync public boolean isPresent(String lineData) {
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync boolean matchFound = false;
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync int patternLength = getPattern().length();
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync lineData = lineData.trim();
b8e299dddd091ae24e0c08c45d91b8f937bd14d2vboxsync
int lineLength = lineData.length();
if (lineLength >= patternLength) {
if (isMatchFromStart()) {
matchFound = lineData.regionMatches(isIgnoreCase(), 0,
getPattern(), 0, patternLength);
} else if (isMatchFromEnd()) {
int frmOff = lineLength - patternLength;
matchFound = lineData.regionMatches(isIgnoreCase(), frmOff,
getPattern(), 0, patternLength);
} else if (isIgnoreCase()) {
String tempLine = new String(lineData);
String tempPattern = new String(getPattern());
tempLine = tempLine.toLowerCase();
tempPattern = tempPattern.toLowerCase();
if (tempLine.indexOf(tempPattern) >= 0) {
matchFound = true;
}
} else if (lineData.indexOf(getPattern()) >= 0) {
matchFound = true;
}
}
return matchFound;
}
public boolean isMatchForLastOccurranceInFile() {
return lastOccurranceInFile;
}
public boolean isActive() {
return isActive;
}
public void setIsActiveFlag(boolean flag) {
isActive = flag;
}
public String getPattern() {
return pattern;
}
public int getMatchType() {
return matchType;
}
public void setIgnoreCaseFlag(boolean flag) {
ignoreCase = flag;
}
public void setLastOccurranceInFile(boolean flag) {
lastOccurranceInFile = flag;
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("[ MatchPattern: pattern='").append(
getPattern()).append("'");
sb.append(", isActive=").append(isActive());
sb.append(", matchFromStart=").append(isMatchFromStart());
sb.append(", matchFromEnd=").append(isMatchFromEnd());
sb.append(", ignoreCase=").append(isIgnoreCase());
sb.append(", lastOccurranceInFile");
sb.append(isMatchForLastOccurranceInFile());
sb.append(" ]");
return sb.toString();
}
private boolean isMatchFromStart() {
return (getMatchType() == INT_MATCH_FROM_START);
}
private boolean isMatchFromEnd() {
return (getMatchType() == INT_MATCH_FROM_END);
}
private boolean isIgnoreCase() {
return ignoreCase;
}
private void setPattern(String pattern) {
this.pattern = pattern;
}
private void setMatchType(int type) {
if ((type == INT_MATCH_FROM_START) || (type == INT_MATCH_FROM_END)
|| (type == INT_MATCH_OCCURRANCE)) {
matchType = type;
} else {
throw new IllegalArgumentException("Invalid argument for match"
+ " type");
}
}
// Intially will be true. Once found it becomes inactive.
private boolean isActive;
// If true only the last occurrance will be removed. Otherwise the first.
private boolean lastOccurranceInFile;
private boolean ignoreCase;
private int matchType;
private String pattern;
public static final int INT_MATCH_OCCURRANCE = 0;
public static final int INT_MATCH_FROM_START = 1;
public static final int INT_MATCH_FROM_END = 2;
}