refactoring package-list.json handling

This commit is contained in:
Grahame Grieve 2022-12-12 19:54:37 +11:00
parent 915d5e89b7
commit f5d486f171
6 changed files with 283 additions and 23 deletions

View File

@ -19,7 +19,7 @@
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>

View File

@ -47,6 +47,12 @@ public class JsonArray extends JsonElement implements Iterable<JsonElement> {
return this;
}
public JsonArray add(int i, JsonElement node) throws JsonException {
check(node != null, "null object in JsonArray.add()");
items.add(i, node);
return this;
}
public JsonArray add(String value) throws JsonException {
check(value != null, "null value in JsonArray.add()");
items.add(new JsonString(value));
@ -134,5 +140,10 @@ public class JsonArray extends JsonElement implements Iterable<JsonElement> {
public void setExtraComma(boolean extraComma) {
this.extraComma = extraComma;
}
public void remove(JsonElement e) {
items.remove(e);
}
}

View File

@ -37,6 +37,7 @@ import org.hl7.fhir.utilities.json.model.JsonElement;
import org.hl7.fhir.utilities.json.model.JsonObject;
import org.hl7.fhir.utilities.json.parser.JsonParser;
import org.hl7.fhir.utilities.npm.NpmPackage.NpmPackageFolder;
import org.hl7.fhir.utilities.npm.PackageList.PackageListEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -728,9 +729,9 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
}
String pu = Utilities.pathURL(url, "package-list.json");
String aurl = pu;
JsonObject json;
PackageList pl;
try {
json = JsonParser.parseObjectFromUrl(pu);
pl = PackageList.fromUrl(pu);
} catch (Exception e) {
String pv = Utilities.pathURL(url, v, "package.tgz");
try {
@ -741,12 +742,12 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
throw new FHIRException("Error fetching package directly (" + pv + "), or fetching package list for " + id + " from " + pu + ": " + e1.getMessage(), e1);
}
}
if (!id.equals(json.asString("package-id")))
throw new FHIRException("Package ids do not match in " + pu + ": " + id + " vs " + json.asString("package-id"));
for (JsonObject vo : json.getJsonObjects("list")) {
if (v.equals(vo.asString("version"))) {
aurl = Utilities.pathURL(vo.asString("path"), "package.tgz");
String u = Utilities.pathURL(vo.asString("path"), "package.tgz");
if (!id.equals(pl.pid()))
throw new FHIRException("Package ids do not match in " + pu + ": " + id + " vs " + pl.pid());
for (PackageListEntry vo : pl.versions()) {
if (v.equals(vo.version())) {
aurl = Utilities.pathURL(vo.path(), "package.tgz");
String u = Utilities.pathURL(vo.path(), "package.tgz");
return new InputStreamWithSrc(fetchFromUrlSpecific(u, true), u, v);
}
}
@ -769,13 +770,12 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
if (url == null) {
throw new FHIRException("Unable to resolve package id " + id);
}
String pu = Utilities.pathURL(url, "package-list.json");
JsonObject json = JsonParser.parseObjectFromUrl(pu);
if (!id.equals(json.asString("package-id")))
throw new FHIRException("Package ids do not match in " + pu + ": " + id + " vs " + json.asString("package-id"));
for (JsonObject vo : json.getJsonObjects("list")) {
if (vo.asBoolean("current")) {
return vo.asString("version");
PackageList pl = PackageList.fromUrl(Utilities.pathURL(url, "package-list.json"));
if (!id.equals(pl.pid()))
throw new FHIRException("Package ids do not match in " + pl.source() + ": " + id + " vs " + pl.pid());
for (PackageListEntry vo : pl.versions()) {
if (vo.current()) {
return vo.version();
}
}

View File

@ -0,0 +1,254 @@
package org.hl7.fhir.utilities.npm;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.hl7.fhir.utilities.FhirPublication;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.json.JsonException;
import org.hl7.fhir.utilities.json.model.JsonArray;
import org.hl7.fhir.utilities.json.model.JsonObject;
import org.hl7.fhir.utilities.json.parser.JsonParser;
import org.hl7.fhir.utilities.npm.PackageList.PackageListEntry;
public class PackageList {
public static class PackageListEntry {
private JsonObject json;
private PackageListEntry(JsonObject json) {
super();
this.json = json;
}
public String version() {
return json.asString("version");
}
public String path() {
return json.asString("path");
}
public boolean current() {
return json.has("current") && json.asBoolean("current");
}
public boolean cibuild() {
return "ci-build".equals(json.asString("status"));
}
public String sequence() {
return json.asString("sequence");
}
public String fhirVersion() {
return json.asString("fhirversion");
}
public String status() {
return json.asString("status");
}
public String desc() {
return json.asString("desc");
}
public String date() {
return json.asString("date");
}
public void setDate(String date) {
json.set("date", date);
}
public boolean isPartofMainSpec() {
return Utilities.startsWithInList(path(), "http://hl7.org/fhir/DSTU2", "http://hl7.org/fhir/2015Sep", "http://hl7.org/fhir/2015May");
}
/**
* For a variety of reasons in the past, the path is not always under the canonical
* this method determines the path *if it exists*
*
* @param root
* @param v
* @throws IOException
*/
public String determineLocalPath(String url, String root) throws IOException {
if (!isPartofMainSpec() && path().startsWith(url+"/")) {
String tail = path().substring(url.length()+1);
return Utilities.path(root, tail);
} else {
return null;
}
}
public void setCurrent(boolean b) {
if (b) {
json.set("current", true);
} else {
if (json.has("current")) {
json.remove("current");
}
}
}
public void init(String version, String path, String status, String sequence, FhirPublication fhirVersion) {
json.set("version", version);
json.set("path", path);
json.set("status", status);
json.set("sequence", sequence);
json.set("fhirversion", fhirVersion.toCode());
}
public void describe(String desc, String descMD, String changes) {
if (!Utilities.noString(desc)) {
json.set("desc", desc);
}
if (!Utilities.noString(descMD)) {
json.set("descmd", descMD);
}
if (!Utilities.noString(changes)) {
json.set("changes", changes);
}
}
}
private String source;
private JsonObject json;
private List<PackageListEntry> list = new ArrayList<>();
private List<PackageListEntry> versions = new ArrayList<>();
private PackageListEntry cibuild;
public PackageList() {
super();
json = new JsonObject();
json.add("list", new JsonArray());
}
public PackageList(JsonObject json) {
this.json = json;
for (JsonObject o : json.getJsonObjects("list")) {
PackageListEntry e = new PackageListEntry(o);
list.add(e);
if ("current".equals(o.asString("version"))) {
cibuild = e;
} else {
versions.add(e);
}
}
}
public static PackageList fromFile(File f) throws JsonException, IOException {
return new PackageList(JsonParser.parseObject(f)).setSource(f.getAbsolutePath());
}
public PackageList setSource(String src) {
this.source = src;
return this;
}
public static PackageList fromFile(String f) throws JsonException, IOException {
return new PackageList(JsonParser.parseObjectFromFile(f)).setSource(f);
}
public static PackageList fromContent(byte[] cnt) throws JsonException, IOException {
return new PackageList(JsonParser.parseObject(cnt)).setSource("unknown");
}
public static PackageList fromUrl(String url) throws JsonException, IOException {
return new PackageList(JsonParser.parseObjectFromUrl(url)).setSource(url);
}
public String source() {
return source;
}
public String canonical() {
return json.asString("canonical");
}
public String pid() {
return json.asString("package-id");
}
public boolean hasPid() {
return json.has("package-id");
}
public String category() {
return json.asString("category");
}
public List<PackageListEntry> list() {
return list;
}
public List<PackageListEntry> versions() {
return versions;
}
public PackageListEntry ciBuild() {
return cibuild;
}
public String toJson() {
return JsonParser.compose(json, true);
}
public PackageListEntry newVersion(String version, String path, String status, String sequence, FhirPublication fhirVersion) {
JsonObject o = new JsonObject();
PackageListEntry e = new PackageListEntry(o);
versions.add(0, e);
if (cibuild != null) {
json.getJsonArray("list").add(1, o);
list.add(1, e);
} else {
json.getJsonArray("list").add(0, o);
list.add(0, e);
}
e.init(version, path, status, sequence, fhirVersion);
return e;
}
public void init(String name, String canonical, String title, String category, String introduction) {
json.add("package-id", name);
json.add("canonical", canonical);
json.add("title", title);
json.add("category", category);
json.add("introduction", introduction);
}
public void addCIBuild(String version, String path, String desc, String status) {
if (cibuild != null) {
json.getJsonArray("list").remove(cibuild.json);
}
cibuild = new PackageListEntry(new JsonObject());
json.getJsonArray("list").add(0, cibuild.json);
}
public PackageListEntry findByVersion(String version) {
for (PackageListEntry p : versions) {
if (version.equals(p.version())) {
return p;
}
}
return null;
}
public void save(String filepath) throws IOException {
TextFile.stringToFile(toJson(), filepath);
}
public String determineLocalPath(String url, String root) throws IOException {
if (canonical().startsWith(url+"/")) {
String tail = canonical().substring(url.length()+1);
return Utilities.path(root, tail);
} else {
return null;
}
}
}

View File

@ -41,8 +41,6 @@ import java.util.Map;
import java.util.Set;
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
/**
* An implementation of <a
* href="http://java.sun.com/javase/6/docs/api/javax/xml/namespace/NamespaceContext.html">
@ -50,7 +48,7 @@ import javax.xml.namespace.NamespaceContext;
*
* @author McDowell
*/
public final class NamespaceContextMap implements NamespaceContext {
public final class NamespaceContextMap {
private final Map<String, String> prefixMap;
private final Map<String, Set<String>> nsMap;
@ -133,21 +131,18 @@ public final class NamespaceContextMap implements NamespaceContext {
return nsMap;
}
@Override
public String getNamespaceURI(String prefix) {
checkNotNull(prefix);
String nsURI = prefixMap.get(prefix);
return nsURI == null ? XMLConstants.NULL_NS_URI : nsURI;
}
@Override
public String getPrefix(String namespaceURI) {
checkNotNull(namespaceURI);
Set<String> set = nsMap.get(namespaceURI);
return set == null ? null : set.iterator().next();
}
@Override
public Iterator<String> getPrefixes(String namespaceURI) {
checkNotNull(namespaceURI);
Set<String> set = nsMap.get(namespaceURI);

View File

@ -19,7 +19,7 @@
<properties>
<hapi_fhir_version>6.2.1</hapi_fhir_version>
<validator_test_case_version>1.2.0</validator_test_case_version>
<validator_test_case_version>1.2.1-SNAPSHOT</validator_test_case_version>
<junit_jupiter_version>5.7.1</junit_jupiter_version>
<junit_platform_launcher_version>1.8.2</junit_platform_launcher_version>
<maven_surefire_version>3.0.0-M5</maven_surefire_version>