Path: blob/master/test/hotspot/jtreg/compiler/compilercontrol/share/JSONFile.java
41155 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package compiler.compilercontrol.share;2425import java.io.FileNotFoundException;26import java.io.PrintStream;27import java.util.Objects;28import java.util.Stack;2930/**31* Simple JSON file writer32*/33public class JSONFile implements AutoCloseable {34private final Stack<Element> stack;35private final String fileName;36private final PrintStream out;37private int spaces;3839/**40* JSON element41*/42public enum Element {43OBJECT,44ARRAY,45PAIR,46VALUE47}4849/**50* Constructor. Creates file with default name51*/52public JSONFile() {53this("directives_file.json");54}5556/**57* Constructor58*59* @param fileName file name60*/61public JSONFile(String fileName) {62this.spaces = 0;63this.stack = new Stack<>();64this.fileName = fileName;65try {66out = new PrintStream(fileName);67} catch (FileNotFoundException e) {68throw new Error("TESTBUG: can't open/create file " + fileName, e);69}70}7172/**73* Gets file name74*75* @return file name string76*/77public String getFileName() {78return fileName;79}8081/**82* Gets current JSON element in the file.83* The element is a current {@linkplain Element}84* that was written into a file.85*86* @return the type of the current element,87* or null if there are nothing written88*/89public Element getElement() {90if (stack.empty()) {91return null;92}93return stack.peek();94}9596/**97* Writes given type with a value to file.98* Note that only PAIR and VALUE types accept a single value parameter.99* OBJECT and ARRAY do not have a value100*101* @param element JSON element type102* @param value element's value103* @return this file instance104*/105public JSONFile write(Element element, String... value) {106if (value.length > 1) {107throw new Error("TESTBUG: Unexpected value length: "108+ value.length);109}110if (!stack.empty()) {111if (stack.peek() == Element.VALUE) {112out.print(", ");113stack.pop();114}115}116switch (element) {117case OBJECT:118out.print("{");119spaces++;120stack.push(Element.VALUE);121break;122case ARRAY:123out.print("[");124stack.push(Element.VALUE);125break;126case PAIR:127fillSpaces();128Objects.requireNonNull(value, "TESTBUG: " + element129+ "requires a value to be set");130out.print(value[0] + ": ");131break;132case VALUE:133Objects.requireNonNull(value, "TESTBUG: " + element134+ "requires a value to be set");135out.print(value[0]);136break;137}138stack.push(element);139return this;140}141142private void fillSpaces() {143out.println();144for (int i = 0; i < spaces; i++) {145// Fill with spaces to be more readable146out.print(" ");147}148}149150/**151* Ends current object or array of {@linkplain Element}152*153* @return this file instance154*/155public JSONFile end() {156if (!stack.empty()) {157Element prev = stack.pop();158while (prev != Element.OBJECT && prev != Element.ARRAY159&& !stack.empty()) {160prev = stack.pop();161}162switch (prev) {163case OBJECT:164spaces--;165fillSpaces();166out.print("}");167break;168case ARRAY:169out.print("]");170break;171default:172throw new Error("TESTBUG: Incorrect end. " +173"Wrong type found: " + prev);174}175} else {176throw new Error("TESTBUG: Incorrect end. Empty stack");177}178return this;179}180181/**182* Closes this file183*/184@Override185public void close() {186out.close();187}188}189190191