Debian Codegraph @

src/main/java/ezvcard/io/json/JCardPrettyPrinter.java

raw symbols SCIP
1 package ezvcard.io.json;
2
3 import java.io.IOException;
4
5 import com.fasterxml.jackson.core.JsonGenerationException;
6 import com.fasterxml.jackson.core.JsonGenerator;
7 import com.fasterxml.jackson.core.JsonStreamContext;
8 import com.fasterxml.jackson.core.util.DefaultIndenter;
9 import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
10
11 /*
12 Copyright (c) 2012-2023, Michael Angstadt
13 All rights reserved.
14
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions are met:
17
18 1. Redistributions of source code must retain the above copyright notice, this
19 list of conditions and the following disclaimer.
20 2. Redistributions in binary form must reproduce the above copyright notice,
21 this list of conditions and the following disclaimer in the documentation
22 and/or other materials provided with the distribution.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
25 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
28 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
35 The views and conclusions contained in the software and documentation are those
36 of the authors and should not be interpreted as representing official policies,
37 either expressed or implied, of the FreeBSD Project.
38 */
39
40 /**
41 * A JSON pretty-printer for jCards.
42 * @author Buddy Gorven
43 * @author Michael Angstadt
44 */
45 public class JCardPrettyPrinter extends DefaultPrettyPrinter {
46 private static final long serialVersionUID = 1L;
47
48 /**
49 * The value that is assigned to {@link JsonGenerator#setCurrentValue} to
50 * let the pretty-printer know that a vCard property is currently being
51 * written.
52 */
53 public static final Object PROPERTY_VALUE = "vcard-property";
54
55 /**
56 * Alias for {@link DefaultIndenter#SYSTEM_LINEFEED_INSTANCE}
57 */
58 private static final Indenter NEWLINE_INDENTER = DefaultIndenter.SYSTEM_LINEFEED_INSTANCE;
59 /**
60 * Instance of {@link DefaultPrettyPrinter.FixedSpaceIndenter}
61 */
62 private static final Indenter INLINE_INDENTER = new DefaultPrettyPrinter.FixedSpaceIndenter();
63
64 private Indenter propertyIndenter, arrayIndenter, objectIndenter;
65
66 public JCardPrettyPrinter() {
67 propertyIndenter = INLINE_INDENTER;
68 indentArraysWith(NEWLINE_INDENTER);
69 indentObjectsWith(NEWLINE_INDENTER);
70 }
71
72 public JCardPrettyPrinter(JCardPrettyPrinter base) {
73 super(base);
74 propertyIndenter = base.propertyIndenter;
75 indentArraysWith(base.arrayIndenter);
76 indentObjectsWith(base.objectIndenter);
77 }
78
79 @Override
80 public JCardPrettyPrinter createInstance() {
81 return new JCardPrettyPrinter(this);
82 }
83
84 @Override
85 public void indentArraysWith(Indenter indenter) {
86 arrayIndenter = indenter;
87 super.indentArraysWith(indenter);
88 }
89
90 @Override
91 public void indentObjectsWith(Indenter indenter) {
92 objectIndenter = indenter;
93 super.indentObjectsWith(indenter);
94 }
95
96 public void indentVCardPropertiesWith(Indenter indenter) {
97 propertyIndenter = indenter;
98 }
99
100 protected static boolean isInVCardProperty(JsonStreamContext context) {
101 if (context == null) {
102 return false;
103 }
104
105 Object currentValue = context.getCurrentValue();
106 if (currentValue == PROPERTY_VALUE) {
107 return true;
108 }
109
110 return isInVCardProperty(context.getParent());
111 }
112
113 private void updateIndenter(JsonStreamContext context) {
114 boolean inVCardProperty = isInVCardProperty(context);
115 super.indentArraysWith(inVCardProperty ? propertyIndenter : arrayIndenter);
116 super.indentObjectsWith(inVCardProperty ? propertyIndenter : objectIndenter);
117 }
118
119 @Override
120 public void writeStartArray(JsonGenerator gen) throws IOException, JsonGenerationException {
121 updateIndenter(gen.getOutputContext().getParent());
122 super.writeStartArray(gen);
123 }
124
125 @Override
126 public void writeEndArray(JsonGenerator gen, int numValues) throws IOException, JsonGenerationException {
127 updateIndenter(gen.getOutputContext().getParent());
128 super.writeEndArray(gen, numValues);
129 }
130
131 @Override
132 public void writeArrayValueSeparator(JsonGenerator gen) throws IOException {
133 updateIndenter(gen.getOutputContext().getParent());
134 super.writeArrayValueSeparator(gen);
135 }
136 }