[#2233] Add Result<?> DSLContext.fetchFromXML() to allow for loading results that were exported using Result.formatXML()

This commit is contained in:
lukaseder 2019-03-06 14:08:48 +01:00
parent 726e77960a
commit 80d8642c5b
2 changed files with 45 additions and 2 deletions

View File

@ -3726,6 +3726,31 @@ public interface DSLContext extends Scope , AutoCloseable {
@Support
Result<Record> fetchFromJSON(String string);
/**
* Fetch all data from an XML string.
* <p>
* This is the inverse of calling {@link Result#formatXML()}. Use the
* various conversion methods to retrieve other data types from the
* <code>Result</code>:
* <ul>
* <li> {@link Result#getValues(Field, Class)}</li>
* <li> {@link Result#getValues(int, Class)}</li>
* <li> {@link Result#getValues(String, Class)}</li>
* <li> {@link Result#getValues(Field, Converter)}</li>
* <li> {@link Result#getValues(int, Converter)}</li>
* <li> {@link Result#getValues(String, Converter)}</li>
* </ul>
* <p>
* Missing values result in <code>null</code>. Empty values result in empty
* <code>Strings</code>
*
* @param string The XML string
* @return The transformed result. This will never be <code>null</code>.
* @throws DataAccessException If anything went wrong parsing the XML file
*/
@Support
Result<Record> fetchFromXML(String string);
/**
* Fetch all data from a list of strings.
* <p>

View File

@ -61,6 +61,7 @@ import static org.jooq.impl.Tools.blocking;
import static org.jooq.impl.Tools.list;
import static org.jooq.tools.Convert.convert;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.io.StringReader;
@ -88,6 +89,8 @@ import java.util.function.Function;
import java.util.stream.Stream;
import javax.sql.DataSource;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.jooq.AlterIndexOnStep;
import org.jooq.AlterIndexStep;
@ -1451,9 +1454,8 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri
}
finally {
try {
if (reader != null) {
if (reader != null)
reader.close();
}
}
catch (IOException ignore) {}
}
@ -1461,6 +1463,22 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri
return fetchFromStringData(list);
}
@Override
public Result<Record> fetchFromXML(String string) {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
// TODO: Why does the SAXParser replace \r by \n?
XMLHandler handler = new XMLHandler(this);
saxParser.parse(new ByteArrayInputStream(string.getBytes()), handler);
return handler.result;
}
catch (Exception e) {
throw new DataAccessException("Could not read the XML string", e);
}
}
@Override
public Result<Record> fetchFromStringData(String[]... strings) {
return fetchFromStringData(list(strings), true);