private void readRoot() throws InvalidPreferencesFormatException, IOException { // Begin starting tag skipTill("<root"); // type attribute skipTill("type=\""); String type = readTill("\""); Preferences root; if ("user".equals(type)) { root = factory.userRoot(); } else if ("system".equals(type)) { root = factory.systemRoot(); } else { throw new InvalidPreferencesFormatException("Unknown type: " + type); } // Read root map and subnodes readMap(root); readNodes(root); // Ending tag skipTill("</root>"); }
private void readMap(Preferences node) throws InvalidPreferencesFormatException, IOException { // Begin map tag skipTill("<map"); // Empty map? if (line.startsWith("/>")) { line = line.substring(2); return; } // Map entries readEntries(node); // Ending tag skipTill("</map>"); }
private void skipTill(String s) throws InvalidPreferencesFormatException, IOException { while(true) { if (line == null) throw new InvalidPreferencesFormatException(s + " not found"); int index = line.indexOf(s); if (index == -1) { line = br.readLine(); } else { line = line.substring(index+s.length()); return; } } }
private String nextTag() throws InvalidPreferencesFormatException, IOException { while(true) { if (line == null) throw new InvalidPreferencesFormatException("unexpected EOF"); int start = line.indexOf("<"); if (start == -1) { line = br.readLine(); } else { // Find end of tag int end = start+1; while (end != line.length() && " \t\r\n".indexOf(line.charAt(end)) == -1) { end++; } // Line now starts at the found tag String tag = line.substring(start+1,end); line = line.substring(start); return tag; } } }
static void importPreferences(InputStream is) throws IOException, InvalidPreferencesFormatException { try { Document doc = loadPrefsDoc(is); String xmlVersion = doc.getDocumentElement().getAttribute("EXTERNAL_XML_VERSION"); if (xmlVersion.compareTo(EXTERNAL_XML_VERSION) > 0) throw new InvalidPreferencesFormatException( "Exported preferences file format version " + xmlVersion + " is not supported. This java installation can read" + " versions " + EXTERNAL_XML_VERSION + " or older. You may need" + " to install a newer version of JDK."); Element xmlRoot = (Element) doc.getDocumentElement(). getChildNodes().item(0); Preferences prefsRoot = (xmlRoot.getAttribute("type").equals("user") ? Preferences.userRoot() : Preferences.systemRoot()); ImportSubtree(prefsRoot, xmlRoot); } catch(SAXException e) { throw new InvalidPreferencesFormatException(e); } }
static void importMap(InputStream is, Map m) throws IOException, InvalidPreferencesFormatException { try { Document doc = loadPrefsDoc(is); Element xmlMap = doc.getDocumentElement(); String mapVersion = xmlMap.getAttribute("MAP_XML_VERSION"); if (mapVersion.compareTo(MAP_XML_VERSION) > 0) throw new InvalidPreferencesFormatException( "Preferences map file format version " + mapVersion + " is not supported. This java installation can read" + " versions " + MAP_XML_VERSION + " or older. You may need" + " to install a newer version of JDK."); NodeList entries = xmlMap.getChildNodes(); for (int i=0, numEntries=entries.getLength(); i<numEntries; i++) { Element entry = (Element) entries.item(i); m.put(entry.getAttribute("key"), entry.getAttribute("value")); } } catch(SAXException e) { throw new InvalidPreferencesFormatException(e); } }
protected Preferences getPreferences() throws IOException { if(this.prefs==null) { InputStream is=null; try { is=openPreferences(); Preferences.importPreferences(is); } catch(InvalidPreferencesFormatException er) { throw new IOException(er); } finally { CloserUtil.close(is); } this.prefs=Preferences.userRoot(); } return this.prefs; }
private void readPreferences() throws InvalidPreferencesFormatException, IOException { // Begin starting tag skipTill("<preferences"); readRoot(); // Ending tag skipTill("</preferences>"); }
private void readNodes(Preferences node) throws InvalidPreferencesFormatException, IOException { while ("node".equals(nextTag())) { skipTill("<node"); skipTill("name=\""); String name = readTill("\""); Preferences subnode = node.node(name); readMap(subnode); readNodes(subnode); skipTill("</node>"); } }
private void readEntries(Preferences node) throws InvalidPreferencesFormatException, IOException { while ("entry".equals(nextTag())) { skipTill("<entry"); skipTill("key=\""); String key = readTill("\""); skipTill("value=\""); String value = readTill("\""); node.put(key, value); } }
private String readTill(String s) throws InvalidPreferencesFormatException { int index = line.indexOf(s); if (index == -1) throw new InvalidPreferencesFormatException(s + " not found"); String read = line.substring(0, index); line = line.substring(index+s.length()); return read; }
@RequestMapping(value = "/", method = RequestMethod.POST, consumes = {MediaType.TEXT_XML_VALUE, MediaType.APPLICATION_XML_VALUE}) @ResponseBody ResponseEntity<Void> importPreferences(InputStream in) throws IOException, InvalidPreferencesFormatException { logger.info("Importing preferences from file..."); Preferences.importPreferences(in); logger.info("Preferences import succeeded"); return seeOtherResponse("/"); }
public static void applyPreferencesFrom(final String path, final Map<String, Object> modelValues) { // System.out.println("Apply preferences from " + path); try { final FileInputStream is = new FileInputStream(path); store.importPreferences(is); is.close(); reloadPreferences(modelValues); } catch (final IOException | InvalidPreferencesFormatException e) { e.printStackTrace(); } }
@TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "InvalidPreferencesFormatException", args = {java.lang.String.class} ) public void testInvalidPreferencesFormatExceptionString() { InvalidPreferencesFormatException e = new InvalidPreferencesFormatException( "msg"); assertNull(e.getCause()); assertEquals("msg", e.getMessage()); }
@TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "InvalidPreferencesFormatException", args = {java.lang.String.class, java.lang.Throwable.class} ) public void testInvalidPreferencesFormatExceptionStringThrowable() { Throwable t = new Throwable("root"); InvalidPreferencesFormatException e = new InvalidPreferencesFormatException( "msg", t); assertSame(t, e.getCause()); assertTrue(e.getMessage().indexOf("root") < 0); assertTrue(e.getMessage().indexOf(t.getClass().getName()) < 0); assertTrue(e.getMessage().indexOf("msg") >= 0); }
@TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "InvalidPreferencesFormatException", args = {java.lang.Throwable.class} ) public void testInvalidPreferencesFormatExceptionThrowable() { Throwable t = new Throwable("root"); InvalidPreferencesFormatException e = new InvalidPreferencesFormatException( t); assertSame(t, e.getCause()); assertTrue(e.getMessage().indexOf("root") >= 0); assertTrue(e.getMessage().indexOf(t.getClass().getName()) >= 0); }
/** * @tests serialization/deserialization. */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Verifies serialization", method = "!SerializationSelf", args = {} ) public void testSerializationSelf() throws Exception { SerializationTest.verifySelf(new InvalidPreferencesFormatException( "msg")); }
/** * @tests serialization/deserialization compatibility with RI. */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Verifies serialization", method = "!SerializationGolden", args = {} ) public void testSerializationCompatibility() throws Exception { SerializationTest.verifyGolden(this, new InvalidPreferencesFormatException("msg")); }
public void testInvalidPreferencesFormatExceptionStringThrowable() { Throwable t = new Throwable("root"); InvalidPreferencesFormatException e = new InvalidPreferencesFormatException( "msg", t); assertSame(t, e.getCause()); assertTrue(e.getMessage().indexOf("root") < 0); assertTrue(e.getMessage().indexOf(t.getClass().getName()) < 0); assertTrue(e.getMessage().indexOf("msg") >= 0); }
public void testInvalidPreferencesFormatExceptionThrowable() { Throwable t = new Throwable("root"); InvalidPreferencesFormatException e = new InvalidPreferencesFormatException( t); assertSame(t, e.getCause()); assertTrue(e.getMessage().indexOf("root") >= 0); assertTrue(e.getMessage().indexOf(t.getClass().getName()) >= 0); }