/** Display a dialog which allows the user to export a scene to an OBJ file. */ public static void exportFile(BFrame parent, Scene theScene) { // Display a dialog box with options on how to export the scene. // ValueField errorField = new ValueField(0.05, ValueField.POSITIVE); // final ValueField widthField = new ValueField(200.0, ValueField.INTEGER+ValueField.POSITIVE); // final ValueField heightField = new ValueField(200.0, ValueField.INTEGER+ValueField.POSITIVE); // final ValueSlider qualitySlider = new ValueSlider(0.0, 1.0, 100, 0.5); // final BCheckBox smoothBox = new BCheckBox(Translate.text("subdivideSmoothMeshes"), true); final BCheckBox injectChoice = new BCheckBox(Translate.text("Inject"), true); // final BCheckBox UVChoice = new BCheckBox(Translate.text("Export UV"), true); /*BComboBox exportChoice = new BComboBox(new String [] { Translate.text("exportWholeScene"), Translate.text("selectedObjectsOnly") });*/ ComponentsDialog dlg; if (theScene.getSelection().length > 0) dlg = new ComponentsDialog( parent, Translate.text("InjectToM2"), new Widget[] {injectChoice}, new String[] {null, null, null, null, null}); else dlg = new ComponentsDialog( parent, Translate.text("exportToM2"), new Widget[] {injectChoice}, new String[] {null, null, null, null}); if (!dlg.clickedOk()) return; // Ask the user to select the output file. BFileChooser fc = new BFileChooser(BFileChooser.SAVE_FILE, Translate.text("exportToWMO")); fc.setSelectedFile(new File("Untitled.m2")); if (ArtOfIllusion.getCurrentDirectory() != null) fc.setDirectory(new File(ArtOfIllusion.getCurrentDirectory())); if (!fc.showDialog(parent)) return; File dir = fc.getDirectory(); f = fc.getSelectedFile(); String name = f.getName(); String baseName = (name.endsWith(".m2") ? name.substring(0, name.length() - 3) : name); ArtOfIllusion.setCurrentDirectory(dir.getAbsolutePath()); m2 obj = null; try { obj = new m2(fileLoader.openBuffer(f.getAbsolutePath())); } catch (InvalidClassException e) { // TODO Auto-generated catch block e.printStackTrace(); } writeScene(theScene, obj, false, 0.05, false, injectChoice.getState()); }
/** * On deserialization, replace this proxy by the static constant. * * @return The resolved key. * @throws ObjectStreamException If the key has not been found. This exception should not occur is * the key is deserialized using the same Geotk version than the one that serialized the key. */ protected Object readResolve() throws ObjectStreamException { try { return definer.getField(field).get(null); } catch (ReflectiveOperationException cause) { final InvalidClassException e = new InvalidClassException( definer.getName(), Errors.format(Errors.Keys.IllegalKey_1, this)); e.initCause(cause); throw e; } }
private <O> O instantiate(ClassTypeModel<O> typeModel, Map<FieldModel<? super O, ?>, ?> state) throws InvalidClassException { try { ObjectFactory<? super O> factory = context.getFactory(typeModel.getJavaType()); // return factory.create(typeModel.getJavaType(), state); } catch (Exception e) { InvalidClassException ice = new InvalidClassException( "Cannot instantiate object from class " + typeModel.getJavaType().getName()); ice.initCause(e); throw ice; } }
public static void main(String args[]) throws Exception { File f = new File("tmp.ser"); if (args[0].compareTo("-s") == 0) { FileOutputStream of = new FileOutputStream(f); ObjectOutputStream oos = new ObjectOutputStream(of); Class cl = Class.forName(args[1]); oos.writeObject(cl); if (ObjectStreamClass.lookup(cl) != null) oos.writeObject(cl.newInstance()); oos.close(); System.out.println("Serialized Class " + cl.getName()); } else if (args[0].compareTo("-de") == 0) { FileInputStream inf = new FileInputStream(f); ObjectInputStream ois = new ObjectInputStream(inf); Class cl = null; try { cl = (Class) ois.readObject(); throw new Error("Expected InvalidClassException to be thrown"); } catch (InvalidClassException e) { System.out.println("Caught expected exception DeSerializing class " + e.getMessage()); } ois.close(); } else if (args[0].compareTo("-doe") == 0) { FileInputStream inf = new FileInputStream(f); ObjectInputStream ois = new ObjectInputStream(inf); Class cl = null; cl = (Class) ois.readObject(); try { ois.readObject(); throw new Error("Expected InvalidClassException to be thrown"); } catch (InvalidClassException e) { System.out.println("Caught expected exception DeSerializing class " + e.getMessage()); } ois.close(); } else if (args[0].compareTo("-d") == 0) { FileInputStream inf = new FileInputStream(f); ObjectInputStream ois = new ObjectInputStream(inf); Class cl = (Class) ois.readObject(); try { ois.readObject(); } catch (EOFException e) { } ois.close(); System.out.println("DeSerialized Class " + cl.getName()); } }
/** * Deserialize the contents of File with given name containing a string and return the resulting * string. If the indicated file doesnt exist or an error occurs, null is returned. If casually is * false, an error message is printed and an exception is raised if the file was not found or an * error occured on loading. */ public static Object loadObject(String Filename, boolean casually) { Object s = null; File f = new File(Filename); if (f.exists()) { try { s = (Object) load(new File(Filename)); } catch (InvalidClassException e) { System.err.println( "WARNING: loading object File " + Filename + " not possible, this may happen on source code changes."); System.err.println(e.getMessage()); } catch (Exception e) { throw new RuntimeException( "WARNING: loading object File " + Filename + " not possible! (" + e.getMessage() + ")"); } return s; } else { if (!casually) System.err.println("Error in Serializer: file " + Filename + " not found!"); return null; } }
/** * Creates a tile manager from the given file or directory. * * <p> * * <ul> * <li>If the argument {@linkplain Files#isRegularFile(Path, LinkOption...)} is a file} having * the {@code ".serialized"} suffix, then this method deserializes the object in the given * file and passes it to {@link #createFromObject(Object)}. * <li>If the given argument {@linkplain Files#isDirectory(Path, LinkOption...)} is a * directory}, then this method delegates to {@link #create(Path, PathMatcher, * ImageReaderSpi)} which scan all image files found in the directory. * <li>Otherwise an {@link IOException} is thrown. * </ul> * * @param file The serialized file or the directory to scan. * @return A tile manager created from the tiles in the given directory. * @throws IOException If the given file is not recognized, or an I/O operation failed. * @since 3.15 */ public TileManager[] create(final Path file) throws IOException { if (Files.isRegularFile(file, LinkOption.NOFOLLOW_LINKS)) { final String suffix = file.getFileName().toString(); if (!suffix.endsWith(".serialized")) { throw new IOException(Errors.format(Errors.Keys.UnknownFileSuffix_1, suffix)); } final Object manager; try (InputStream fin = Files.newInputStream(file); ObjectInputStream in = new ObjectInputStream(fin)) { try { manager = in.readObject(); } catch (ClassNotFoundException cause) { InvalidClassException ex = new InvalidClassException(cause.getLocalizedMessage()); ex.initCause(cause); throw ex; } } return setSourceFile(createFromObject(manager), file); } else if (Files.isDirectory(file, LinkOption.NOFOLLOW_LINKS)) { return create(file, null, null); } else { throw new IOException(Errors.format(Errors.Keys.NotADirectory_1, file)); } }