Skip to content
This repository has been archived by the owner on Jan 4, 2020. It is now read-only.

Commit

Permalink
added characterArraySource
Browse files Browse the repository at this point in the history
  • Loading branch information
Torsten Krause committed Mar 15, 2016
1 parent b26abfc commit bfabd8a
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright (c) 2016 Torsten Krause, Markenwerk GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package net.markenwerk.utils.json.parser;

import java.io.IOException;

/**
* A {@link CharacterArraySource} is a {@link JsonSource} that is backed by a
* given {@code char[]}.
*
* @author Torsten Krause (tk at markenwerk dot net)
* @since 1.0.0
*/
public final class CharacterArraySource implements JsonSource {

private char[] characters;

private int position;

private int line = 1;

private int column = 1;

private int lastNewLinePosition;

/**
* Creates a new {@link CharacterArraySource} for the given {@link String}.
*
* @param characters
* The {@code char[]} to be used.
* @throws IllegalArgumentException
* If the given {@link String} is {@literal null}.
*/
public CharacterArraySource(char[] characters) throws IllegalArgumentException {
if (null == characters) {
throw new IllegalArgumentException("characters is null");
}
this.characters = characters;
if (0 != characters.length && JsonSource.BYTE_ORDER_MARK == characters[0]) {
position++;
column++;
}
}

@Override
public int getAvailable() {
return characters.length - position;
}

@Override
public boolean makeAvailable(int minimum) throws IOException {
return position + minimum <= characters.length;
}

@Override
public char nextCharacter() {
char result = characters[position++];
if ('\n' == result) {
lastNewLinePosition = position;
column = 1;
line += 1;
}
return result;
}

@Override
public char peekCharacter(int offset) {
return characters[position + offset];
}

@Override
public String nextString(int length) {
String string = new String(characters, position, length);
for (int i = 0; i < length; i++) {
nextCharacter();
}
return string;
}

@Override
public void appendNextString(StringBuilder builder, int length) {
builder.append(characters, position, length);
for (int i = 0; i < length; i++) {
nextCharacter();
}
}

@Override
public String getPast(int maximum) {
if (0 == position) {
return "";
} else {
int availableLength = Math.min(maximum, position);
return new String(characters, position - availableLength, availableLength);
}
}

@Override
public String getFuture(int maximum) {
int availableLength = Math.min(maximum, characters.length - position);
return new String(characters, position, availableLength);
}

@Override
public int getLine() {
return line;
}

@Override
public int getColumn() {
return column + (position - lastNewLinePosition);
}

@Override
public void close() throws IOException {
}

}
12 changes: 12 additions & 0 deletions src/main/java/net/markenwerk/utils/json/parser/JsonParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ public JsonParser(String string) throws IllegalArgumentException {
this(new StringSource(string));
}

/**
* Creates a new {@link JsonParser} for the given {@code char[]}.
*
* @param characters
* The {@code char[]} to read from.
* @throws IllegalArgumentException
* If the given {@code char[]} is {@literal null}.
*/
public JsonParser(char[] characters) throws IllegalArgumentException {
this(new CharacterArraySource(characters));
}

/**
* Creates a new {@link JsonParser} for the given {@link Reader}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,24 @@ public void emptyArray() throws JsonSyntaxException, IOException {
}
}

@Test
@SuppressWarnings("javadoc")
public void emptyArray_afterBom() throws JsonSyntaxException, IOException {
JsonParser jsonReader = new JsonParser(getSource(JsonSource.BYTE_ORDER_MARK + "[]"));
try {

Assert.assertEquals(JsonState.ARRAY_BEGIN, jsonReader.currentState());
jsonReader.beginArray();
Assert.assertEquals(JsonState.ARRAY_END, jsonReader.currentState());
jsonReader.endArray();
Assert.assertEquals(JsonState.DOCUMENT_END, jsonReader.currentState());
jsonReader.endDocumnet();

} finally {
jsonReader.close();
}
}

@SuppressWarnings("javadoc")
@Test(expected = JsonSyntaxException.class)
public void emptyArray_unfinished() throws JsonSyntaxException, IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package net.markenwerk.utils.json.parser;

import org.junit.Test;

/**
* JUnit test for {@link JsonParser} with an underlying
* {@link CharacterArraySource}.
*
* @author Torsten Krause (tk at markenwerk dot net)
*/
public class CharacterArrayJsonParserTests extends AbstractJsonParserTests {

@SuppressWarnings({ "resource", "javadoc" })
@Test(expected = IllegalArgumentException.class)
public void create_nullString() {
new JsonParser((char[]) null);
}

@Override
protected JsonSource getSource(String string) {
return new CharacterArraySource(string.toCharArray());
}

}

0 comments on commit bfabd8a

Please sign in to comment.