1 /*
   2  * Copyright (c) 2016, 2018, 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 jdk.jfr.internal.jfc;
  27 import java.util.LinkedHashMap;
  28 import java.util.Map;
  29 
  30 import jdk.internal.org.xml.sax.Attributes;
  31 import jdk.internal.org.xml.sax.SAXException;
  32 import jdk.internal.org.xml.sax.helpers.DefaultHandler;
  33 
  34 final class JFCParserHandler extends DefaultHandler {
  35     private static final String ELEMENT_CONFIGURATION = "configuration";
  36     private static final String ELEMENT_EVENT_TYPE = "event";
  37     private static final String ELEMENT_SETTING = "setting";
  38     private static final String ATTRIBUTE_NAME = "name";
  39     private static final String ATTRIBUTE_LABEL = "label";
  40     private static final String ATTRIBUTE_DESCRIPTION = "description";
  41     private static final String ATTRIBUTE_PROVIDER = "provider";
  42     private static final String ATTRIBUTE_VERSION = "version";
  43 
  44     final Map<String, String> settings = new LinkedHashMap<String, String>();
  45     private String currentEventPath;
  46     private String currentSettingsName;
  47     private StringBuilder currentCharacters;
  48     String label;
  49     String provider;
  50     String description;
  51 
  52     @Override
  53     public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
  54         switch (qName.toLowerCase()) {
  55         case ELEMENT_CONFIGURATION:
  56             String version = attributes.getValue(ATTRIBUTE_VERSION);
  57             if (version == null || !version.startsWith("2.")) {
  58                throw new SAXException("This version of Flight Recorder can only read JFC file format version 2.x");
  59             }
  60             label = attributes.getValue(ATTRIBUTE_LABEL);
  61             description = getOptional(attributes, ATTRIBUTE_DESCRIPTION, "");
  62             provider = getOptional(attributes, ATTRIBUTE_PROVIDER, "");
  63             break;
  64         case ELEMENT_EVENT_TYPE:
  65             currentEventPath = attributes.getValue(ATTRIBUTE_NAME);
  66             break;
  67         case ELEMENT_SETTING:
  68             currentSettingsName = attributes.getValue(ATTRIBUTE_NAME);
  69             break;
  70         }
  71         currentCharacters = null;
  72     }
  73 
  74     private String getOptional(Attributes attributes, String name, String defaultValue) {
  75         String value = attributes.getValue(name);
  76         return value == null ? defaultValue : value;
  77     }
  78 
  79     @Override
  80     public void characters(char[] ch, int start, int length) throws SAXException {
  81         if (currentCharacters == null) {
  82             currentCharacters = new StringBuilder(length);
  83         }
  84         currentCharacters.append(ch, start, length);
  85     }
  86 
  87     @Override
  88     public void endElement(String uri, String localName, String qName) {
  89         switch (qName.toLowerCase()) {
  90         case ELEMENT_CONFIGURATION:
  91             break;
  92         case ELEMENT_EVENT_TYPE:
  93             currentEventPath = null;
  94             break;
  95         case ELEMENT_SETTING:
  96             String settingsValue = currentCharacters == null ? "" : currentCharacters.toString();
  97             settings.put(currentEventPath + "#" + currentSettingsName, "" + settingsValue);
  98             currentSettingsName = null;
  99             break;
 100         }
 101     }
 102 
 103     public Map<String, String> getSettings() {
 104         return settings;
 105     }
 106 }