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-2023, 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, arrayIndenter, objectIndenter; 065 066 public JCardPrettyPrinter() { 067 propertyIndenter = INLINE_INDENTER; 068 indentArraysWith(NEWLINE_INDENTER); 069 indentObjectsWith(NEWLINE_INDENTER); 070 } 071 072 public JCardPrettyPrinter(JCardPrettyPrinter base) { 073 super(base); 074 propertyIndenter = base.propertyIndenter; 075 indentArraysWith(base.arrayIndenter); 076 indentObjectsWith(base.objectIndenter); 077 } 078 079 @Override 080 public JCardPrettyPrinter createInstance() { 081 return new JCardPrettyPrinter(this); 082 } 083 084 @Override 085 public void indentArraysWith(Indenter indenter) { 086 arrayIndenter = indenter; 087 super.indentArraysWith(indenter); 088 } 089 090 @Override 091 public void indentObjectsWith(Indenter indenter) { 092 objectIndenter = indenter; 093 super.indentObjectsWith(indenter); 094 } 095 096 public void indentVCardPropertiesWith(Indenter indenter) { 097 propertyIndenter = indenter; 098 } 099 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}