| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| AbstractFormatter |
|
| 3.125;3.125 |
| 1 | /* | |
| 2 | * Copyright 2005,2009 Ivan SZKIBA | |
| 3 | * | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | * See the License for the specific language governing permissions and | |
| 14 | * limitations under the License. | |
| 15 | */ | |
| 16 | package org.ini4j.spi; | |
| 17 | ||
| 18 | import org.ini4j.Config; | |
| 19 | ||
| 20 | import java.io.PrintWriter; | |
| 21 | ||
| 22 | 32 | abstract class AbstractFormatter implements HandlerBase |
| 23 | { | |
| 24 | private static final char OPERATOR = '='; | |
| 25 | private static final char COMMENT = '#'; | |
| 26 | private static final char SPACE = ' '; | |
| 27 | 32 | private Config _config = Config.getGlobal(); |
| 28 | 32 | private boolean _header = true; |
| 29 | private PrintWriter _output; | |
| 30 | ||
| 31 | @Override public void handleComment(String comment) | |
| 32 | { | |
| 33 | 498 | if (getConfig().isComment() && (!_header || getConfig().isHeaderComment()) && (comment != null) && (comment.length() != 0)) |
| 34 | { | |
| 35 | 259 | for (String line : comment.split(getConfig().getLineSeparator())) |
| 36 | { | |
| 37 | 192 | getOutput().print(COMMENT); |
| 38 | 192 | getOutput().print(line); |
| 39 | 192 | getOutput().print(getConfig().getLineSeparator()); |
| 40 | } | |
| 41 | ||
| 42 | 67 | if (_header) |
| 43 | { | |
| 44 | 8 | getOutput().print(getConfig().getLineSeparator()); |
| 45 | } | |
| 46 | } | |
| 47 | ||
| 48 | 498 | _header = false; |
| 49 | 498 | } |
| 50 | ||
| 51 | @Override public void handleOption(String optionName, String optionValue) | |
| 52 | { | |
| 53 | 545 | if (getConfig().isStrictOperator()) |
| 54 | { | |
| 55 | 94 | if (getConfig().isEmptyOption() || (optionValue != null)) |
| 56 | { | |
| 57 | 93 | getOutput().print(escapeFilter(optionName)); |
| 58 | 93 | getOutput().print(OPERATOR); |
| 59 | } | |
| 60 | ||
| 61 | 94 | if (optionValue != null) |
| 62 | { | |
| 63 | 92 | getOutput().print(escapeFilter(optionValue)); |
| 64 | } | |
| 65 | ||
| 66 | 94 | if (getConfig().isEmptyOption() || (optionValue != null)) |
| 67 | { | |
| 68 | 93 | getOutput().print(getConfig().getLineSeparator()); |
| 69 | } | |
| 70 | } | |
| 71 | else | |
| 72 | { | |
| 73 | 451 | String value = ((optionValue == null) && getConfig().isEmptyOption()) ? "" : optionValue; |
| 74 | ||
| 75 | 451 | if (value != null) |
| 76 | { | |
| 77 | 450 | getOutput().print(escapeFilter(optionName)); |
| 78 | 450 | getOutput().print(SPACE); |
| 79 | 450 | getOutput().print(OPERATOR); |
| 80 | 450 | getOutput().print(SPACE); |
| 81 | 450 | getOutput().print(escapeFilter(value)); |
| 82 | 450 | getOutput().print(getConfig().getLineSeparator()); |
| 83 | } | |
| 84 | } | |
| 85 | ||
| 86 | 545 | setHeader(false); |
| 87 | 545 | } |
| 88 | ||
| 89 | protected Config getConfig() | |
| 90 | { | |
| 91 | 3591 | return _config; |
| 92 | } | |
| 93 | ||
| 94 | protected void setConfig(Config value) | |
| 95 | { | |
| 96 | 31 | _config = value; |
| 97 | 31 | } |
| 98 | ||
| 99 | protected PrintWriter getOutput() | |
| 100 | { | |
| 101 | 4236 | return _output; |
| 102 | } | |
| 103 | ||
| 104 | protected void setOutput(PrintWriter value) | |
| 105 | { | |
| 106 | 32 | _output = value; |
| 107 | 32 | } |
| 108 | ||
| 109 | void setHeader(boolean value) | |
| 110 | { | |
| 111 | 654 | _header = value; |
| 112 | 654 | } |
| 113 | ||
| 114 | String escapeFilter(String input) | |
| 115 | { | |
| 116 | 1194 | return getConfig().isEscape() ? EscapeTool.getInstance().escape(input) : input; |
| 117 | } | |
| 118 | } |