Created
November 28, 2018 10:21
-
-
Save webbres/d2d4d94235a67fbb765dd73345ec82dc to your computer and use it in GitHub Desktop.
Custom port serialization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyCustomPortObjectSerializer extends PortObjectSerializer<MyCustomPortObject> | |
{ | |
private static final String FILE_NAME = "config.xml"; | |
@Override | |
public void savePortObject(MyCustomPortObject portObject, PortObjectZipOutputStream out, | |
ExecutionMonitor exec) throws IOException, CanceledExecutionException | |
{ | |
out.putNextEntry(new ZipEntry(FILE_NAME)); | |
try | |
{ | |
// Create the content to serialize, in my case I save my port data in XML | |
String xm = portObject.getXml(); | |
out.write(xml.getBytes()); | |
} catch (Exception e) | |
{ | |
throw new IOException(e); | |
} | |
out.closeEntry(); | |
} | |
@Override | |
public MyCustomPortObject loadPortObject(PortObjectZipInputStream in, PortObjectSpec spec, | |
ExecutionMonitor exec) throws IOException, CanceledExecutionException | |
{ | |
String entryName = in.getNextEntry().getName(); | |
MyCustomPortObject obj = null; | |
if (entryName.equals(FILE_NAME)) | |
{ | |
try | |
{ | |
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); | |
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); | |
Document doc = dBuilder.parse(in); | |
doc.getDocumentElement().normalize(); | |
obj = new MyCustomPortObject(MyCustomPortObjectFactory.create(doc)); | |
} catch (Exception e) | |
{ | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
in.close(); | |
} else if (!entryName.equals(FILE_NAME)) | |
{ | |
throw new IOException("Found unexpected zip entry " + entryName + "! Expected " + FILE_NAME); | |
} | |
return obj; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment