/** * @param pmmlString PMML model encoded as an XML doc in a string * @return {@link PMML} object representing the model * @throws IOException if XML can't be unserialized */ public static PMML fromString(String pmmlString) throws IOException { // Emulate PMMLUtil.unmarshal here, but need to accept a Reader InputSource source = new InputSource(new StringReader(pmmlString)); try { return JAXBUtil.unmarshalPMML(ImportFilter.apply(source)); } catch (JAXBException | SAXException e) { throw new IOException(e); } }
static public void checkApplicationClasspath(){ String string = "<PMML xmlns=\"http://www.dmg.org/PMML-4_3\"/>"; try { JAXBUtil.unmarshalPMML(new StreamSource(new StringReader(string))); } catch(JAXBException je){ throw new IllegalArgumentException("Expected JPMML-Model version 1.4.X, got a legacy version. See https://issues.apache.org/jira/browse/SPARK-15526", je); } }
public static PMML loadPMML(String path) throws Exception { InputStream is = null; try { is = new FileInputStream(path); InputSource source = new InputSource(is); SAXSource transformedSource = ImportFilter.apply(source); return JAXBUtil.unmarshalPMML(transformedSource); } catch (Exception e) { e.printStackTrace(); throw e; } }
/** * @param pmmlString PMML model encoded as an XML doc in a string * @return {@link PMML} object representing the model * @throws IOException if XML can't be unserialized */ public static PMML fromString(String pmmlString) throws IOException { // Emulate PMMLUtil.unmarshal here, but need to accept a Reader InputSource source = new InputSource(new StringReader(pmmlString)); try { return JAXBUtil.unmarshalPMML(ImportFilter.apply(source)); } catch (JAXBException | SAXException e) { throw new IOException(e); } }
public static PMML loadPMML(String path) throws Exception { InputStream is = null; try { is = new FileInputStream(path); InputSource source = new InputSource(is); SAXSource transformedSource = ImportFilter.apply(source); return JAXBUtil.unmarshalPMML(transformedSource); } catch (Exception e) { e.printStackTrace(); throw e; } finally { IOUtils.closeQuietly(is); } }
@PostConstruct public void setUp() throws IOException, SAXException, JAXBException { try (InputStream is = properties.getModelLocation().getInputStream()) { Source transformedSource = ImportFilter.apply(new InputSource(is)); pmml = JAXBUtil.unmarshalPMML(transformedSource); Assert.state(!pmml.getModels().isEmpty(), "The provided PMML file at " + properties.getModelLocation() + " does not contain any model"); } }
@PostConstruct public void setUp() throws IOException, SAXException, JAXBException { try (InputStream is = properties.getModelLocation().getInputStream()) { Source transformedSource = ImportFilter.apply(new InputSource(is)); pmml = JAXBUtil.unmarshalPMML(transformedSource); Assert.state(!pmml.getModels().isEmpty(), "The provided PMML file at " + properties.getModelLocation() + " does not contain any model"); } }
/** * @see JAXBUtil#unmarshalPMML(Source) */ static public PMML unmarshal(InputStream is) throws SAXException, JAXBException { Source source = SAXUtil.createFilteredSource(is, new ImportFilter()); return JAXBUtil.unmarshalPMML(source); }
/** * @see JAXBUtil#unmarshalPMML(Source) */ static public PMML unmarshal(InputStream is) throws SAXException, JAXBException { Source source = SAXUtil.createFilteredSource(is, new ImportFilter()); return JAXBUtil.unmarshalPMML(source); }
/** * Quite manually parse our fake model representation in PMML. */ private static ALSModelDescription read(InputStream in) throws JAXBException, SAXException { PMML pmml = JAXBUtil.unmarshalPMML(ImportFilter.apply(new InputSource(in))); List<Extension> extensions = pmml.getExtensions(); Preconditions.checkNotNull(extensions); Preconditions.checkArgument(!extensions.isEmpty()); ALSModelDescription model = new ALSModelDescription(); for (Extension extension : extensions) { String name = extension.getName(); String value = extension.getValue(); Preconditions.checkNotNull(name); Preconditions.checkNotNull(value); model.getPathByKey().put(name, value); } return model; }
static public PMML unmarshal(Version version) throws IOException, JAXBException { if(!(Version.PMML_4_3).equals(version)){ throw new IllegalArgumentException(); } try(InputStream is = getStream(version)){ return JAXBUtil.unmarshalPMML(new StreamSource(is)); } }
static public PMML unmarshal(Class<?> clazz, XMLFilter... filters) throws IOException, SAXException, JAXBException { try(InputStream is = getStream(clazz)){ Source source = SAXUtil.createFilteredSource(is, filters); return JAXBUtil.unmarshalPMML(source); } }
@Test public void unmarshal() throws Exception { PMML pmml; System.setProperty("javax.xml.accessExternalDTD", "file"); try(InputStream is = ResourceUtil.getStream(XXEAttackTest.class);){ Source source = new StreamSource(is); pmml = JAXBUtil.unmarshalPMML(source); } finally { System.clearProperty("javax.xml.accessExternalDTD"); } List<?> content = ExtensionUtil.getContent(pmml); assertEquals(Arrays.asList("lol"), content); }
@Test public void copy() throws Exception { PMML pmml; try(InputStream is = ResourceUtil.getStream(Version.PMML_4_3)){ pmml = JAXBUtil.unmarshalPMML(new StreamSource(is)); } assertEquals(PMML.class, pmml.getClass()); ByteArrayOutputStream os = new ByteArrayOutputStream(); JAXBUtil.marshalPMML(pmml, new StreamResult(os)); assertTrue(os.size() > 0); }
@Test public void unmarshal() throws Exception { try(InputStream is = ResourceUtil.getStream(XEEAttackTest.class)){ JAXBUtil.unmarshalPMML(new StreamSource(is)); fail(); } catch(UnmarshalException ue){ // Ignored } } }
@Test public void unmarshalSecurely() throws Exception { try(InputStream is = ResourceUtil.getStream(XXEAttackTest.class)){ Source source = SAXUtil.createFilteredSource(is); JAXBUtil.unmarshalPMML(source); fail(); } catch(UnmarshalException ue){ Throwable cause = ue.getCause(); assertTrue(cause instanceof SAXParseException); } } }