001package ezvcard.io.json; 002 003import java.io.IOException; 004 005import com.fasterxml.jackson.core.JsonGenerationException; 006import com.fasterxml.jackson.core.JsonGenerator; 007import com.fasterxml.jackson.core.JsonStreamContext; 008import com.fasterxml.jackson.core.util.DefaultIndenter; 009import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; 010 011/* 012 Copyright (c) 2012-2026, Michael Angstadt 013 All rights reserved. 014 015 Redistribution and use in source and binary forms, with or without 016 modification, are permitted provided that the following conditions are met: 017 018 1. Redistributions of source code must retain the above copyright notice, this 019 list of conditions and the following disclaimer. 020 2. Redistributions in binary form must reproduce the above copyright notice, 021 this list of conditions and the following disclaimer in the documentation 022 and/or other materials provided with the distribution. 023 024 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 025 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 026 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 027 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 028 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 029 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 030 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 031 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 032 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 033 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 034 035 The views and conclusions contained in the software and documentation are those 036 of the authors and should not be interpreted as representing official policies, 037 either expressed or implied, of the FreeBSD Project. 038 */ 039 040/** 041 * A JSON pretty-printer for jCards. 042 * @author Buddy Gorven 043 * @author Michael Angstadt 044 */ 045public class JCardPrettyPrinter extends DefaultPrettyPrinter { 046 private static final long serialVersionUID = 1L; 047 048 /** 049 * The value that is assigned to {@link JsonGenerator#setCurrentValue} to 050 * let the pretty-printer know that a vCard property is currently being 051 * written. 052 */ 053 public static final Object PROPERTY_VALUE = "vcard-property"; 054 055 /** 056 * Alias for {@link DefaultIndenter#SYSTEM_LINEFEED_INSTANCE} 057 */ 058 private static final Indenter NEWLINE_INDENTER = DefaultIndenter.SYSTEM_LINEFEED_INSTANCE; 059 /** 060 * Instance of {@link DefaultPrettyPrinter.FixedSpaceIndenter} 061 */ 062 private static final Indenter INLINE_INDENTER = new DefaultPrettyPrinter.FixedSpaceIndenter(); 063 064 private Indenter propertyIndenter; 065 private Indenter arrayIndenter; 066 private Indenter objectIndenter; 067 068 public JCardPrettyPrinter() { 069 propertyIndenter = INLINE_INDENTER; 070 indentArraysWith(NEWLINE_INDENTER); 071 indentObjectsWith(NEWLINE_INDENTER); 072 } 073 074 public JCardPrettyPrinter(JCardPrettyPrinter base) { 075 super(base); 076 propertyIndenter = base.propertyIndenter; 077 indentArraysWith(base.arrayIndenter); 078 indentObjectsWith(base.objectIndenter); 079 } 080 081 @Override 082 public JCardPrettyPrinter createInstance() { 083 return new JCardPrettyPrinter(this); 084 } 085 086 @Override 087 public void indentArraysWith(Indenter indenter) { 088 arrayIndenter = indenter; 089 super.indentArraysWith(indenter); 090 } 091 092 @Override 093 public void indentObjectsWith(Indenter indenter) { 094 objectIndenter = indenter; 095 super.indentObjectsWith(indenter); 096 } 097 098 public void indentVCardPropertiesWith(Indenter indenter) { 099 propertyIndenter = indenter; 100 } 101 102 protected static boolean isInVCardProperty(JsonStreamContext context) { 103 if (context == null) { 104 return false; 105 } 106 107 Object currentValue = context.getCurrentValue(); 108 if (currentValue == PROPERTY_VALUE) { 109 return true; 110 } 111 112 return isInVCardProperty(context.getParent()); 113 } 114 115 private void updateIndenter(JsonStreamContext context) { 116 boolean inVCardProperty = isInVCardProperty(context); 117 super.indentArraysWith(inVCardProperty ? propertyIndenter : arrayIndenter); 118 super.indentObjectsWith(inVCardProperty ? propertyIndenter : objectIndenter); 119 } 120 121 @Override 122 public void writeStartArray(JsonGenerator gen) throws IOException, JsonGenerationException { 123 updateIndenter(gen.getOutputContext().getParent()); 124 super.writeStartArray(gen); 125 } 126 127 @Override 128 public void writeEndArray(JsonGenerator gen, int numValues) throws IOException, JsonGenerationException { 129 updateIndenter(gen.getOutputContext().getParent()); 130 super.writeEndArray(gen, numValues); 131 } 132 133 @Override 134 public void writeArrayValueSeparator(JsonGenerator gen) throws IOException { 135 updateIndenter(gen.getOutputContext().getParent()); 136 super.writeArrayValueSeparator(gen); 137 } 138}