001: /**
002: * <copyright>
003: *
004: * Copyright 2002-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */package org.cougaar.tools.csmart.util;
026:
027: import org.cougaar.tools.csmart.ui.viewer.CSMART;
028: import org.cougaar.util.ConfigFinder;
029: import org.cougaar.util.log.Logger;
030:
031: import java.io.File;
032: import java.io.FileInputStream;
033: import java.io.IOException;
034: import java.nio.CharBuffer;
035: import java.nio.MappedByteBuffer;
036: import java.nio.channels.FileChannel;
037: import java.nio.charset.CharacterCodingException;
038: import java.nio.charset.Charset;
039: import java.nio.charset.CharsetDecoder;
040: import java.util.regex.Matcher;
041: import java.util.regex.Pattern;
042:
043: /**
044: * FileParseUtil.java
045: *
046: *
047: * Created: Wed Mar 13 09:45:18 2002
048: *
049: */
050: public class FileParseUtil {
051:
052: public FileParseUtil() {
053: }
054:
055: /**
056: * Searches the given file for the specified regular expression
057: * pattern. Returns true or false based on the result of the find.
058: *
059: * @param filename - The file to search for regex.
060: * @param pattern - Regular Expression pattern to search for.
061: * @return boolean indicating the result of the search.
062: */
063: public static final boolean containsPattern(String filename,
064: String pattern) {
065: Logger log = CSMART
066: .createLogger("org.cougaar.tools.csmart.FileParseUtil");
067:
068: // if(log.isDebugEnabled()) {
069: // log.debug("Using File: " + filename);
070: // }
071:
072: // Create the CharBuffer for the file.
073: FileInputStream iStream = null;
074: try {
075: File input = new File(filename);
076: if (!input.exists()) {
077: // Try the ConfigFinder
078: // FIXME: On windows, maybe we need to use SocietyFinder?
079: input = ConfigFinder.getInstance("csmart").locateFile(
080: filename);
081: }
082: if (input != null && input.exists()) {
083: // if (log.isDebugEnabled()) {
084: // log.debug("Found file: " + input.getPath());
085: // }
086: iStream = new FileInputStream(input);
087: } else {
088: // Couldn't find the file at all. Return false;
089: if (log.isDebugEnabled()) {
090: log.debug("Couldn't find file: " + filename);
091: }
092: return false;
093: }
094: } catch (IOException e) {
095: if (log.isErrorEnabled()) {
096: log.error("Exception finding file", e);
097: }
098: }
099:
100: FileChannel channel = iStream.getChannel();
101: int length = 0;
102: try {
103: length = (int) channel.size();
104: } catch (IOException e) {
105: if (log.isErrorEnabled()) {
106: log.error("Exception getting channel size", e);
107: }
108: }
109: MappedByteBuffer buffer = null;
110: try {
111: buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0,
112: length);
113: } catch (IOException e) {
114: if (log.isErrorEnabled()) {
115: log.error("Exception creating buffer", e);
116: }
117: }
118:
119: Charset charset = Charset.forName("ISO-8859-1");
120: CharsetDecoder decoder = charset.newDecoder();
121: CharBuffer cBuffer = null;
122: try {
123: cBuffer = decoder.decode(buffer);
124: } catch (CharacterCodingException cce) {
125: if (log.isErrorEnabled()) {
126: log.error("Exception decoding buffer", cce);
127: }
128: }
129:
130: // Now search the CharBuffer using regex Patterns.
131: Pattern ptrn = Pattern.compile(pattern);
132: Matcher matcher = ptrn.matcher(cBuffer);
133:
134: return matcher.find();
135:
136: }
137:
138: /**
139: * Tests the ini file to determine if it is a New ini.dat style, or
140: * an older ini.dat style. The old ini.dat style contains a
141: * [UniqueId] which does not exist in the newer style.
142: * <br>
143: * There are other differences, but this difference is the first and
144: * is the easiest to check existence of.
145: *
146: * @param filename - The file to determine if old or new style.
147: */
148: public static final boolean isOldStyleIni(String filename) {
149: if (FileParseUtil.containsPattern(filename, "^\\[UniqueId")
150: || FileParseUtil.containsPattern(filename, "^\\[UIC")
151: || !FileParseUtil
152: .containsPattern(filename,
153: "\\[Relationship\\]\\s*([^\\s#]+[^\\S\\n\\r]+){5}\\S*")) {
154:
155: return true;
156: } else {
157: return false;
158: }
159: }
160:
161: public static void main(String args[]) {
162: String filename = args[0];
163: if (FileParseUtil.isOldStyleIni(filename)) {
164: System.out.println("Yes, " + filename + " is old Style");
165: } else {
166: System.out.println("No, " + filename + " is not old Style");
167: }
168: }
169:
170: }// FileParseUtil
|