Showing the changes to java/pkg/sun/jvmstat/perfdata/monitor/AliasFileParser.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,165 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved. | |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
| 4 | + * | |
| 5 | + * This code is free software; you can redistribute it and/or modify it | |
| 6 | + * under the terms of the GNU General Public License version 2 only, as | |
| 7 | + * published by the Free Software Foundation. Oracle designates this | |
| 8 | + * particular file as subject to the "Classpath" exception as provided | |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. | |
| 10 | + * | |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT | |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that | |
| 15 | + * accompanied this code). | |
| 16 | + * | |
| 17 | + * You should have received a copy of the GNU General Public License version | |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, | |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 20 | + * | |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
| 22 | + * or visit www.oracle.com if you need additional information or have any | |
| 23 | + * questions. | |
| 24 | + */ | |
| 25 | + | |
| 26 | +package sun.jvmstat.perfdata.monitor; | |
| 27 | + | |
| 28 | +import java.net.*; | |
| 29 | +import java.io.*; | |
| 30 | +import java.util.*; | |
| 31 | +import java.util.regex.*; | |
| 32 | + | |
| 33 | +/** | |
| 34 | + * Class for parsing alias files. File format is expected to follow | |
| 35 | + * the following syntax: | |
| 36 | + * | |
| 37 | + * alias name [alias]* | |
| 38 | + * | |
| 39 | + * Java style comments can occur anywhere within the file. | |
| 40 | + * @author Brian Doherty | |
| 41 | + * @since 1.5 | |
| 42 | + */ | |
| 43 | +public class AliasFileParser { | |
| 44 | + private static final String ALIAS = "alias"; | |
| 45 | + private static final boolean DEBUG = false; | |
| 46 | + | |
| 47 | + // other variables | |
| 48 | + private URL inputfile; | |
| 49 | + private StreamTokenizer st; | |
| 50 | + private Token currentToken; | |
| 51 | + | |
| 52 | + AliasFileParser(URL inputfile) { | |
| 53 | + this.inputfile = inputfile; | |
| 54 | + } | |
| 55 | + | |
| 56 | + // value class to hold StreamTokenizer token values | |
| 57 | + private class Token { | |
| 58 | + public String sval; | |
| 59 | + public int ttype; | |
| 60 | + | |
| 61 | + public Token(int ttype, String sval) { | |
| 62 | + this.ttype = ttype; | |
| 63 | + this.sval = sval; | |
| 64 | + } | |
| 65 | + } | |
| 66 | + | |
| 67 | + private void logln(String s) { | |
| 68 | + if (DEBUG) { | |
| 69 | + System.err.println(s); | |
| 70 | + } | |
| 71 | + } | |
| 72 | + | |
| 73 | + /** | |
| 74 | + * method to get the next token as a Token type | |
| 75 | + */ | |
| 76 | + private void nextToken() throws IOException { | |
| 77 | + st.nextToken(); | |
| 78 | + currentToken = new Token(st.ttype, st.sval); | |
| 79 | + | |
| 80 | + logln("Read token: type = " + currentToken.ttype | |
| 81 | + + " string = " + currentToken.sval); | |
| 82 | + } | |
| 83 | + | |
| 84 | + /** | |
| 85 | + * method to match the current Token to a specified token type and | |
| 86 | + * value Throws a SyntaxException if token doesn't match. | |
| 87 | + */ | |
| 88 | + private void match(int ttype, String token) | |
| 89 | + throws IOException, SyntaxException { | |
| 90 | + | |
| 91 | + if ((currentToken.ttype == ttype) | |
| 92 | + && (currentToken.sval.compareTo(token) == 0)) { | |
| 93 | + logln("matched type: " + ttype + " and token = " | |
| 94 | + + currentToken.sval); | |
| 95 | + nextToken(); | |
| 96 | + } else { | |
| 97 | + throw new SyntaxException(st.lineno()); | |
| 98 | + } | |
| 99 | + } | |
| 100 | + | |
| 101 | + | |
| 102 | + /* | |
| 103 | + * method to match the current Token to a specified token type. | |
| 104 | + * Throws a SyntaxException if token doesn't match. | |
| 105 | + */ | |
| 106 | + private void match(int ttype) throws IOException, SyntaxException { | |
| 107 | + if (currentToken.ttype == ttype) { | |
| 108 | + logln("matched type: " + ttype + ", token = " + currentToken.sval); | |
| 109 | + nextToken(); | |
| 110 | + } else { | |
| 111 | + throw new SyntaxException(st.lineno()); | |
| 112 | + } | |
| 113 | + } | |
| 114 | + | |
| 115 | + private void match(String token) throws IOException, SyntaxException { | |
| 116 | + match(StreamTokenizer.TT_WORD, token); | |
| 117 | + } | |
| 118 | + | |
| 119 | + /** | |
| 120 | + * method to parse the given input file. | |
| 121 | + */ | |
| 122 | + public void parse(Map<String, ArrayList<String>> map) throws SyntaxException, IOException { | |
| 123 | + | |
| 124 | + if (inputfile == null) { | |
| 125 | + return; | |
| 126 | + } | |
| 127 | + | |
| 128 | + BufferedReader r = new BufferedReader( | |
| 129 | + new InputStreamReader(inputfile.openStream())); | |
| 130 | + st = new StreamTokenizer(r); | |
| 131 | + | |
| 132 | + // allow both forms of commenting styles | |
| 133 | + st.slashSlashComments(true); | |
| 134 | + st.slashStarComments(true); | |
| 135 | + st.wordChars('_','_'); | |
| 136 | + | |
| 137 | + nextToken(); | |
| 138 | + | |
| 139 | + while (currentToken.ttype != StreamTokenizer.TT_EOF) { | |
| 140 | + // look for the start symbol | |
| 141 | + if ((currentToken.ttype != StreamTokenizer.TT_WORD) | |
| 142 | + || (currentToken.sval.compareTo(ALIAS) != 0)) { | |
| 143 | + nextToken(); | |
| 144 | + continue; | |
| 145 | + } | |
| 146 | + | |
| 147 | + match(ALIAS); | |
| 148 | + String name = currentToken.sval; | |
| 149 | + match(StreamTokenizer.TT_WORD); | |
| 150 | + | |
| 151 | + ArrayList<String> aliases = new ArrayList<String>(); | |
| 152 | + | |
| 153 | + do { | |
| 154 | + aliases.add(currentToken.sval); | |
| 155 | + match(StreamTokenizer.TT_WORD); | |
| 156 | + | |
| 157 | + } while ((currentToken.ttype != StreamTokenizer.TT_EOF) | |
| 158 | + && (currentToken.sval.compareTo(ALIAS) != 0)); | |
| 159 | + | |
| 160 | + logln("adding map entry for " + name + " values = " + aliases); | |
| 161 | + | |
| 162 | + map.put(name, aliases); | |
| 163 | + } | |
| 164 | + } | |
| 165 | +} | |