Skip to content

Commit

Permalink
YamlInput design
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Feb 9, 2017
1 parent 59c157e commit d5508bf
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/main/java/com/amihaiemil/camel/RtYamlInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright (c) 2016-2017, Mihai Emil Andronache
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
package com.amihaiemil.camel;

import java.io.InputStream;
import java.io.InputStreamReader;

/**
* Implementation for {@link YamlInput}.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 1.0.0
* @todo #46:30m Finish implementing this class using a finite automata.
* Short idea: using an InputStreamReader, read each char from the stream,
* see if the automata accepts it. If it's accepted, build the yaml.
*/
final class RtYamlInput implements YamlInput {

/**
* Ctor.
* @param source Given source.
*/
RtYamlInput(final InputStream source) {
new InputStreamReader(source);
}

@Override
public YamlMapping readYamlMapping() {
return null;
}

@Override
public YamlSequence readYamlSequence() {
return null;
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/amihaiemil/camel/Yaml.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
*/
package com.amihaiemil.camel;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
* Yaml.
* @author Mihai Andronache (amihaiemil@gmail.com)
Expand Down Expand Up @@ -57,4 +63,35 @@ public static YamlSequenceBuilder createYamlSequenceBuilder() {
return new RtYamlSequenceBuilder();
}

/**
* Create a {@link YamlInput} from a File.
* @return YamlInput, reader of Yaml.
* @param input File to read from.
* @throws FileNotFoundException If the file is not found.
*/
public static YamlInput createYamlInput(final File input)
throws FileNotFoundException {
return Yaml.createYamlInput(new FileInputStream(input));
}

/**
* Create a {@link YamlInput} from a String.
* @param input String to read from.
* @return YamlInput, reader of Yaml.
*/
public static YamlInput createYamlInput(final String input) {
return Yaml.createYamlInput(
new ByteArrayInputStream(input.getBytes())
);
}

/**
* Create a {@link YamlInput} from an InputStream.
* @param input InputStream to read from.
* @return YamlInput, reader of Yaml.
*/
public static YamlInput createYamlInput(final InputStream input) {
return new RtYamlInput(input);
}

}
50 changes: 50 additions & 0 deletions src/main/java/com/amihaiemil/camel/YamlInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright (c) 2016-2017, Mihai Emil Andronache
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
package com.amihaiemil.camel;

/**
* Yaml input.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 1.0.0
*/
public interface YamlInput {

/**
* Read the given input as a Yaml mapping.
* @return Read YamlMapping.
*/
YamlMapping readYamlMapping();

/**
* Read the given input as a Yaml sequence.
* @return Read YamlSequence.
*/
YamlSequence readYamlSequence();

}
40 changes: 40 additions & 0 deletions src/test/java/com/amihaiemil/camel/YamlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
*/
package com.amihaiemil.camel;

import java.io.ByteArrayInputStream;
import java.io.File;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
Expand Down Expand Up @@ -58,4 +61,41 @@ public void createsYamlSequenceBuilder() {
Yaml.createYamlSequenceBuilder(), Matchers.notNullValue()
);
}

/**
* Yaml can create a YamlInput from a File.
* @throws Exception if something goes wrong
*/
@Test
public void createsYamlInputFromFile() throws Exception {
MatcherAssert.assertThat(
Yaml.createYamlInput(
new File("src/test/resources/simpleMapping.yml")
),
Matchers.notNullValue()
);
}

/**
* Yaml can create a YamlInput from an InputStream.
*/
@Test
public void createsYamlInputFromInputStream() {
MatcherAssert.assertThat(
Yaml.createYamlInput(
new ByteArrayInputStream("yaml: test".getBytes())
),
Matchers.notNullValue()
);
}

/**
* Yaml can create a YamlInput from a String.
*/
@Test
public void createsYamlInputFromString() {
MatcherAssert.assertThat(
Yaml.createYamlInput("yaml: test"), Matchers.notNullValue()
);
}
}

0 comments on commit d5508bf

Please sign in to comment.