List of usage examples for java.beans XMLEncoder setPersistenceDelegate
public void setPersistenceDelegate(Class<?> type, PersistenceDelegate delegate)
From source file:Main.java
public static void main(String[] argv) throws Exception { MyClass o = new MyClass(123); XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("outfilename.xml"))); String[] propertyNames = new String[] { "prop" }; encoder.setPersistenceDelegate(MyClass.class, new DefaultPersistenceDelegate(propertyNames)); encoder.writeObject(o);//from www .ja v a 2 s .c o m encoder.close(); }
From source file:com.swingtech.commons.util.ClassUtil.java
/** * NOTE: When using this to print an object it will not display the * primitive type boolean. Must use the wrapper class. All other primitives * work fine.// w ww . j a v a 2s .c om * * NOTE: If an int value has a 0 it won't display. * * NOTE: Object must have a public constructor. * * @param object * @return */ public static String getXMLForObject(final Object object) { ByteArrayOutputStream baos = null; XMLEncoder e = null; baos = new ByteArrayOutputStream(); e = new XMLEncoder(new BufferedOutputStream(baos)); e.setPersistenceDelegate(Date.class, new PersistenceDelegate() { @Override protected Expression instantiate(final Object oldInstance, final Encoder out) { final Date date = (Date) oldInstance; final Long time = new Long(date.getTime()); return new Expression(date, date.getClass(), "new", new Object[] { time }); } }); e.setPersistenceDelegate(BigDecimal.class, new PersistenceDelegate() { @Override protected Expression instantiate(final Object oldInstance, final Encoder out) { final BigDecimal bigDec = (BigDecimal) oldInstance; final double doubleVal = bigDec.doubleValue(); return new Expression(bigDec, bigDec.getClass(), "new", new Object[] { new Double(doubleVal) }); } }); e.writeObject(object); e.close(); return baos.toString(); }
From source file:com.alvermont.terraj.fracplanet.ui.AbstractTerrainViewerFrame.java
private void saveParamsItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_saveParamsItemActionPerformed {//GEN-HEADEREND:event_saveParamsItemActionPerformed final int choice = this.xmlChooser.showSaveDialog(this); if (choice == JFileChooser.APPROVE_OPTION) { if (!this.xmlChooser.getSelectedFile().isFile() || (JOptionPane.showConfirmDialog(this, "This file already exists. Do you want to\n" + "overwrite it?", "Replace File?", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)) { try { final File target = FileUtils.addExtension(this.xmlChooser.getSelectedFile(), ".xml"); final XMLEncoder enc = new XMLEncoder(new FileOutputStream(target)); getParameters().getRenderParameters().setCameraPosition(display.getCameraPosition()); getParameters().getCameraPositionParameters().setPositions(this.cameraPosDialog.getPositions()); enc.setPersistenceDelegate(TerrainParameters.ObjectTypeEnum.class, new EnumDelegate<TerrainParameters.ObjectTypeEnum>( TerrainParameters.ObjectTypeEnum.class)); enc.writeObject(getParameters()); enc.close();/*from ww w. j a va 2 s .co m*/ } catch (IOException ioe) { log.error("Error writing xml file", ioe); JOptionPane.showMessageDialog(this, "Error: " + ioe.getMessage() + "\nCheck log file for full details", "Error Saving", JOptionPane.ERROR_MESSAGE); } } } }
From source file:de.huxhorn.lilith.swing.ApplicationPreferences.java
private void writeLevelColors(Map<LoggingEvent.Level, ColorScheme> colors) { File appPath = getStartupApplicationPath(); File file = new File(appPath, LEVEL_COLORS_XML_FILENAME); try {//from www .ja va2 s . com BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); XMLEncoder e = new XMLEncoder(bos); PersistenceDelegate delegate = new EnumPersistenceDelegate(); e.setPersistenceDelegate(LoggingEvent.Level.class, delegate); e.writeObject(colors); e.close(); } catch (Throwable ex) { if (logger.isWarnEnabled()) logger.warn("Exception while writing colors!", ex); IOUtilities.interruptIfNecessary(ex); } }
From source file:de.huxhorn.lilith.swing.ApplicationPreferences.java
private void writeStatusColors(Map<HttpStatus.Type, ColorScheme> colors) { File appPath = getStartupApplicationPath(); File file = new File(appPath, STATUS_COLORS_XML_FILENAME); try {/*from ww w. java2 s .co m*/ BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); XMLEncoder e = new XMLEncoder(bos); PersistenceDelegate delegate = new EnumPersistenceDelegate(); e.setPersistenceDelegate(HttpStatus.Type.class, delegate); e.writeObject(colors); e.close(); } catch (Throwable ex) { if (logger.isWarnEnabled()) logger.warn("Exception while writing colors!", ex); IOUtilities.interruptIfNecessary(ex); } }
From source file:org.openmrs.reporting.ReportObjectXMLEncoder.java
@SuppressWarnings("unchecked") public String toXmlString() { ByteArrayOutputStream arr = new ByteArrayOutputStream(); EnumDelegate enumDelegate = new EnumDelegate(); XMLEncoder enc = new XMLEncoder(new BufferedOutputStream(arr)); enc.setPersistenceDelegate(User.class, new UserDelegate()); enc.setPersistenceDelegate(Location.class, new LocationDelegate()); enc.setPersistenceDelegate(Cohort.class, new CohortDelegate()); enc.setPersistenceDelegate(Concept.class, new ConceptDelegate()); enc.setPersistenceDelegate(Drug.class, new DrugDelegate()); enc.setPersistenceDelegate(Encounter.class, new EncounterDelegate()); enc.setPersistenceDelegate(Patient.class, new PatientDelegate()); enc.setPersistenceDelegate(Program.class, new ProgramDelegate()); enc.setPersistenceDelegate(ProgramWorkflow.class, new ProgramWorkflowDelegate()); enc.setPersistenceDelegate(ProgramWorkflowState.class, new ProgramWorkflowStateDelegate()); enc.setPersistenceDelegate(ConceptAnswer.class, new ConceptAnswerDelegate()); enc.setPersistenceDelegate(EncounterType.class, new EncounterTypeDelegate()); enc.setPersistenceDelegate(PersonAttributeType.class, new PersonAttributeTypeDelegate()); enc.setPersistenceDelegate(ConceptNumeric.class, new ConceptNumericDelegate()); Set<Class> alreadyAdded = new HashSet<Class>(); {/* w ww . j a v a 2 s . c om*/ List<Class> enumClasses = new ArrayList<Class>(); enumClasses.add(PatientSetService.Modifier.class); enumClasses.add(PatientSetService.TimeModifier.class); enumClasses.add(PatientSetService.BooleanOperator.class); enumClasses.add(PatientSetService.GroupMethod.class); for (Class clz : enumClasses) { enc.setPersistenceDelegate(clz, enumDelegate); alreadyAdded.add(clz); } } // This original implementation won't handle enums that aren't direct properties of the bean, but I'm leaving it here anyway. for (Field f : this.objectToEncode.getClass().getDeclaredFields()) { Class clz = f.getType(); if (clz.isEnum() && !alreadyAdded.contains(clz)) { try { enc.setPersistenceDelegate(clz, enumDelegate); alreadyAdded.add(clz); } catch (Exception e) { log.error("ReportObjectXMLEncoder failed to write enumeration " + f.getName(), e); } } } log.debug("objectToEncode.type: " + objectToEncode.getClass()); enc.writeObject(this.objectToEncode); enc.close(); return arr.toString(); }