Skip to content

Instantly share code, notes, and snippets.

@webbres
Created November 28, 2018 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webbres/d2d4d94235a67fbb765dd73345ec82dc to your computer and use it in GitHub Desktop.
Save webbres/d2d4d94235a67fbb765dd73345ec82dc to your computer and use it in GitHub Desktop.
Custom port serialization
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