summaryrefslogtreecommitdiff
path: root/src/com/android/loganalysis/parser/AbstractSectionParser.java
diff options
context:
space:
mode:
authorEric Rowe <erowe@google.com>2013-02-27 18:47:16 -0800
committerEric Rowe <erowe@google.com>2013-02-28 11:06:30 -0800
commitb806548ef043fc36761ba9bf0167371e432bf0af (patch)
tree7bb98b2b72b423764a54401b03d34c02d6832373 /src/com/android/loganalysis/parser/AbstractSectionParser.java
parent3e3a6ce5de8fd927b2a025da105d4a917d0c465e (diff)
downloadloganalysis-b806548ef043fc36761ba9bf0167371e432bf0af.tar.gz
Move brillopad in tradefed to new project.
Code has been unmodified except for update packages and references, and removing CLog statements (to remove the dependency on ddmlib). Added .classpath and .project files for eclipse, and Android.mk files for the make system. Change-Id: I64d6764c08332b1c420a8f563a026aaa547e45bf
Diffstat (limited to 'src/com/android/loganalysis/parser/AbstractSectionParser.java')
-rw-r--r--src/com/android/loganalysis/parser/AbstractSectionParser.java124
1 files changed, 124 insertions, 0 deletions
diff --git a/src/com/android/loganalysis/parser/AbstractSectionParser.java b/src/com/android/loganalysis/parser/AbstractSectionParser.java
new file mode 100644
index 0000000..c57546d
--- /dev/null
+++ b/src/com/android/loganalysis/parser/AbstractSectionParser.java
@@ -0,0 +1,124 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * 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.android.loganalysis.parser;
+
+import com.android.loganalysis.item.IItem;
+import com.android.loganalysis.util.RegexTrie;
+
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * A {@link IParser} that splits an input file into discrete sections and passes each section to an
+ * {@link IParser} to parse.
+ * <p>
+ * Before parsing input, {@link IParser}s can be added with
+ * {@link #addSectionParser(IParser, String)}. The default parser is {@link NoopParser} but this can
+ * be overwritten by calling {@link #setParser(IParser)} before parsing the input.
+ * </p>
+ */
+public abstract class AbstractSectionParser implements IParser {
+ private RegexTrie<IParser> mSectionTrie = new RegexTrie<IParser>();
+ private IParser mCurrentParser = new NoopParser();
+ private List<String> mParseBlock = new LinkedList<String>();
+ private Map<String, IItem> mSections = new HashMap<String, IItem>();
+
+ /**
+ * A method to add a given section parser to the set of potential parsers to use.
+ *
+ * @param parser The {@link IParser} to add
+ * @param pattern The regular expression to trigger this parser
+ */
+ protected void addSectionParser(IParser parser, String pattern) {
+ if (parser == null) {
+ throw new NullPointerException("Parser is null");
+ }
+ if (pattern == null) {
+ throw new NullPointerException("Pattern is null");
+ }
+ mSectionTrie.put(parser, pattern);
+ }
+
+ /**
+ * Parse a line of input, either adding the input to the current block or switching parsers and
+ * running the current parser.
+ *
+ * @param line The line to parse
+ */
+ protected void parseLine(String line) {
+ IParser nextParser = mSectionTrie.retrieve(line);
+
+ if (nextParser == null) {
+ // no match, so buffer this for the current parser, if there is one
+ if (mCurrentParser != null) {
+ mParseBlock.add(line);
+ } else {
+ // CLog.w("Line outside of parsed section: %s", line);
+ }
+ } else {
+ runCurrentParser();
+ mParseBlock.clear();
+ mCurrentParser = nextParser;
+
+ onSwitchParser();
+ }
+ }
+
+ /**
+ * Signal that the input has finished and run the last parser.
+ */
+ protected void commit() {
+ runCurrentParser();
+ }
+
+ /**
+ * Gets the {@link IItem} for a given section.
+ *
+ * @param section The {@link IItem} type for the section.
+ * @return The {@link IItem}.
+ */
+ protected IItem getSection(String section) {
+ return mSections.get(section);
+ }
+
+ /**
+ * Set the {@link IParser}. Used to set the initial parser.
+ *
+ * @param parser The {@link IParser} to set.
+ */
+ protected void setParser(IParser parser) {
+ mCurrentParser = parser;
+ }
+
+ protected void onSwitchParser() {
+ }
+
+ /**
+ * Run the current parser and add the {@link IItem} to the sections map.
+ */
+ private void runCurrentParser() {
+ if (mCurrentParser != null) {
+ IItem item = mCurrentParser.parse(mParseBlock);
+ if (item != null && !(mCurrentParser instanceof NoopParser)) {
+ mSections.put(item.getType(), item);
+ // CLog.v("Just ran the %s parser", mCurrentParser.getClass().getSimpleName());
+ }
+ }
+ }
+}
+