Skip to content

Commit

Permalink
Adds the IonTextWriterBuilder_1_1 interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
tgregg committed May 13, 2024
1 parent a4ff2e7 commit 1174837
Show file tree
Hide file tree
Showing 20 changed files with 1,127 additions and 540 deletions.
26 changes: 22 additions & 4 deletions src/main/java/com/amazon/ion/IonEncodingVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
// SPDX-License-Identifier: Apache-2.0
package com.amazon.ion;

import com.amazon.ion.impl._Private_IonTextWriterBuilder_1_1;
import com.amazon.ion.system.IonBinaryWriterBuilder;
import com.amazon.ion.system.IonBinaryWriterBuilder_1_1;
import com.amazon.ion.system.IonTextWriterBuilder;
import com.amazon.ion.system.IonTextWriterBuilder_1_1;
import com.amazon.ion.system._Private_IonBinaryWriterBuilder_1_1;

/**
Expand All @@ -15,30 +18,39 @@
*
* @param <BinaryWriterBuilder> the type of binary writer builder compatible with this version.
*/
// TODO add a parameter for the text writer builder type; add a "textWriterBuilder()" method.
public abstract class IonEncodingVersion<BinaryWriterBuilder> {
public abstract class IonEncodingVersion<BinaryWriterBuilder, TextWriterBuilder> {

/**
* Ion 1.0, see the <a href="https://amazon-ion.github.io/ion-docs/docs/binary.html">binary</a> and
* <a href="https://amazon-ion.github.io/ion-docs/docs/text.html">text</a> specification.
*/
public static IonEncodingVersion<IonBinaryWriterBuilder> ION_1_0 = new IonEncodingVersion<IonBinaryWriterBuilder>(0) {
public static IonEncodingVersion<IonBinaryWriterBuilder, IonTextWriterBuilder> ION_1_0 = new IonEncodingVersion<IonBinaryWriterBuilder, IonTextWriterBuilder>(0) {

@Override
public IonBinaryWriterBuilder binaryWriterBuilder() {
return IonBinaryWriterBuilder.standard();
}

@Override
public IonTextWriterBuilder textWriterBuilder() {
return IonTextWriterBuilder.standard();
}
};

/**
* Ion 1.1, TODO link to the finalized specification.
*/
public static IonEncodingVersion<IonBinaryWriterBuilder_1_1> ION_1_1 = new IonEncodingVersion<IonBinaryWriterBuilder_1_1>(1) {
public static IonEncodingVersion<IonBinaryWriterBuilder_1_1, IonTextWriterBuilder_1_1> ION_1_1 = new IonEncodingVersion<IonBinaryWriterBuilder_1_1, IonTextWriterBuilder_1_1>(1) {

@Override
public IonBinaryWriterBuilder_1_1 binaryWriterBuilder() {
return _Private_IonBinaryWriterBuilder_1_1.standard();
}

@Override
public IonTextWriterBuilder_1_1 textWriterBuilder() {
return _Private_IonTextWriterBuilder_1_1.standard();
}
};

private final int minorVersion;
Expand All @@ -53,6 +65,12 @@ private IonEncodingVersion(int minorVersion) {
*/
public abstract BinaryWriterBuilder binaryWriterBuilder();

/**
* Provides a new mutable text writer builder for IonWriter instances that write this version of the Ion encoding.
* @return a new mutable writer builder.
*/
public abstract TextWriterBuilder textWriterBuilder();

@Override
public String toString() {
return String.format("Ion 1.%d", minorVersion);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/amazon/ion/impl/IonRawTextWriter_1_1.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import java.time.Instant
* - To make it easier to concatenate streams, this eagerly emits a top-level separator after each top-level syntax item.
*/
class IonRawTextWriter_1_1 internal constructor(
private val options: _Private_IonTextWriterBuilder,
private val options: _Private_IonTextWriterBuilder_1_1,
private val output: _Private_IonTextAppender,
) : IonRawWriter_1_1 {

Expand Down
158 changes: 22 additions & 136 deletions src/main/java/com/amazon/ion/impl/_Private_IonTextWriterBuilder.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,24 @@
/*
* Copyright 2007-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package com.amazon.ion.impl;

import static com.amazon.ion.impl._Private_Utils.initialSymtab;

import com.amazon.ion.IonCatalog;
import com.amazon.ion.IonSystem;
import com.amazon.ion.IonWriter;
import com.amazon.ion.SymbolTable;
import com.amazon.ion.system.IonSystemBuilder;
import com.amazon.ion.system.IonTextWriterBuilder;
import com.amazon.ion.system.SimpleCatalog;
import com.amazon.ion.util._Private_FastAppendable;
import java.io.OutputStream;

/**
* Contains configuration common to text writers for all Ion versions.
* NOT FOR APPLICATION USE!
*/
public class _Private_IonTextWriterBuilder
public abstract class _Private_IonTextWriterBuilder<T extends _Private_IonTextWriterBuilder<T>>
extends IonTextWriterBuilder
{
private final static CharSequence SPACE_CHARACTER = " ";

public static _Private_IonTextWriterBuilder standard()
{
return new _Private_IonTextWriterBuilder.Mutable();
}

public static _Private_IonTextWriterBuilder STANDARD =
standard().immutable();


//=========================================================================

private boolean _pretty_print;
public boolean _pretty_print;

// These options control whether the IonTextWriter will write standard ion or ion that is down-converted json.
public boolean _blob_as_string;
Expand All @@ -62,15 +34,15 @@ public static _Private_IonTextWriterBuilder standard()
public boolean _untyped_nulls;
public boolean _allow_invalid_sids;

private _Private_CallbackBuilder _callback_builder;
public _Private_CallbackBuilder _callback_builder;


private _Private_IonTextWriterBuilder()
_Private_IonTextWriterBuilder()
{
super();
}

private _Private_IonTextWriterBuilder(_Private_IonTextWriterBuilder that)
_Private_IonTextWriterBuilder(T that)
{
super(that);
this._callback_builder = that._callback_builder ;
Expand All @@ -89,40 +61,35 @@ private _Private_IonTextWriterBuilder(_Private_IonTextWriterBuilder that)
this._allow_invalid_sids = that._allow_invalid_sids ;
}


@Override
public final _Private_IonTextWriterBuilder copy()
{
return new Mutable(this);
}
public abstract T copy();

@Override
public _Private_IonTextWriterBuilder immutable()
public T immutable()
{
return this;
return (T) this;
}

@Override
public _Private_IonTextWriterBuilder mutable()
public T mutable()
{
return copy();
}


//=========================================================================

@Override
public final IonTextWriterBuilder withPrettyPrinting()
public IonTextWriterBuilder withPrettyPrinting()
{
_Private_IonTextWriterBuilder b = mutable();
T b = mutable();
b._pretty_print = true;
return b;
}

@Override
public final IonTextWriterBuilder withJsonDowngrade()
public IonTextWriterBuilder withJsonDowngrade()
{
_Private_IonTextWriterBuilder b = mutable();
T b = mutable();

b.withMinimalSystemData();

Expand Down Expand Up @@ -150,8 +117,8 @@ public final IonTextWriterBuilder withJsonDowngrade()
* @param allowInvalidSids whether to allow invalid SIDs.
* @return the builder.
*/
public final _Private_IonTextWriterBuilder withInvalidSidsAllowed(boolean allowInvalidSids) {
_Private_IonTextWriterBuilder b = mutable();
public final T withInvalidSidsAllowed(boolean allowInvalidSids) {
T b = mutable();
b._allow_invalid_sids = allowInvalidSids;
return b;
}
Expand Down Expand Up @@ -179,7 +146,7 @@ final CharSequence topLevelSeparator()

//=========================================================================

private _Private_IonTextWriterBuilder fillDefaults()
T fillDefaults()
{
// Ensure that we don't modify the user's builder.
IonTextWriterBuilder b = copy();
Expand All @@ -206,87 +173,7 @@ private _Private_IonTextWriterBuilder fillDefaults()
));
}

return (_Private_IonTextWriterBuilder) b.immutable();
}


/** Assumes that {@link #fillDefaults()} has been called. */
private IonWriter build(_Private_FastAppendable appender)
{
IonCatalog catalog = getCatalog();
SymbolTable[] imports = getImports();

// TODO We shouldn't need a system here
IonSystem system =
IonSystemBuilder.standard().withCatalog(catalog).build();

SymbolTable defaultSystemSymtab = system.getSystemSymbolTable();

IonWriterSystemText systemWriter =
(getCallbackBuilder() == null
? new IonWriterSystemText(defaultSystemSymtab,
this,
appender)
: new IonWriterSystemTextMarkup(defaultSystemSymtab,
this,
appender));

SymbolTable initialSymtab =
initialSymtab(((_Private_ValueFactory)system).getLstFactory(), defaultSystemSymtab, imports);

return new IonWriterUser(catalog, system, systemWriter, initialSymtab, !_allow_invalid_sids);
}


@Override
public final IonWriter build(Appendable out)
{
_Private_IonTextWriterBuilder b = fillDefaults();

_Private_FastAppendable fast = new AppendableFastAppendable(out);

return b.build(fast);
}


@Override
public final IonWriter build(OutputStream out)
{
_Private_IonTextWriterBuilder b = fillDefaults();

_Private_FastAppendable fast = new OutputStreamFastAppendable(out);

return b.build(fast);
}

//=========================================================================

private static final class Mutable
extends _Private_IonTextWriterBuilder
{
private Mutable() { }

private Mutable(_Private_IonTextWriterBuilder that)
{
super(that);
}

@Override
public _Private_IonTextWriterBuilder immutable()
{
return new _Private_IonTextWriterBuilder(this);
}

@Override
public _Private_IonTextWriterBuilder mutable()
{
return this;
}

@Override
protected void mutationCheck()
{
}
return (T) b.immutable();
}

//-------------------------------------------------------------------------
Expand Down Expand Up @@ -328,10 +215,9 @@ public void setCallbackBuilder(_Private_CallbackBuilder builder)
* @see #getCallbackBuilder()
* @see #setCallbackBuilder(_Private_CallbackBuilder)
*/
public final _Private_IonTextWriterBuilder
withCallbackBuilder(_Private_CallbackBuilder builder)
public final T withCallbackBuilder(_Private_CallbackBuilder builder)
{
_Private_IonTextWriterBuilder b = mutable();
T b = mutable();
b.setCallbackBuilder(builder);
return b;
}
Expand Down
Loading

0 comments on commit 1174837

Please sign in to comment.