| 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 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class JCardPrettyPrinter extends DefaultPrettyPrinter { |
| 46 |
private static final long serialVersionUID = 1L; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public static final Object PROPERTY_VALUE = "vcard-property"; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
private static final Indenter NEWLINE_INDENTER = DefaultIndenter.SYSTEM_LINEFEED_INSTANCE; |
| 59 |
|
| 60 |
|
| 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 |
} |