org.apache.logging.log4j.layout.template.json.util.StringParameterParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of log4j-layout-template-json Show documentation
Show all versions of log4j-layout-template-json Show documentation
Apache Log4j Layout for JSON template.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.logging.log4j.layout.template.json.util;
import org.apache.logging.log4j.util.Strings;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.Callable;
public final class StringParameterParser {
private StringParameterParser() {}
public static final class Values {
private Values() {}
static NullValue nullValue() {
return NullValue.INSTANCE;
}
static StringValue stringValue(final String string) {
return new StringValue(string);
}
static DoubleQuotedStringValue doubleQuotedStringValue(
final String doubleQuotedString) {
return new DoubleQuotedStringValue(doubleQuotedString);
}
}
public interface Value {}
public static final class NullValue implements Value {
private static final NullValue INSTANCE = new NullValue();
private NullValue() {}
@Override
public String toString() {
return "null";
}
}
public static final class StringValue implements Value {
private final String string;
private StringValue(String string) {
this.string = string;
}
public String getString() {
return string;
}
@Override
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (object == null || getClass() != object.getClass()) {
return false;
}
StringValue that = (StringValue) object;
return string.equals(that.string);
}
@Override
public int hashCode() {
return 31 + Objects.hashCode(string);
}
@Override
public String toString() {
return string;
}
}
public static final class DoubleQuotedStringValue implements Value {
private final String doubleQuotedString;
private DoubleQuotedStringValue(String doubleQuotedString) {
this.doubleQuotedString = doubleQuotedString;
}
public String getDoubleQuotedString() {
return doubleQuotedString;
}
@Override
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (object == null || getClass() != object.getClass()) {
return false;
}
DoubleQuotedStringValue that = (DoubleQuotedStringValue) object;
return doubleQuotedString.equals(that.doubleQuotedString);
}
@Override
public int hashCode() {
return 31 + Objects.hashCode(doubleQuotedString);
}
@Override
public String toString() {
return doubleQuotedString.replaceAll("\\\\\"", "\"");
}
}
private enum State { READING_KEY, READING_VALUE }
private static final class Parser implements Callable