|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright 2015 Real Logic Ltd. | 
|  | 3 | + * | 
|  | 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | + * you may not use this file except in compliance with the License. | 
|  | 6 | + * You may obtain a copy of the License at | 
|  | 7 | + * | 
|  | 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | + * | 
|  | 10 | + * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | + * See the License for the specific language governing permissions and | 
|  | 14 | + * limitations under the License. | 
|  | 15 | + */ | 
|  | 16 | +package uk.co.real_logic.sbe.json; | 
|  | 17 | + | 
|  | 18 | +import uk.co.real_logic.agrona.concurrent.UnsafeBuffer; | 
|  | 19 | +import uk.co.real_logic.sbe.ir.Ir; | 
|  | 20 | +import uk.co.real_logic.sbe.ir.Token; | 
|  | 21 | +import uk.co.real_logic.sbe.otf.OtfHeaderDecoder; | 
|  | 22 | +import uk.co.real_logic.sbe.otf.OtfMessageDecoder; | 
|  | 23 | + | 
|  | 24 | +import java.nio.ByteBuffer; | 
|  | 25 | +import java.util.List; | 
|  | 26 | + | 
|  | 27 | +/** | 
|  | 28 | + * . | 
|  | 29 | + */ | 
|  | 30 | +public class JsonPrinter { | 
|  | 31 | + | 
|  | 32 | + private final Ir ir; | 
|  | 33 | + | 
|  | 34 | + public JsonPrinter(final Ir ir) | 
|  | 35 | + { | 
|  | 36 | + this.ir = ir; | 
|  | 37 | + } | 
|  | 38 | + | 
|  | 39 | + public void print(final ByteBuffer encodedMessage, final StringBuilder output) | 
|  | 40 | + { | 
|  | 41 | + // From the IR we can create OTF decoder for message headers. | 
|  | 42 | + final OtfHeaderDecoder headerDecoder = new OtfHeaderDecoder(ir.headerStructure()); | 
|  | 43 | + | 
|  | 44 | + // Now we have IR we can read the message header | 
|  | 45 | + int bufferOffset = 0; | 
|  | 46 | + final UnsafeBuffer buffer = new UnsafeBuffer(encodedMessage); | 
|  | 47 | + | 
|  | 48 | + final int templateId = headerDecoder.getTemplateId(buffer, bufferOffset); | 
|  | 49 | + final int schemaId = headerDecoder.getSchemaId(buffer, bufferOffset); | 
|  | 50 | + final int actingVersion = headerDecoder.getSchemaVersion(buffer, bufferOffset); | 
|  | 51 | + final int blockLength = headerDecoder.getBlockLength(buffer, bufferOffset); | 
|  | 52 | + | 
|  | 53 | + validateId(schemaId); | 
|  | 54 | + validateVersion(schemaId, actingVersion); | 
|  | 55 | + | 
|  | 56 | + bufferOffset += headerDecoder.size(); | 
|  | 57 | + | 
|  | 58 | + final List<Token> msgTokens = ir.getMessage(templateId); | 
|  | 59 | + | 
|  | 60 | + bufferOffset = OtfMessageDecoder.decode( | 
|  | 61 | + buffer, | 
|  | 62 | + bufferOffset, | 
|  | 63 | + actingVersion, | 
|  | 64 | + blockLength, | 
|  | 65 | + msgTokens, | 
|  | 66 | + new JsonTokenListener(output)); | 
|  | 67 | + | 
|  | 68 | + if (bufferOffset != encodedMessage.position()) | 
|  | 69 | + { | 
|  | 70 | + throw new IllegalStateException("Message not fully decoded"); | 
|  | 71 | + } | 
|  | 72 | + } | 
|  | 73 | + | 
|  | 74 | + private void validateId(int schemaId) | 
|  | 75 | + { | 
|  | 76 | + if (schemaId != ir.id()) | 
|  | 77 | + { | 
|  | 78 | + throw new IllegalArgumentException( | 
|  | 79 | + String.format("Required schema id %d but was actually %d", ir.id(), schemaId)); | 
|  | 80 | + } | 
|  | 81 | + } | 
|  | 82 | + | 
|  | 83 | + private void validateVersion(int schemaId, int actingVersion) | 
|  | 84 | + { | 
|  | 85 | + if (actingVersion > ir.version()) | 
|  | 86 | + { | 
|  | 87 | + throw new IllegalArgumentException( | 
|  | 88 | + String.format("Required schema id %d but was actually %d", ir.id(), schemaId)); | 
|  | 89 | + } | 
|  | 90 | + } | 
|  | 91 | + | 
|  | 92 | + public String print(final ByteBuffer encodedMessage) | 
|  | 93 | + { | 
|  | 94 | + StringBuilder sb = new StringBuilder(); | 
|  | 95 | + print(encodedMessage, sb); | 
|  | 96 | + return sb.toString(); | 
|  | 97 | + } | 
|  | 98 | + | 
|  | 99 | +} | 
0 commit comments