Skip to content
This repository has been archived by the owner on Oct 13, 2019. It is now read-only.

Commit

Permalink
Complete code generation for model and parser
Browse files Browse the repository at this point in the history
Still only RSS and only certain fields, but already a big step in the
right direction.

Closes #13
  • Loading branch information
mishako committed Mar 22, 2019
1 parent 227f607 commit 75ee17b
Show file tree
Hide file tree
Showing 60 changed files with 2,345 additions and 1,017 deletions.
Empty file modified README.md
100644 → 100755
Empty file.
5 changes: 2 additions & 3 deletions throwaway-prototype/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.PHONY: build test model

MAVEN = $(shell mvn --version >/dev/null 2>&1 && echo "mvn")
CURRENT_DIRECTORY = $(shell pwd)

ifeq ($(MAVEN),)
$(error "Please install maven")
Expand All @@ -15,5 +14,5 @@ build:
test:
$(MAVEN) test

model:
$(MAVEN) compile exec:java -pl model -Dexec.args="$(CURRENT_DIRECTORY)" -Dexec.mainClass="com.rometools.rome.model.Main"
generate:
$(MAVEN) package -am -pl factory
16 changes: 16 additions & 0 deletions throwaway-prototype/common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.rometools.rome2.prototype</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>

<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.rometools.rome.common;

