com.anrisoftware.globalpom.initfileparser.internal.SectionFormatterImpl Maven / Gradle / Ivy
/*
* Copyright 2016 Erwin Müller
*
* Licensed 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 com.anrisoftware.globalpom.initfileparser.internal;
import java.io.IOException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.inject.Inject;
import com.anrisoftware.globalpom.initfileparser.external.InitFileAttributes;
import com.anrisoftware.globalpom.initfileparser.external.Section;
import com.anrisoftware.globalpom.initfileparser.external.SectionFormatter;
import com.anrisoftware.globalpom.initfileparser.external.SectionFormatterFactory;
import com.google.inject.assistedinject.Assisted;
/**
* Implements the INI file section formatter.
*
* @author Erwin Mueller, [email protected]
* @since 1.0
*/
class SectionFormatterImpl implements SectionFormatter {
private static final String SPACE = " ";
private static final Pattern QUOTE_MATCH_PATTERN = Pattern
.compile("[\\s\\p{Punct}]");
private final char delimiter;
private final char openSection;
private final char closeSection;
private final boolean whitespaceBetweenPropertyDelimiter;
private final String newLine;
private final String mark;
private final char stringQuote;
private final boolean stringQuoteEnabled;
/**
* @see SectionFormatterFactory#create(InitFileAttributes)
*/
@Inject
SectionFormatterImpl(@Assisted InitFileAttributes attributes) {
this.delimiter = attributes.getPropertyDelimiter();
this.openSection = attributes.getSectionBrackets()[0];
this.closeSection = attributes.getSectionBrackets()[1];
this.mark = attributes.getMultiValueMark();
this.whitespaceBetweenPropertyDelimiter = attributes
.isWhitespaceBetweenPropertyDelimiter();
this.newLine = attributes.getNewLine();
this.stringQuote = attributes.getStringQuote();
this.stringQuoteEnabled = attributes.isStringQuoteEnabled();
}
@Override
public String format(Section section) {
StringBuilder builder = new StringBuilder();
return format(section, builder).toString();
}
@Override
public StringBuilder format(Section section, StringBuilder builder) {
try {
return (StringBuilder) format0(section, builder);
} catch (IOException e) {
return null;
}
}
@Override
public StringBuffer format(Section section, StringBuffer builder) {
try {
return (StringBuffer) format0(section, builder);
} catch (IOException e) {
return null;
}
}
private Appendable format0(Section section, Appendable b)
throws IOException {
b.append(openSection).append(section.getName()).append(closeSection);
b.append(newLine);
for (Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy