Skip to content

Commit f2c7817

Browse files
committed
rework the schema forward migration
for run documents, there is a lot more variation in how the schema is declared, so we have to do a more generous replacement
1 parent ce0605d commit f2c7817

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

src/main/java/edu/ucsb/nceas/mdqengine/serialize/XmlMarshaller.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,25 @@
22

33
import org.apache.commons.io.IOUtils;
44
import org.apache.commons.lang.StringEscapeUtils;
5+
import org.w3c.dom.Document;
6+
import org.xml.sax.InputSource;
57
import org.xml.sax.SAXException;
68

79
import javax.xml.XMLConstants;
810
import javax.xml.bind.JAXBContext;
911
import javax.xml.bind.JAXBException;
1012
import javax.xml.bind.Marshaller;
1113
import javax.xml.bind.Unmarshaller;
14+
import javax.xml.parsers.DocumentBuilder;
15+
import javax.xml.parsers.DocumentBuilderFactory;
1216
import javax.xml.parsers.ParserConfigurationException;
1317
import javax.xml.transform.stream.StreamSource;
1418
import javax.xml.validation.Schema;
1519
import javax.xml.validation.SchemaFactory;
1620
import java.io.ByteArrayOutputStream;
1721
import java.io.IOException;
1822
import java.io.InputStream;
23+
import java.io.StringReader;
1924
import java.io.UnsupportedEncodingException;
2025
import java.util.Map;
2126

@@ -133,4 +138,22 @@ public static String normalizeNamespace(String xml) {
133138
return result;
134139
}
135140

141+
/**
142+
* Extracts the root namespace URI from an XML string.
143+
*
144+
* @param xml the XML string from which the root namespace will be extracted
145+
* @return the root namespace URI of the XML document
146+
* @throws ParserConfigurationException
147+
* @throws SAXException
148+
* @throws IOException
149+
*/
150+
public static String getRootNamespace(String xml) throws ParserConfigurationException, SAXException, IOException {
151+
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
152+
dbf.setNamespaceAware(true);
153+
DocumentBuilder db = dbf.newDocumentBuilder();
154+
Document doc = db.parse(new InputSource(new StringReader(xml)));
155+
156+
return doc.getDocumentElement().getNamespaceURI();
157+
}
158+
136159
}

src/main/java/edu/ucsb/nceas/mdqengine/store/DatabaseStore.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import java.time.Instant;
3333
import java.util.*;
3434
import java.util.Date;
35+
import java.util.regex.Matcher;
36+
import java.util.regex.Pattern;
3537

3638
/**
3739
* Persistent storage for quality runs.
@@ -184,9 +186,15 @@ public Run getRun(String metadataId, String suiteId) throws MetadigStoreExceptio
184186
// Convert the returned run xml document to a 'run' object.
185187

186188
// first migrate the schema forward
187-
188-
resultStr = resultStr.replace("https://nceas.ucsb.edu/mdqe/v1", "https://nceas.ucsb.edu/mdqe/v1.2");
189-
resultStr = resultStr.replace("https://nceas.ucsb.edu/mdqe/v1.1","https://nceas.ucsb.edu/mdqe/v1.2");
189+
String ns = XmlMarshaller.getRootNamespace(resultStr);
190+
191+
if ("https://nceas.ucsb.edu/mdqe/v1".equals(ns) || "https://nceas.ucsb.edu/mdqe/v1.1".equals(ns)) {
192+
// Replace known older namespaces with v1.2
193+
resultStr = resultStr.replace("https://nceas.ucsb.edu/mdqe/v1",
194+
"https://nceas.ucsb.edu/mdqe/v1.2");
195+
resultStr = resultStr.replace("https://nceas.ucsb.edu/mdqe/v1.1",
196+
"https://nceas.ucsb.edu/mdqe/v1.2");
197+
}
190198

191199
InputStream is = new ByteArrayInputStream(resultStr.getBytes());
192200
run = TypeMarshaller.unmarshalTypeFromStream(Run.class, is);

0 commit comments

Comments
 (0)