/* * [Persist.java] * * Summary: Save and Restore config in registry via Preferences API. * * Copyright: (c) 2006-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2010-01-17 initial release. */ package com.mindprod.htmlreflow; import java.util.prefs.BackingStoreException; import java.util.prefs.Preferences; /** * Save and Restore config in registry via Preferences API. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2010-01-17 initial release. * @since 2010-01-17 */ class Persist { /** * sub node for the tag descriptions. */ private static final Preferences persistedTags = Preferences.userNodeForPackage( HtmlReflow.class ).node( "tags" ); /** * where in registry we persist our history */ private static final Preferences persistence = Preferences.userNodeForPackage( HtmlReflow.class ); /** * reflow line length (Unbreakable lines may be considerably shorter or longer.) */ private static int desiredLineLength; /** * how to end each line CrLf Lf Cr */ private static String lineSeparator; /** * maximum line length if line longer than this, we break at attributes. */ private static int maxLineLength; /** * lookup table so we can get information on how to do NLs and indents for a given */ private static TagConfig[] tags; /** * restore the configuration from the registry via the Preferences API * * @throws java.util.prefs.BackingStoreException on any problem with persistence */ static void restoreConfig() throws BackingStoreException { long expectedSerialVersionUID = persistence.getLong( "version", 0 ); if ( expectedSerialVersionUID != TagConfig.serialVersionUID ) { throw new BackingStoreException( "Configuration out of date. Please rerun Configure utility." ); } // all keys must be present or there is a program bug. desiredLineLength = persistence.getInt( "desiredLineLength", 0 ); maxLineLength = persistence.getInt( "maxLineLength", 0 ); lineSeparator = persistence.get( "lineSeparator", null ); if ( desiredLineLength < 10 || maxLineLength < 10 || lineSeparator == null ) { throw new BackingStoreException( "Configuration corrupted. Please rerun Configure utility." ); } final String[] keys = persistedTags.keys(); tags = new TagConfig[ keys.length ]; int i = 0; for ( String key : keys ) { final byte[] byteValues = persistedTags.getByteArray( key, null ); if ( byteValues == null ) { throw new BackingStoreException( "Configuration corrupted. Please rerun Configure utility." ); } tags[ i++ ] = new TagConfig( key, byteValues ); } // end for } /** * save the configuration into the registry via the Preferences API * * @throws java.util.prefs.BackingStoreException on any problem with persistence */ static void saveConfig() throws BackingStoreException { // must save config as key=value pairs with unique keys. persistence.clear(); persistence.putLong( "version", TagConfig.serialVersionUID ); persistence.putInt( "desiredLineLength", desiredLineLength ); persistence.putInt( "maxLineLength", maxLineLength ); persistence.put( "lineSeparator", lineSeparator ); for ( TagConfig tag : tags ) { persistedTags.putByteArray( tag.getTagName(), tag.getByteValues() ); } persistence.flush(); } /** * get the desiredLineLength * * @return line length to aim for in reflowing */ public static int getDesiredLineLength() { return desiredLineLength; } /** * set the desiredLineLength * * @param desiredLineLength line length to aim for in reflowing */ public static void setDesiredLineLength( final int desiredLineLength ) { Persist.desiredLineLength = desiredLineLength; } /** * get the lineSeparator string * * @return \r\n \n \r */ public static String getLineSeparator() { return lineSeparator; } /** * set the lineSeparator string * * @param lineSeparator \r\n \n \r */ public static void setLineSeparator( final String lineSeparator ) { Persist.lineSeparator = lineSeparator; } /** * get the maxLineLength * * @return maximum line length if line longer than this, we break at attributes. */ public static int getMaxLineLength() { return desiredLineLength; } /** * set the maxLineLength * * @param maxLineLength if line longer than this, we break at attributes. */ public static void setMaxLineLength( final int maxLineLength ) { Persist.maxLineLength = maxLineLength; } /** * get the configuration for all the tags * * @return tags array of all the tag configurations */ public static TagConfig[] getTags() { return tags; } /** * set the configuration for all the tags * * @param tags array of all the tag configurations */ public static void setTags( final TagConfig[] tags ) { Persist.tags = tags; } }