001/*
002 * Copyright 2007-2018 The jdeb developers.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.vafer.jdeb.debian;
018
019import java.io.BufferedReader;
020import java.io.IOException;
021import java.io.StringReader;
022
023/**
024 * A field of a control file. This class is immutable.
025 */
026public class ControlField {
027
028    /**
029     * The format of a field.
030     */
031    public enum Type {
032        /** Value on a single line */
033        SIMPLE,
034        /** Value on multiple lines, space characters are ignored */
035        FOLDED,
036        /** Value on multiple lines, space characters are preserved */
037        MULTILINE
038    }
039
040    /** The name of the field */
041    private String name;
042
043    /** Tells if the field is mandatory */
044    private boolean mandatory;
045
046    /** The type of the field */
047    private Type type = Type.SIMPLE;
048
049    /** Tells is the first line of the field must be empty (for MULTILINE values only) */
050    private boolean firstLineEmpty;
051
052
053    public ControlField(String name) {
054        this(name, false);
055    }
056
057    public ControlField(String name, boolean mandatory) {
058        this(name, mandatory, Type.SIMPLE);
059    }
060
061    public ControlField(String name, boolean mandatory, Type type) {
062        this(name, mandatory, type, false);
063    }
064
065    public ControlField(String name, boolean mandatory, Type type, boolean firstLineEmpty) {
066        this.name = name;
067        this.mandatory = mandatory;
068        this.type = type;
069        this.firstLineEmpty = firstLineEmpty;
070    }
071
072    public String getName() {
073        return name;
074    }
075
076    public boolean isMandatory() {
077        return mandatory;
078    }
079
080    public Type getType() {
081        return type;
082    }
083
084    public boolean isFirstLineEmpty() {
085        return firstLineEmpty;
086    }
087
088    /**
089     * Returns the field with the specified value properly formatted. Multiline
090     * values are automatically indented, and dots are added on the empty lines.
091     *
092     * <pre>
093     * Field-Name: value
094     * </pre>
095     */
096    public String format(String value) {
097        StringBuilder s = new StringBuilder();
098
099        if (value != null && value.trim().length() > 0) {
100            boolean continuationLine = false;
101
102            s.append(getName()).append(":");
103            if (isFirstLineEmpty()) {
104                s.append("\n");
105                continuationLine = true;
106            }
107
108            try {
109                BufferedReader reader = new BufferedReader(new StringReader(value));
110                String line;
111                while ((line = reader.readLine()) != null) {
112                    if (continuationLine && line.trim().length() == 0) {
113                        // put a dot on the empty continuation lines
114                        s.append(" .\n");
115                    } else {
116                        s.append(" ").append(line).append("\n");
117                    }
118
119                    continuationLine = true;
120                }
121            } catch (IOException e) {
122                e.printStackTrace();
123            }
124        }
125
126        return s.toString();
127    }
128}