Skip to content

Commit

Permalink
Imported Java code from the JPMML-Converter project
Browse files Browse the repository at this point in the history
  • Loading branch information
vruusmann committed Dec 2, 2024
1 parent 6395ba7 commit 5453019
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
19 changes: 19 additions & 0 deletions pmml-model/src/main/java/org/jpmml/model/SAXUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
*/
package org.jpmml.model;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Result;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;

import org.jpmml.model.filters.CountFilter;
import org.jpmml.model.filters.DepthFilter;
Expand All @@ -25,6 +31,19 @@ public class SAXUtil {
private SAXUtil(){
}

static
public void transform(InputSource source, Result result, XMLFilter... filters) throws IOException, TransformerConfigurationException, ParserConfigurationException, SAXException {
SAXTransformerFactory transformerFactory = (SAXTransformerFactory)TransformerFactory.newInstance();

TransformerHandler transformerHandler = transformerFactory.newTransformerHandler();
transformerHandler.setResult(result);

XMLReader xmlReader = SAXUtil.createFilteredReader(SAXUtil.createXMLReader(), filters);
xmlReader.setContentHandler(transformerHandler);

xmlReader.parse(source);
}

/**
* @see ImportFilter
* @see SkipFilter
Expand Down
13 changes: 1 addition & 12 deletions pmml-model/src/test/java/org/jpmml/model/SchemaUpdateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;

import jakarta.xml.bind.JAXBException;
Expand Down Expand Up @@ -42,16 +39,8 @@ public byte[] upgradeToLatest(byte[] bytes) throws IOException, ParserConfigurat
public byte[] downgrade(byte[] bytes, Version version) throws IOException, TransformerConfigurationException, ParserConfigurationException, SAXException {
ByteArrayOutputStream result = new ByteArrayOutputStream();

SAXTransformerFactory transformerFactory = (SAXTransformerFactory)TransformerFactory.newInstance();

TransformerHandler transformer = transformerFactory.newTransformerHandler();
transformer.setResult(new StreamResult(result));

ExportFilter exportFilter = new ExportFilter(SAXUtil.createXMLReader(), version);
exportFilter.setContentHandler(transformer);

try(InputStream is = new ByteArrayInputStream(bytes)){
exportFilter.parse(new InputSource(is));
SAXUtil.transform(new InputSource(is), new StreamResult(result), new ExportFilter(version));
}

return result.toByteArray();
Expand Down

0 comments on commit 5453019

Please sign in to comment.