Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@
<dependency-check-maven.version>8.4.3</dependency-check-maven.version>
<gitflow-maven-plugin.version>1.21.0</gitflow-maven-plugin.version>
<sonar-maven-plugin.version>5.0.0.4389</sonar-maven-plugin.version>
<prettier-java.version>2.1.0</prettier-java.version>
<prettier-maven-plugin.version>0.22</prettier-maven-plugin.version>
<plugin.prettier.goal>write</plugin.prettier.goal>
<jacoco-maven-plugin.version>0.8.12</jacoco-maven-plugin.version>
<!-- empty argLine property, the value is set up by Jacoco during unit tests execution -->
<argLine />
Expand Down Expand Up @@ -285,6 +288,22 @@
<ossindexAnalyzerEnabled>false</ossindexAnalyzerEnabled>
</configuration>
</plugin>
<plugin>
<groupId>com.hubspot.maven.plugins</groupId>
<artifactId>prettier-maven-plugin</artifactId>
<version>${prettier-maven-plugin.version}</version>
<configuration>
<prettierJavaVersion>${prettier-java.version}</prettierJavaVersion>
</configuration>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>${plugin.prettier.goal}</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
Expand All @@ -305,6 +324,14 @@
<artifactId>sonar-maven-plugin</artifactId>
<version>${sonar-maven-plugin.version}</version>
</plugin>
<plugin>
<groupId>com.hubspot.maven.plugins</groupId>
<artifactId>prettier-maven-plugin</artifactId>
<version>${prettier-maven-plugin.version}</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Expand Down Expand Up @@ -386,5 +413,12 @@
</plugins>
</build>
</profile>
<profile>
<id>prettierCheck</id>
<properties>
<!-- In the CI environment we want to validate that code is formatted -->
<plugin.prettier.goal>check</plugin.prettier.goal>
</properties>
</profile>
</profiles>
</project>
144 changes: 74 additions & 70 deletions src/main/java/org/entur/netex/NetexParser.java
Original file line number Diff line number Diff line change
@@ -1,92 +1,96 @@
package org.entur.netex;

import org.entur.netex.index.api.NetexEntitiesIndex;
import org.entur.netex.index.impl.NetexEntitiesIndexImpl;
import org.entur.netex.loader.NetexXmlParser;
import org.entur.netex.loader.parser.NetexDocumentParser;
import org.rutebanken.netex.model.PublicationDeliveryStructure;

import jakarta.xml.bind.JAXBException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.entur.netex.index.api.NetexEntitiesIndex;
import org.entur.netex.index.impl.NetexEntitiesIndexImpl;
import org.entur.netex.loader.NetexXmlParser;
import org.entur.netex.loader.parser.NetexDocumentParser;
import org.rutebanken.netex.model.PublicationDeliveryStructure;

/**
* Main entry point to the library. Used to parse a NeTEx publication
* delivery into a queryable index.
*/
public class NetexParser {
private final NetexXmlParser xmlParser = new NetexXmlParser();

/**
* Parse a NeTEx publication delivery from one or more files in
* a zip archive
*
* @param pathToZip Path to zip file
* @return A queryable index of NeTEx entities
*/
public NetexEntitiesIndex parse(String pathToZip) throws IOException {
NetexEntitiesIndex index = new NetexEntitiesIndexImpl();
return parse(pathToZip, index);
}
private final NetexXmlParser xmlParser = new NetexXmlParser();

/**
* Parse a NeTEx publication delivery from one or more files in
* a zip archive into an existing index.
*
* Existing entities with same id will be overwritten.
*
* @param pathToZip Path to zip file
* @param index An instance of NetexEntitiesIndex
* @return The mutated index
*/
public NetexEntitiesIndex parse(String pathToZip, NetexEntitiesIndex index) throws IOException {
try(ZipFile zipFile = new ZipFile(pathToZip)) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while(entries.hasMoreElements()){
ZipEntry entry = entries.nextElement();
InputStream stream = zipFile.getInputStream(entry);
load(index, stream);
}
return index;
}
}
/**
* Parse a NeTEx publication delivery from one or more files in
* a zip archive
*
* @param pathToZip Path to zip file
* @return A queryable index of NeTEx entities
*/
public NetexEntitiesIndex parse(String pathToZip) throws IOException {
NetexEntitiesIndex index = new NetexEntitiesIndexImpl();
return parse(pathToZip, index);
}

/**
* Parse an input stream of a single NeTEx public delivery
*
* @param inputStream
* @return A queryable index of NeTEx entities
*/
public NetexEntitiesIndex parse(InputStream inputStream) {
NetexEntitiesIndex index = new NetexEntitiesIndexImpl();
load(index, inputStream);
return index;
/**
* Parse a NeTEx publication delivery from one or more files in
* a zip archive into an existing index.
*
* Existing entities with same id will be overwritten.
*
* @param pathToZip Path to zip file
* @param index An instance of NetexEntitiesIndex
* @return The mutated index
*/
public NetexEntitiesIndex parse(String pathToZip, NetexEntitiesIndex index)
throws IOException {
try (ZipFile zipFile = new ZipFile(pathToZip)) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
InputStream stream = zipFile.getInputStream(entry);
load(index, stream);
}
return index;
}
}

/**
* Parse an input stream of a single NeTEx publication delivery
* into an existing index
*
* Existing entities with same id will be overwritten.
*
* @param inputStream An InputStream
* @param index An instance of NetexEntitiesIndex
* @return The mutated index
*/
public NetexEntitiesIndex parse(InputStream inputStream, NetexEntitiesIndex index) {
load(index, inputStream);
return index;
}
/**
* Parse an input stream of a single NeTEx public delivery
*
* @param inputStream
* @return A queryable index of NeTEx entities
*/
public NetexEntitiesIndex parse(InputStream inputStream) {
NetexEntitiesIndex index = new NetexEntitiesIndexImpl();
load(index, inputStream);
return index;
}

/**
* Parse an input stream of a single NeTEx publication delivery
* into an existing index
*
* Existing entities with same id will be overwritten.
*
* @param inputStream An InputStream
* @param index An instance of NetexEntitiesIndex
* @return The mutated index
*/
public NetexEntitiesIndex parse(
InputStream inputStream,
NetexEntitiesIndex index
) {
load(index, inputStream);
return index;
}

private void load(NetexEntitiesIndex index, InputStream inputStream) {
try {
PublicationDeliveryStructure doc = xmlParser.parseXmlDoc(inputStream);
NetexDocumentParser.parseAndPopulateIndex(index, doc);
} catch (JAXBException e) {
throw new RuntimeException(e.getMessage(), e);
}
private void load(NetexEntitiesIndex index, InputStream inputStream) {
try {
PublicationDeliveryStructure doc = xmlParser.parseXmlDoc(inputStream);
NetexDocumentParser.parseAndPopulateIndex(index, doc);
} catch (JAXBException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}
Loading