/plugins/LogViewer/tags/rel-0-8/src/java/logviewer/LogTypeParser.java

# · Java · 388 lines · 328 code · 24 blank · 36 comment · 203 complexity · 510d1bf94f57e08fac13a757ac560626 MD5 · raw file

  1. package logviewer;
  2. import java.io.*;
  3. import java.util.*;
  4. import java.util.regex.*;
  5. import javax.xml.parsers.*;
  6. import org.w3c.dom.*;
  7. import org.xml.sax.*;
  8. public class LogTypeParser {
  9. /**
  10. * Gets the default LogType to use when no other LogTypes are found.
  11. *
  12. * @return The default LogType value
  13. */
  14. public LogType getDefaultLogType() {
  15. LogType defaultType = new LogType( "default" );
  16. defaultType.addColumn( "Message" );
  17. return defaultType;
  18. }
  19. public List parse( InputStream in ) {
  20. // the list of LogTypes to return
  21. List list = new ArrayList();
  22. // parse the input stream and create a list of LogTypes --
  23. // this is ugly...
  24. try {
  25. // read the xml file
  26. InputSource source = new InputSource( in );
  27. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  28. //factory.setValidating(false);
  29. DocumentBuilder builder = factory.newDocumentBuilder();
  30. Document doc = builder.parse( source );
  31. Node root = doc.getDocumentElement();
  32. NodeList childNodes = root.getChildNodes();
  33. // if no settings in file, use default log type
  34. if ( childNodes == null || childNodes.getLength() == 0 ) {
  35. //System.out.println("no settings in file");
  36. list.add( getDefaultLogType() );
  37. return list;
  38. }
  39. // parse the xml file
  40. for ( int x = 0; x < childNodes.getLength(); x++ ) {
  41. Node logNode = childNodes.item( x );
  42. String nodeName = logNode.getNodeName();
  43. // look for "log" nodes
  44. if ( "log".equals( nodeName ) ) {
  45. // "log" nodes must have a "name" attribute
  46. NamedNodeMap logAttrs = logNode.getAttributes();
  47. if ( logAttrs != null && logAttrs.getLength() > 0 ) {
  48. Node nameNode = logAttrs.getNamedItem( "name" );
  49. if ( nameNode != null ) {
  50. String name = nameNode.getNodeValue();
  51. if ( name == null || name.length() == 0 )
  52. continue;
  53. // got a valid name, so create the log type
  54. LogType type = new LogType( name );
  55. list.add( type );
  56. // get the settings for this log type
  57. NodeList childContents = logNode.getChildNodes();
  58. if ( childContents != null && childContents.getLength() > 0 ) {
  59. for ( int i = 0; i < childContents.getLength(); i++ ) {
  60. // most settings elements have CTEXT data
  61. Node node = childContents.item( i );
  62. nodeName = node.getNodeName();
  63. String nodeValue = node.getFirstChild() == null ? "" : node.getFirstChild().getNodeValue();
  64. //System.out.println("nodeName = " + nodeName + ", nodeValue = " + nodeValue);
  65. // handle individual child nodes...
  66. // file_name_glob has a CTEXT section only
  67. if ( "file_name_glob".equals( nodeName ) )
  68. type.setFileNameGlob( nodeValue );
  69. // first_line_glob has a CTEXT section only
  70. else if ( "first_line_glob".equals( nodeName ) )
  71. type.setFirstLineGlob( nodeValue );
  72. // regular expression to separate log entries
  73. else if ( "entry_separator".equals( nodeName ) )
  74. type.setRowSeparatorRegex( nodeValue );
  75. // row_regex has the regular expression as a CTEXT section,
  76. // group and flags are attributes. Flags are the same flags as used
  77. // in java.util.regex.Pattern, except all are lowercase.
  78. else if ( "entry_regex".equals( nodeName ) ) {
  79. NamedNodeMap regexAttrs = node.getAttributes();
  80. boolean include = true;
  81. int flags = 0;
  82. if ( regexAttrs != null && regexAttrs.getLength() > 0 ) {
  83. Node attrNode = regexAttrs.getNamedItem( "include" );
  84. String value;
  85. if ( attrNode != null ) {
  86. value = attrNode.getNodeValue();
  87. if ( value != null && value.length() > 0 ) {
  88. include = StringUtils.stringToBoolean( value );
  89. }
  90. }
  91. attrNode = regexAttrs.getNamedItem( "case_insensitive" );
  92. if ( attrNode != null ) {
  93. value = attrNode.getNodeValue();
  94. if ( value != null && value.length() > 0 ) {
  95. if ( StringUtils.stringToBoolean( value ) )
  96. flags += Pattern.CASE_INSENSITIVE;
  97. }
  98. }
  99. attrNode = regexAttrs.getNamedItem( "dotall" );
  100. if ( attrNode != null ) {
  101. value = attrNode.getNodeValue();
  102. if ( value != null && value.length() > 0 ) {
  103. if ( StringUtils.stringToBoolean( value ) )
  104. flags += Pattern.DOTALL;
  105. }
  106. }
  107. else {
  108. // default for dotall is true
  109. flags += Pattern.DOTALL;
  110. }
  111. attrNode = regexAttrs.getNamedItem( "multiline" );
  112. if ( attrNode != null ) {
  113. value = attrNode.getNodeValue();
  114. if ( value != null && value.length() > 0 ) {
  115. if ( StringUtils.stringToBoolean( value ) )
  116. flags += Pattern.MULTILINE;
  117. }
  118. }
  119. attrNode = regexAttrs.getNamedItem( "unicode_case" );
  120. if ( attrNode != null ) {
  121. value = attrNode.getNodeValue();
  122. if ( value != null && value.length() > 0 ) {
  123. if ( StringUtils.stringToBoolean( value ) )
  124. flags += Pattern.UNICODE_CASE;
  125. }
  126. }
  127. attrNode = regexAttrs.getNamedItem( "canon_eq" );
  128. if ( attrNode != null ) {
  129. value = attrNode.getNodeValue();
  130. if ( value != null && value.length() > 0 ) {
  131. if ( StringUtils.stringToBoolean( value ) )
  132. flags += Pattern.CANON_EQ;
  133. }
  134. }
  135. attrNode = regexAttrs.getNamedItem( "unix_lines" );
  136. if ( attrNode != null ) {
  137. value = attrNode.getNodeValue();
  138. if ( value != null && value.length() > 0 ) {
  139. if ( StringUtils.stringToBoolean( value ) )
  140. flags += Pattern.UNIX_LINES;
  141. }
  142. }
  143. }
  144. type.setRowRegex( nodeValue, include, flags );
  145. }
  146. // same as entry_regex, but to identify color
  147. else if ( "entry_regex_color".equals( nodeName ) ) {
  148. NamedNodeMap regexAttrs = node.getAttributes();
  149. boolean include = true;
  150. int flags = 0;
  151. int color = -1;
  152. if ( regexAttrs != null && regexAttrs.getLength() > 0 ) {
  153. Node attrNode = regexAttrs.getNamedItem( "include" );
  154. String value;
  155. if ( attrNode != null ) {
  156. value = attrNode.getNodeValue();
  157. if ( value != null && value.length() > 0 ) {
  158. include = StringUtils.stringToBoolean( value );
  159. }
  160. }
  161. attrNode = regexAttrs.getNamedItem( "case_insensitive" );
  162. if ( attrNode != null ) {
  163. value = attrNode.getNodeValue();
  164. if ( value != null && value.length() > 0 ) {
  165. if ( StringUtils.stringToBoolean( value ) )
  166. flags += Pattern.CASE_INSENSITIVE;
  167. }
  168. }
  169. attrNode = regexAttrs.getNamedItem( "dotall" );
  170. if ( attrNode != null ) {
  171. value = attrNode.getNodeValue();
  172. if ( value != null && value.length() > 0 ) {
  173. if ( StringUtils.stringToBoolean( value ) )
  174. flags += Pattern.DOTALL;
  175. }
  176. }
  177. else {
  178. // default for dotall is true
  179. flags += Pattern.DOTALL;
  180. }
  181. attrNode = regexAttrs.getNamedItem( "multiline" );
  182. if ( attrNode != null ) {
  183. value = attrNode.getNodeValue();
  184. if ( value != null && value.length() > 0 ) {
  185. if ( StringUtils.stringToBoolean( value ) )
  186. flags += Pattern.MULTILINE;
  187. }
  188. }
  189. attrNode = regexAttrs.getNamedItem( "unicode_case" );
  190. if ( attrNode != null ) {
  191. value = attrNode.getNodeValue();
  192. if ( value != null && value.length() > 0 ) {
  193. if ( StringUtils.stringToBoolean( value ) )
  194. flags += Pattern.UNICODE_CASE;
  195. }
  196. }
  197. attrNode = regexAttrs.getNamedItem( "canon_eq" );
  198. if ( attrNode != null ) {
  199. value = attrNode.getNodeValue();
  200. if ( value != null && value.length() > 0 ) {
  201. if ( StringUtils.stringToBoolean( value ) )
  202. flags += Pattern.CANON_EQ;
  203. }
  204. }
  205. attrNode = regexAttrs.getNamedItem( "unix_lines" );
  206. if ( attrNode != null ) {
  207. value = attrNode.getNodeValue();
  208. if ( value != null && value.length() > 0 ) {
  209. if ( StringUtils.stringToBoolean( value ) )
  210. flags += Pattern.UNIX_LINES;
  211. }
  212. }
  213. attrNode = regexAttrs.getNamedItem( "color" );
  214. if ( attrNode != null ) {
  215. value = attrNode.getNodeValue();
  216. if ( value != null && value.length() > 0 ) {
  217. if (value.startsWith("#")) {
  218. value = "0x" + value.substring(1);
  219. }
  220. color = Integer.parseInt( value );
  221. }
  222. }
  223. }
  224. if ( color >= 0 ) {
  225. type.setRowColor( nodeValue, include, flags, color );
  226. }
  227. }
  228. // column_regex has the regular expression as a CTEXT section,
  229. // group and flags are attributes. Flags are the same flags as used
  230. // in java.util.regex.Pattern, except all are lowercase.
  231. else if ( "column_regex".equals( nodeName ) ) {
  232. NamedNodeMap regexAttrs = node.getAttributes();
  233. String groups = null;
  234. int flags = 0;
  235. if ( regexAttrs != null && regexAttrs.getLength() > 0 ) {
  236. Node attrNode = regexAttrs.getNamedItem( "groups" );
  237. String value;
  238. if ( attrNode != null ) {
  239. value = attrNode.getNodeValue();
  240. if ( value != null && value.length() > 0 ) {
  241. groups = value;
  242. }
  243. }
  244. attrNode = regexAttrs.getNamedItem( "case_insensitive" );
  245. if ( attrNode != null ) {
  246. value = attrNode.getNodeValue();
  247. if ( value != null && value.length() > 0 ) {
  248. if ( StringUtils.stringToBoolean( value ) )
  249. flags += Pattern.CASE_INSENSITIVE;
  250. }
  251. }
  252. attrNode = regexAttrs.getNamedItem( "dotall" );
  253. if ( attrNode != null ) {
  254. value = attrNode.getNodeValue();
  255. if ( value != null && value.length() > 0 ) {
  256. if ( StringUtils.stringToBoolean( value ) )
  257. flags += Pattern.DOTALL;
  258. }
  259. }
  260. attrNode = regexAttrs.getNamedItem( "multiline" );
  261. if ( attrNode != null ) {
  262. value = attrNode.getNodeValue();
  263. if ( value != null && value.length() > 0 ) {
  264. if ( StringUtils.stringToBoolean( value ) )
  265. flags += Pattern.MULTILINE;
  266. }
  267. }
  268. attrNode = regexAttrs.getNamedItem( "unicode_case" );
  269. if ( attrNode != null ) {
  270. value = attrNode.getNodeValue();
  271. if ( value != null && value.length() > 0 ) {
  272. if ( StringUtils.stringToBoolean( value ) )
  273. flags += Pattern.UNICODE_CASE;
  274. }
  275. }
  276. attrNode = regexAttrs.getNamedItem( "canon_eq" );
  277. if ( attrNode != null ) {
  278. value = attrNode.getNodeValue();
  279. if ( value != null && value.length() > 0 ) {
  280. if ( StringUtils.stringToBoolean( value ) )
  281. flags += Pattern.CANON_EQ;
  282. }
  283. }
  284. attrNode = regexAttrs.getNamedItem( "unix_lines" );
  285. if ( attrNode != null ) {
  286. value = attrNode.getNodeValue();
  287. if ( value != null && value.length() > 0 ) {
  288. if ( StringUtils.stringToBoolean( value ) )
  289. flags += Pattern.UNIX_LINES;
  290. }
  291. }
  292. }
  293. type.setColumnRegex( nodeValue, groups, flags );
  294. }
  295. // column_delimiter has a CTEXT section only
  296. else if ( "column_delimiter".equals( nodeName ) )
  297. type.setColumnDelimiter( nodeValue );
  298. // the "columns" element has "column" subelements
  299. else if ( "columns".equals( nodeName ) ) {
  300. NodeList columns = node.getChildNodes();
  301. if ( columns != null && columns.getLength() > 0 ) {
  302. for ( int j = 0; j < columns.getLength(); j++ ) {
  303. Node columnNode = columns.item( j );
  304. String columnName = columnNode.getFirstChild() == null ? "" : columnNode.getFirstChild().getNodeValue();
  305. if ( columnName.length() == 0 )
  306. continue;
  307. // "column" elements may have "width" and "offset" attributes
  308. int width = -1;
  309. int offset = -1;
  310. NamedNodeMap columnAttrs = columnNode.getAttributes();
  311. if ( columnAttrs != null && columnAttrs.getLength() > 0 ) {
  312. Node attrNode = columnAttrs.getNamedItem( "width" );
  313. if ( attrNode != null ) {
  314. String value = attrNode.getNodeValue();
  315. if ( value != null && value.length() > 0 ) {
  316. try {
  317. width = Integer.parseInt( value );
  318. if ( width < 0 )
  319. width = -1;
  320. }
  321. catch ( NumberFormatException e ) {}
  322. }
  323. }
  324. attrNode = columnAttrs.getNamedItem( "offset" );
  325. if ( attrNode != null ) {
  326. String value = attrNode.getNodeValue();
  327. if ( value != null && value.length() > 0 ) {
  328. try {
  329. offset = Integer.parseInt( value );
  330. if ( offset < 0 )
  331. offset = -1;
  332. }
  333. catch ( NumberFormatException e ) {}
  334. }
  335. }
  336. }
  337. if ( width >= -1 && offset >= 0 ) {
  338. type.addColumn( columnName, offset, width );
  339. }
  340. else if ( width >= -1 ) {
  341. type.addColumn( columnName, width );
  342. }
  343. else {
  344. type.addColumn( columnName );
  345. }
  346. }
  347. }
  348. }
  349. }
  350. }
  351. ///System.out.println(type.toString());
  352. }
  353. }
  354. }
  355. }
  356. }
  357. catch ( Exception e ) {
  358. e.printStackTrace();
  359. list = new ArrayList();
  360. list.add( getDefaultLogType() );
  361. }
  362. return list;
  363. }
  364. }