public enum OneOrMany {
ONE,
MANY;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.rometools.rome.common.parser;

import com.rometools.rome.common.value.IntValue;

public class IntParser implements Parser<IntValue> {

@Override
public Class<IntValue> getResultClass() {
return IntValue.class;
}

@Override
public IntValue parse(String input) {
return IntValue.ofNullable(Integer.valueOf(input));
}

@Override
public IntValue none() {
return IntValue.none();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.rometools.rome.common.parser;

public interface Parser<T> {

Class<T> getResultClass();

T parse(String input);

T none();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.rometools.rome.common.parser;

import com.rometools.rome.common.value.StringValue;

public class StringParser implements Parser<StringValue> {

@Override
public Class<StringValue> getResultClass() {
return StringValue.class;
}

@Override
public StringValue parse(String input) {
return StringValue.ofNullable(input);
}

@Override
public StringValue none() {
return StringValue.none();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.rometools.rome.model.value;
package com.rometools.rome.common.value;

import java.util.Optional;

Expand Down
20 changes: 9 additions & 11 deletions ...tools/rome/model/value/DateTimeValue.java → ...ools/rome/common/value/DateTimeValue.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
package com.rometools.rome.model.value;
package com.rometools.rome.common.value;

import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.stream.Collectors;

public class DateTimeValue extends AbstractValue<ZonedDateTime> {

protected DateTimeValue(ZonedDateTime value) {
super(value);
}

public static DateTimeValue ofNullable(ZonedDateTime value) {
public static DateTimeValue of(ZonedDateTime value) {
if (value == null) {
throw new IllegalArgumentException();
}
return new DateTimeValue(value);
}

public static DateTimeValue empty() {
return ofNullable(null);
public static DateTimeValue ofNullable(ZonedDateTime value) {
return new DateTimeValue(value);
}

public static ArrayList<DateTimeValue> mapListOfNullables(ArrayList<ZonedDateTime> values) {
return new ArrayList<>(
values.stream()
.map(DateTimeValue::ofNullable)
.collect(Collectors.toList()));
public static DateTimeValue none() {
return ofNullable(null);
}

public ZonedDateTime defaultToEpoch() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.rometools.rome.common.value;

import java.util.Comparator;
import java.util.Objects;

public class IntValue extends AbstractValue<Integer> implements Comparable<IntValue> {

private IntValue(Integer value) {
super(value);
}

public static IntValue of(int value) {
return new IntValue(value);
}

public static IntValue ofNullable(Integer value) {
return new IntValue(value);
}

public static IntValue none() {
return ofNullable(null);
}

public Integer defaultToZero() {
return orElse(0);
}

@Override
public int compareTo(IntValue other) {
return Objects.compare(this, other, Comparator.comparing(IntValue::asNullable));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.rometools.rome.common.value;

public class StringValue extends AbstractValue<String> {

protected StringValue(String value) {
super(value);
}

public static StringValue of(String value) {
if (value == null) {
throw new IllegalArgumentException();
}
return new StringValue(value);
}

public static StringValue ofNullable(String value) {
return new StringValue(value);
}

public static StringValue none() {
return ofNullable(null);
}

public String defaultToEmpty() {
return orElse("");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.rometools.rome.common.value;

public class Value<T> extends AbstractValue<T> {

protected Value(T value) {
super(value);
}

public static <T> Value<T> ofNullable(T value) {
return new Value<>(value);
}

public static <T> Value<T> none() {
return ofNullable(null);
}

public T defaultToNull() {
return orElse(null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.rometools.rome.common.xml;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

public class XmlPath {

public static final XmlPath ROOT = new XmlPath(Collections.emptyList(), false);

private List<String> hierarchy;
private boolean isAttribute;

public XmlPath(List<String> hierarchy, boolean isAttribute) {
this.hierarchy = hierarchy;
this.isAttribute = isAttribute;
}

public static XmlPath create(String value) {
List<String> hierarchy = Arrays.asList(value.substring(1).split("/"));

String name = hierarchy.get(hierarchy.size() - 1);

boolean isAttribute = false;

if (name.startsWith("@")) {
hierarchy.set(hierarchy.size() - 1, name.replaceFirst("@", ""));
isAttribute = true;
}

return new XmlPath(hierarchy, isAttribute);
}

public boolean isAttribute() {
return isAttribute;
}

public String getName() {
return hierarchy.get(hierarchy.size() - 1);
}

private XmlPath child(String name, boolean childIsAttribute) {
if (isAttribute) {
throw new IllegalStateException();
}

List<String> newHierarchy = new ArrayList<>(hierarchy);
newHierarchy.add(name);
return new XmlPath(newHierarchy, childIsAttribute);
}

public XmlPath child(String name) {
return child(name, false);
}

public XmlPath attribute(String name) {
return child(name, true);
}

public XmlPath parent() {
return new XmlPath(hierarchy.subList(0, hierarchy.size() - 1), false);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
XmlPath that = (XmlPath) o;
return isAttribute == that.isAttribute && Objects.equals(hierarchy, that.hierarchy);
}

@Override
public int hashCode() {
return Objects.hash(hierarchy, isAttribute);
}

@Override
public String toString() {
if (isAttribute) {
return "/" + String.join("/", hierarchy.subList(0, hierarchy.size() - 1)) + "/@" + getName();
} else {
return "/" + String.join("/", hierarchy);
}
}
}
14 changes: 7 additions & 7 deletions throwaway-prototype/core/pom.xml
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.rometools</groupId>
<artifactId>throwaway-prototype</artifactId>
<groupId>com.rometools.rome2.prototype</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>

<artifactId>throwaway-prototype-core</artifactId>
<artifactId>core</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>com.rometools</groupId>
<artifactId>throwaway-prototype-model</artifactId>
<groupId>com.rometools.rome2.prototype</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
Expand Down
Empty file.

This file was deleted.

Empty file.
5 changes: 3 additions & 2 deletions throwaway-prototype/core/src/main/java/com/rometools/rome/Rome.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.rometools.rome;

import com.google.common.io.ByteStreams;
import com.rometools.rome.model.rss.Feed;
import com.rometools.rome.model.Feed;
import com.rometools.rome.xml.XmlParser;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -30,7 +31,7 @@ static Rome.Builder custom() {
}

public Feed read(byte[] source) {
return null;
return new XmlParser(source).parse();
}

public Feed read(String source) {
Expand Down
Empty file.
Loading

0 comments on commit 75ee17b

Please sign in to comment.