http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2c3a7cb5/com.ibm.team.juno/src/main/java/org/apache/juneau/html/dto/temp.txt ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno/src/main/java/org/apache/juneau/html/dto/temp.txt b/com.ibm.team.juno/src/main/java/org/apache/juneau/html/dto/temp.txt deleted file mode 100644 index cd769ed..0000000 --- a/com.ibm.team.juno/src/main/java/org/apache/juneau/html/dto/temp.txt +++ /dev/null @@ -1,142 +0,0 @@ -â a â hyperlink CHANGED -â abbr â abbreviation -â address â contact information -â area â image-map hyperlink -â article â article NEW -â aside â tangential content NEW -â audio â audio stream NEW -â b â offset text conventionally styled in bold CHANGED -â base â base URL -â bdi â BiDi isolate NEW -â bdo â BiDi override -â blockquote â block quotation -â body â document body -â br â line break -â button â button -â button type=submit â submit button -â button type=reset â reset button -â button type=button â button with no additional semantics -â canvas â canvas for dynamic graphics NEW -â caption â table title -â cite â cited title of a work CHANGED -â code â code fragment -â col â table column -â colgroup â table column group -â command â command NEW -â command type=command â command with an associated action NEW -â command type=radio â selection of one item from a list of items NEW -â command type=checkbox â state or option that can be toggled NEW -â datalist â predefined options for other controls NEW -â dd â description or value -â del â deleted text -â details â control for additional on-demand information NEW -â dfn â defining instance -â div â generic flow container -â dl â description list -â dt â term or name -â em â emphatic stress -â embed â integration point for plugins NEW -â fieldset â set of related form controls -â figcaption â figure caption NEW -â figure â figure with optional caption NEW -â footer â footer NEW -â form â user-submittable form -â h1 â heading -â h2 â heading -â h3 â heading -â h4 â heading -â h5 â heading -â h6 â heading -â head â document metadata container -â header â header NEW -â hgroup â heading group NEW -â hr â thematic break CHANGED -â html â root element -â i â offset text conventionally styled in italic CHANGED -â iframe â nested browsing context (inline frame) -â img â image -â input â input control CHANGED -â input type=text â text-input field -â input type=password â password-input field -â input type=checkbox â checkbox -â input type=radio â radio button -â input type=button â button -â input type=submit â submit button -â input type=reset â reset button -â input type=file â file upload control -â input type=hidden â hidden input control -â input type=image â image-coordinates input control -â input type=datetime â global date-and-time input control NEW -â input type=datetime-local â local date-and-time input control NEW -â input type=date â date input control NEW -â input type=month â year-and-month input control NEW -â input type=time â time input control NEW -â input type=week â year-and-week input control NEW -â input type=number â number input control NEW -â input type=range â imprecise number-input control NEW -â input type=email â e-mail address input control NEW -â input type=url â URL input control NEW -â input type=search â search field NEW -â input type=tel â telephone-number-input field NEW -â input type=color â color-well control NEW -â ins â inserted text -â kbd â user input -â keygen â key-pair generator/input control NEW -â label â caption for a form control -â legend â title or explanatory caption -â li â list item -â link â inter-document relationship metadata -â map â image-map definition -â mark â marked (highlighted) text NEW -â menu â list of commands CHANGED -â meta â metadata CHANGED -â meta name â name-value metadata -â meta http-equiv=refresh â ârefreshâ pragma directive -â meta http-equiv=default-style â âpreferred stylesheetâ pragma directive -â meta charset â document character-encoding declaration NEW -â meta http-equiv=content-type â document character-encoding declaration -â meter â scalar gauge NEW -â nav â group of navigational links NEW -â noscript â fallback content for script -â object â generic external content -â ol â ordered list -â optgroup â group of options -â option â option -â output â result of a calculation in a form NEW -â p â paragraph -â param â initialization parameters for plugins -â pre â preformatted text -â progress â progress indicator NEW -â q â quoted text -â rp â ruby parenthesis NEW -â rt â ruby text NEW -â ruby â ruby annotation NEW -â s â struck text CHANGED -â samp â (sample) output -â script â embedded script -â section â section NEW -â select â option-selection form control -â small â small print CHANGED -â source â media source NEW -â span â generic span -â strong â strong importance -â style â style (presentation) information -â sub â subscript -â summary â summary, caption, or legend for a details control NEW -â sup â superscript -â table â table -â tbody â table row group -â td â table cell -â textarea â text input area -â tfoot â table footer row group -â th â table header cell -â thead â table heading group -â time â date and/or time NEW -â title â document title -â tr â table row -â track â supplementary media track NEW -â u â offset text conventionally styled with an underline CHANGED -â ul â unordered list -â var â variable or placeholder text -â video â video NEW -â wbr â line-break opportunity NEW \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2c3a7cb5/com.ibm.team.juno/src/main/java/org/apache/juneau/html/package.html ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno/src/main/java/org/apache/juneau/html/package.html b/com.ibm.team.juno/src/main/java/org/apache/juneau/html/package.html deleted file mode 100644 index 371c8c4..0000000 --- a/com.ibm.team.juno/src/main/java/org/apache/juneau/html/package.html +++ /dev/null @@ -1,79 +0,0 @@ -<!DOCTYPE HTML> -<!-- -/*************************************************************************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - * - ***************************************************************************************************************************/ - --> -<html> -<head> - <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> - <style type="text/css"> - /* For viewing in Page Designer */ - @IMPORT url("../../../../../../javadoc.css"); - - /* For viewing in REST interface */ - @IMPORT url("../htdocs/javadoc.css"); - body { - margin: 20px; - } - </style> - <script> - /* Replace all @code and @link tags. */ - window.onload = function() { - document.body.innerHTML = document.body.innerHTML.replace(/\{\@code ([^\}]+)\}/g, '<code>$1</code>'); - document.body.innerHTML = document.body.innerHTML.replace(/\{\@link (([^\}]+)\.)?([^\.\}]+)\}/g, '<code>$3</code>'); - } - </script> -</head> -<body> -<p>HTML serialization and parsing support</p> -<script> - function toggle(x) { - var div = x.nextSibling; - while (div != null && div.nodeType != 1) - div = div.nextSibling; - if (div != null) { - var d = div.style.display; - if (d == 'block' || d == '') { - div.style.display = 'none'; - x.className += " closed"; - } else { - div.style.display = 'block'; - x.className = x.className.replace(/(?:^|\s)closed(?!\S)/g , '' ); - } - } - } -</script> - -<a id='TOC'></a><h5 class='toc'>Table of Contents</h5> -<ol class='toc'> - <li><p><a class='doclink' href='#HtmlSerializer'>HTML serialization support</a></p> - <li><p><a class='doclink' href='#HtmlParser'>HTML parsing support</a></p> -</ol> - -<!-- ======================================================================================================== --> -<a id="HtmlSerializer"></a> -<h2 class='topic' onclick='toggle(this)'>1 - HTML serialization support</h2> -<div class='topic'> - TODO -</div> - -<!-- ======================================================================================================== --> -<a id="HtmlParser"></a> -<h2 class='topic' onclick='toggle(this)'>2 - HTML parsing support</h2> -<div class='topic'> - TODO -</div> - -</body> -</html> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2c3a7cb5/com.ibm.team.juno/src/main/java/org/apache/juneau/ini/ConfigFile.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno/src/main/java/org/apache/juneau/ini/ConfigFile.java b/com.ibm.team.juno/src/main/java/org/apache/juneau/ini/ConfigFile.java deleted file mode 100644 index 08e78ec..0000000 --- a/com.ibm.team.juno/src/main/java/org/apache/juneau/ini/ConfigFile.java +++ /dev/null @@ -1,766 +0,0 @@ -/*************************************************************************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - ***************************************************************************************************************************/ -package org.apache.juneau.ini; - -import static java.lang.reflect.Modifier.*; -import static org.apache.juneau.ini.ConfigFileFormat.*; -import static org.apache.juneau.ini.ConfigUtils.*; -import static org.apache.juneau.internal.ThrowableUtils.*; - -import java.beans.*; -import java.io.*; -import java.lang.reflect.*; -import java.util.*; - -import org.apache.juneau.*; -import org.apache.juneau.internal.*; -import org.apache.juneau.json.*; -import org.apache.juneau.parser.*; -import org.apache.juneau.serializer.*; -import org.apache.juneau.svl.*; - -/** - * Implements the API for accessing the contents of a config file. - * <p> - * Refer to {@link org.apache.juneau.ini} for more information. - * - * @author James Bognar ([email protected]) - */ -public abstract class ConfigFile implements Map<String,Section> { - - //-------------------------------------------------------------------------------- - // Abstract methods - //-------------------------------------------------------------------------------- - - /** - * Retrieves an entry value from this config file. - * - * @param sectionName The section name. Must not be <jk>null</jk>. - * @param sectionKey The section key. Must not be <jk>null</jk>. - * @return The value, or the default value if the section or value doesn't exist. - */ - public abstract String get(String sectionName, String sectionKey); - - /** - * Sets an entry value in this config file. - * - * @param sectionName The section name. Must not be <jk>null</jk>. - * @param sectionKey The section key. Must not be <jk>null</jk>. - * @param value The new value. - * @param encoded - * @return The previous value, or <jk>null</jk> if the section or key did not previously exist. - * @throws UnsupportedOperationException If config file is read only. - */ - public abstract String put(String sectionName, String sectionKey, Object value, boolean encoded); - - /** - * Removes an antry from this config file. - * - * @param sectionName The section name. Must not be <jk>null</jk>. - * @param sectionKey The section key. Must not be <jk>null</jk>. - * @return The previous value, or <jk>null</jk> if the section or key did not previously exist. - * @throws UnsupportedOperationException If config file is read only. - */ - public abstract String remove(String sectionName, String sectionKey); - - /** - * Returns the current set of keys in the specified section. - * - * @param sectionName The section name. Must not be <jk>null</jk>. - * @return The list of keys in the specified section, or <jk>null</jk> if section does not exist. - * @throws UnsupportedOperationException If config file is read only. - */ - public abstract Set<String> getSectionKeys(String sectionName); - - /** - * Reloads ths config file object from the persisted file contents if the modified timestamp on the file has changed. - * - * @return This object (for method chaining). - * @throws IOException If file could not be read, or file is not associated with this object. - * @throws UnsupportedOperationException If config file is read only. - */ - public abstract ConfigFile loadIfModified() throws IOException; - - /** - * Loads ths config file object from the persisted file contents. - * - * @return This object (for method chaining). - * @throws IOException If file could not be read, or file is not associated with this object. - * @throws UnsupportedOperationException If config file is read only. - */ - public abstract ConfigFile load() throws IOException; - - /** - * Loads ths config file object from the specified reader. - * - * @param r The reader to read from. - * @return This object (for method chaining). - * @throws IOException If file could not be read, or file is not associated with this object. - * @throws UnsupportedOperationException If config file is read only. - */ - public abstract ConfigFile load(Reader r) throws IOException; - - /** - * Adds arbitrary lines to the specified config file section. - * <p> - * The lines can be any of the following.... - * <ul class='spaced-list'> - * <li><js>"# comment"</js> - A comment line. - * <li><js>"key=val"</js> - A key/value pair (equivalent to calling {@link #put(String,Object)}. - * <li><js>" foobar "</js> - Anything else (interpreted as a comment). - * </ul> - * <p> - * If the section does not exist, it will automatically be created. - * - * @param section The name of the section to add lines to, or <jk>null</jk> to add to the beginning unnamed section. - * @param lines The lines to add to the section. - * @return This object (for method chaining). - * @throws UnsupportedOperationException If config file is read only. - */ - public abstract ConfigFile addLines(String section, String...lines); - - /** - * Adds header comments to the specified section. - * <p> - * Header comments are defined as lines that start with <jk>"#"</jk> immediately preceding a section header <jk>"[section]"</jk>. - * These are handled as part of the section itself instead of being interpreted as comments in the previous section. - * <p> - * Header comments can be of the following formats... - * <ul class='spaced-list'> - * <li><js>"# comment"</js> - A comment line. - * <li><js>"comment"</js> - Anything else (will automatically be prefixed with <js>"# "</js>). - * </ul> - * <p> - * If the section does not exist, it will automatically be created. - * - * @param section The name of the section to add lines to, or <jk>null</jk> to add to the default section. - * @param headerComments The comment lines to add to the section. - * @return This object (for method chaining). - * @throws UnsupportedOperationException If config file is read only. - */ - public abstract ConfigFile addHeaderComments(String section, String...headerComments); - - /** - * Removes any header comments from the specified section. - * - * @param section The name of the section to remove header comments from. - * @return This object (for method chaining). - * @throws UnsupportedOperationException If config file is read only. - */ - public abstract ConfigFile clearHeaderComments(String section); - - /** - * Returns the serializer in use for this config file. - * - * @return This object (for method chaining). - * @throws SerializeException If no serializer is defined on this config file. - */ - protected abstract WriterSerializer getSerializer() throws SerializeException; - - /** - * Returns the parser in use for this config file. - * - * @return This object (for method chaining). - * @throws ParseException If no parser is defined on this config file. - */ - protected abstract ReaderParser getParser() throws ParseException; - - /** - * Places a read lock on this config file. - */ - protected abstract void readLock(); - - /** - * Removes the read lock on this config file. - */ - protected abstract void readUnlock(); - - - //-------------------------------------------------------------------------------- - // API methods - //-------------------------------------------------------------------------------- - - /** - * Returns the specified value as a string from the config file. - * - * @param key The key. See {@link #getString(String)} for a description of the key. - * @param def The default value if the section or value does not exist. - * @return The value, or the default value if the section or value doesn't exist. - */ - public final String getString(String key, String def) { - assertFieldNotNull(key, "key"); - String s = get(getSectionName(key), getSectionKey(key)); - return (StringUtils.isEmpty(s) && def != null ? def : s); - } - - /** - * Removes an entry with the specified key. - * - * @param key The key. See {@link #getString(String)} for a description of the key. - * @return The previous value, or <jk>null</jk> if the section or key did not previously exist. - * @throws UnsupportedOperationException If config file is read only. - */ - public final String removeString(String key) { - assertFieldNotNull(key, "key"); - return remove(getSectionName(key), getSectionKey(key)); - } - - /** - * Gets the entry with the specified key and converts it to the specified value. - * <p> - * The key can be in one of the following formats... - * <ul class='spaced-list'> - * <li><js>"key"</js> - A value in the default section (i.e. defined above any <code>[section]</code> header). - * <li><js>"section/key"</js> - A value from the specified section. - * </ul> - * <p> - * If the class type is an array, the value is split on commas and converted individually. - * <p> - * If you specify a primitive element type using this method (e.g. <code><jk>int</jk>.<jk>class</jk></code>, - * you will get an array of wrapped objects (e.g. <code>Integer[].<jk>class</jk></code>. - * - * @param c The class to convert the value to. - * @param key The key. See {@link #getString(String)} for a description of the key. - * @throws ParseException If parser could not parse the value or if a parser is not registered with this config file. - * @return The value, or <jk>null</jk> if the section or key does not exist. - */ - @SuppressWarnings("unchecked") - public final <T> T getObject(Class<T> c, String key) throws ParseException { - assertFieldNotNull(c, "c"); - return getObject(c, key, c.isArray() ? (T)Array.newInstance(c.getComponentType(), 0) : null); - } - - /** - * Gets the entry with the specified key and converts it to the specified value.. - * <p> - * The key can be in one of the following formats... - * <ul class='spaced-list'> - * <li><js>"key"</js> - A value in the default section (i.e. defined above any <code>[section]</code> header). - * <li><js>"section/key"</js> - A value from the specified section. - * </ul> - * - * @param c The class to convert the value to. - * @param key The key. See {@link #getString(String)} for a description of the key. - * @param def The default value if section or key does not exist. - * @throws ParseException If parser could not parse the value or if a parser is not registered with this config file. - * @return The value, or <jk>null</jk> if the section or key does not exist. - */ - public final <T> T getObject(Class<T> c, String key, T def) throws ParseException { - assertFieldNotNull(c, "c"); - assertFieldNotNull(key, "key"); - return getObject(c, getSectionName(key), getSectionKey(key), def); - } - - /** - * Same as {@link #getObject(Class, String, Object)}, but value is referenced through section name and key instead of full key. - * - * @param c The class to convert the value to. - * @param sectionName The section name. Must not be <jk>null</jk>. - * @param sectionKey The section key. Must not be <jk>null</jk>. - * @param def The default value if section or key does not exist. - * @throws ParseException If parser could not parse the value or if a parser is not registered with this config file. - * @return The value, or the default value if the section or value doesn't exist. - */ - @SuppressWarnings("unchecked") - public <T> T getObject(Class<T> c, String sectionName, String sectionKey, T def) throws ParseException { - String s = get(sectionName, sectionKey); - if (s == null) - return def; - if (c == String.class) - return (T)s; - if (c == Integer.class || c == int.class) - return (T)(StringUtils.isEmpty(s) ? def : Integer.valueOf(parseIntWithSuffix(s))); - if (c == Boolean.class || c == boolean.class) - return (T)(StringUtils.isEmpty(s) ? def : Boolean.valueOf(Boolean.parseBoolean(s))); - if (c == String[].class) { - String[] r = StringUtils.isEmpty(s) ? new String[0] : StringUtils.split(s, ','); - return (T)(r.length == 0 ? def : r); - } - if (c.isArray()) { - Class<?> ce = c.getComponentType(); - if (StringUtils.isEmpty(s)) - return def; - String[] r = StringUtils.split(s, ','); - Object o = Array.newInstance(ce, r.length); - for (int i = 0; i < r.length; i++) - Array.set(o, i, getParser().parse(r[i], ce)); - return (T)o; - } - if (StringUtils.isEmpty(s)) - return def; - return getParser().parse(s, c); - } - - /** - * Gets the entry with the specified key. - * <p> - * The key can be in one of the following formats... - * <ul class='spaced-list'> - * <li><js>"key"</js> - A value in the default section (i.e. defined above any <code>[section]</code> header). - * <li><js>"section/key"</js> - A value from the specified section. - * </ul> - * - * @param key The key. See {@link #getString(String)} for a description of the key. - * @return The value, or <jk>null</jk> if the section or key does not exist. - */ - public final String getString(String key) { - return getString(key, null); - } - - /** - * Gets the entry with the specified key, splits the value on commas, and returns the values as trimmed strings. - * - * @param key The key. See {@link #getString(String)} for a description of the key. - * @return The value, or an empty list if the section or key does not exist. - */ - public final String[] getStringArray(String key) { - return getStringArray(key, new String[0]); - } - - /** - * Same as {@link #getStringArray(String)} but returns a default value if the value cannot be found. - * - * @param key The key. See {@link #getString(String)} for a description of the key. - * @param def The default value if section or key does not exist. - * @return The value, or an empty list if the section or key does not exist. - */ - public final String[] getStringArray(String key, String[] def) { - String s = getString(key); - if (s == null) - return def; - String[] r = StringUtils.isEmpty(s) ? new String[0] : StringUtils.split(s, ','); - return r.length == 0 ? def : r; - } - - /** - * Convenience method for getting int config values. - * - * @param key The key. See {@link #getString(String)} for a description of the key. - * @return The value, or <code>0</code> if the section or key does not exist or cannot be parsed as an integer. - */ - public final int getInt(String key) { - return getInt(key, 0); - } - - /** - * Convenience method for getting int config values. - * <p> - * <js>"M"</js> and <js>"K"</js> can be used to identify millions and thousands. - * - * <dl> - * <dt>Example:</dt> - * <dd> - * <ul class='spaced-list'> - * <li><code><js>"100K"</js> => 1024000</code> - * <li><code><js>"100M"</js> => 104857600</code> - * </ul> - * </dd> - * </dl> - * - * @param key The key. See {@link #getString(String)} for a description of the key. - * @param def The default value if config file or value does not exist. - * @return The value, or the default value if the section or key does not exist or cannot be parsed as an integer. - */ - public final int getInt(String key, int def) { - String s = getString(key); - if (StringUtils.isEmpty(s)) - return def; - return parseIntWithSuffix(s); - } - - /** - * Convenience method for getting boolean config values. - * - * @param key The key. See {@link #getString(String)} for a description of the key. - * @return The value, or <jk>false</jk> if the section or key does not exist or cannot be parsed as a boolean. - */ - public final boolean getBoolean(String key) { - return getBoolean(key, false); - } - - /** - * Convenience method for getting boolean config values. - * - * @param key The key. See {@link #getString(String)} for a description of the key. - * @param def The default value if config file or value does not exist. - * @return The value, or the default value if the section or key does not exist or cannot be parsed as a boolean. - */ - public final boolean getBoolean(String key, boolean def) { - String s = getString(key); - return StringUtils.isEmpty(s) ? def : Boolean.parseBoolean(s); - } - - /** - * Adds or replaces an entry with the specified key with a POJO serialized to a string using the registered serializer. - * <p> - * Equivalent to calling <code>put(key, value, isEncoded(key))</code>. - * - * @param key The key. See {@link #getString(String)} for a description of the key. - * @param value The new value POJO. - * @return The previous value, or <jk>null</jk> if the section or key did not previously exist. - * @throws SerializeException If serializer could not serialize the value or if a serializer is not registered with this config file. - * @throws UnsupportedOperationException If config file is read only. - */ - public final String put(String key, Object value) throws SerializeException { - return put(key, value, isEncoded(key)); - } - - /** - * Adds or replaces an entry with the specified key with the specified value. - * <p> - * The format of the entry depends on the data type of the value. - * <ul class='spaced-list'> - * <li>Simple types (<code>String</code>, <code>Number</code>, <code>Boolean</code>, primitives) - * are serialized as plain strings. - * <li>Arrays and collections of simple types are serialized as comma-delimited lists of plain strings. - * <li>Other types (e.g. beans) are serialized using the serializer registered with this config file. - * <li>Arrays and collections of other types are serialized as comma-delimited lists of serialized strings of each entry. - * </ul> - * - * @param key The key. See {@link #getString(String)} for a description of the key. - * @param value The new value. - * @param encoded If <jk>true</jk>, value is encoded by the registered encoder when the config file is persisted to disk. - * @return The previous value, or <jk>null</jk> if the section or key did not previously exist. - * @throws SerializeException If serializer could not serialize the value or if a serializer is not registered with this config file. - * @throws UnsupportedOperationException If config file is read only. - */ - public final String put(String key, Object value, boolean encoded) throws SerializeException { - assertFieldNotNull(key, "key"); - if (value == null) - value = ""; - Class<?> c = value.getClass(); - if (isSimpleType(c)) - return put(getSectionName(key), getSectionKey(key), value.toString(), encoded); - if (c.isAssignableFrom(Collection.class)) { - Collection<?> c2 = (Collection<?>)value; - String[] r = new String[c2.size()]; - int i = 0; - for (Object o2 : c2) { - boolean isSimpleType = o2 == null ? true : isSimpleType(o2.getClass()); - r[i++] = (isSimpleType ? Array.get(value, i).toString() : getSerializer().toString(Array.get(value, i))); - } - return put(getSectionName(key), getSectionKey(key), StringUtils.join(r, ','), encoded); - } else if (c.isArray()) { - boolean isSimpleType = isSimpleType(c.getComponentType()); - String[] r = new String[Array.getLength(value)]; - for (int i = 0; i < r.length; i++) { - r[i] = (isSimpleType ? Array.get(value, i).toString() : getSerializer().toString(Array.get(value, i))); - } - return put(getSectionName(key), getSectionKey(key), StringUtils.join(r, ','), encoded); - } - return put(getSectionName(key), getSectionKey(key), getSerializer().toString(value), encoded); - } - - private final boolean isSimpleType(Class<?> c) { - return (c == String.class || c.isPrimitive() || c.isAssignableFrom(Number.class) || c == Boolean.class); - } - - /** - * Returns the specified section as a map of key/value pairs. - * - * @param sectionName The section name to retrieve. - * @return A map of the section, or <jk>null</jk> if the section was not found. - */ - public final ObjectMap getSectionMap(String sectionName) { - readLock(); - try { - Set<String> keys = getSectionKeys(sectionName); - if (keys == null) - return null; - ObjectMap m = new ObjectMap(); - for (String key : keys) - m.put(key, get(sectionName, key)); - return m; - } finally { - readUnlock(); - } - } - - /** - * Copies the entries in a section to the specified bean by calling the public setters on that bean. - * - * @param sectionName The section name to write from. - * @param bean The bean to set the properties on. - * @param ignoreUnknownProperties If <jk>true</jk>, don't throw an {@link IllegalArgumentException} if this section - * contains a key that doesn't correspond to a setter method. - * @param permittedPropertyTypes If specified, only look for setters whose property types - * are those listed. If not specified, use all setters. - * @return An object map of the changes made to the bean. - * @throws ParseException If parser was not set on this config file or invalid properties were found in the section. - * @throws IllegalArgumentException - * @throws IllegalAccessException - * @throws InvocationTargetException - */ - public final ObjectMap writeProperties(String sectionName, Object bean, boolean ignoreUnknownProperties, Class<?>...permittedPropertyTypes) throws ParseException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { - assertFieldNotNull(bean, "bean"); - ObjectMap om = new ObjectMap(); - readLock(); - try { - Set<String> keys = getSectionKeys(sectionName); - if (keys == null) - throw new IllegalArgumentException("Section not found"); - keys = new LinkedHashSet<String>(keys); - for (Method m : bean.getClass().getMethods()) { - int mod = m.getModifiers(); - if (isPublic(mod) && (!isStatic(mod)) && m.getName().startsWith("set") && m.getParameterTypes().length == 1) { - Class<?> pt = m.getParameterTypes()[0]; - if (permittedPropertyTypes == null || permittedPropertyTypes.length == 0 || ArrayUtils.contains(pt, permittedPropertyTypes)) { - String propName = Introspector.decapitalize(m.getName().substring(3)); - Object value = getObject(pt, sectionName, propName, null); - if (value != null) { - m.invoke(bean, value); - om.put(propName, value); - keys.remove(propName); - } - } - } - } - if (! (ignoreUnknownProperties || keys.isEmpty())) - throw new ParseException("Invalid properties found in config file section ["+sectionName+"]: " + JsonSerializer.DEFAULT_LAX.toString(keys)); - return om; - } finally { - readUnlock(); - } - } - - /** - * Shortcut for calling <code>asBean(sectionName, c, <jk>false</jk>)</code>. - * - * @param sectionName The section name to write from. - * @param c The bean class to create. - * @return A new bean instance. - * @throws ParseException - */ - public final <T> T getSectionAsBean(String sectionName, Class<T>c) throws ParseException { - return getSectionAsBean(sectionName, c, false); - } - - /** - * Converts this config file section to the specified bean instance. - * - * @param sectionName The section name to write from. - * @param c The bean class to create. - * @param ignoreUnknownProperties If <jk>false</jk>, throws a {@link ParseException} if - * the section contains an entry that isn't a bean property name. - * @return A new bean instance. - * @throws ParseException - */ - public final <T> T getSectionAsBean(String sectionName, Class<T> c, boolean ignoreUnknownProperties) throws ParseException { - assertFieldNotNull(c, "c"); - readLock(); - try { - BeanMap<T> bm = getParser().getBeanContext().newBeanMap(c); - for (String k : getSectionKeys(sectionName)) { - BeanPropertyMeta<?> bpm = bm.getPropertyMeta(k); - if (bpm == null) { - if (! ignoreUnknownProperties) - throw new ParseException("Unknown property {0} encountered", k); - } else { - bm.put(k, getObject(bpm.getClassMeta().getInnerClass(), sectionName + '/' + k)); - } - } - return bm.getBean(); - } finally { - readUnlock(); - } - } - - /** - * Returns <jk>true</jk> if this section contains the specified key and the key has a non-blank value. - * - * @param key The key. See {@link #getString(String)} for a description of the key. - * @return <jk>true</jk> if this section contains the specified key and the key has a non-blank value. - */ - public final boolean containsNonEmptyValue(String key) { - return ! StringUtils.isEmpty(getString(key, null)); - } - - /** - * Gets the section with the specified name. - * - * @param name The section name. - * @return The section, or <jk>null</jk> if section does not exist. - */ - protected abstract Section getSection(String name); - - /** - * Gets the section with the specified name and optionally creates it if it's not there. - * - * @param name The section name. - * @param create Create the section if it's not there. - * @return The section, or <jk>null</jk> if section does not exist. - * @throws UnsupportedOperationException If config file is read only and section doesn't exist and <code>create</code> is <jk>true</jk>. - */ - protected abstract Section getSection(String name, boolean create); - - /** - * Appends a section to this config file if it does not already exist. - * <p> - * Returns the existing section if it already exists. - * - * @param name The section name, or <jk>null</jk> for the default section. - * @return The appended or existing section. - * @throws UnsupportedOperationException If config file is read only. - */ - public abstract ConfigFile addSection(String name); - - /** - * Creates or overwrites the specified section. - * - * @param name The section name, or <jk>null</jk> for the default section. - * @param contents The contents of the new section. - * @return The appended or existing section. - * @throws UnsupportedOperationException If config file is read only. - */ - public abstract ConfigFile setSection(String name, Map<String,String> contents); - - /** - * Removes the section with the specified name. - * - * @param name The name of the section to remove, or <jk>null</jk> for the default section. - * @return The removed section, or <jk>null</jk> if named section does not exist. - * @throws UnsupportedOperationException If config file is read only. - */ - public abstract ConfigFile removeSection(String name); - - /** - * Returns <jk>true</jk> if the encoding flag is set on the specified entry. - * - * @param key The key. See {@link #getString(String)} for a description of the key. - * @return <jk>true</jk> if the encoding flag is set on the specified entry. - */ - public abstract boolean isEncoded(String key); - - /** - * Saves this config file to disk. - * - * @return This object (for method chaining). - * @throws IOException If a problem occurred trying to save file to disk, or file is not associated with this object. - * @throws UnsupportedOperationException If config file is read only. - */ - public abstract ConfigFile save() throws IOException; - - /** - * Saves this config file to the specified writer as an INI file. - * <p> - * The writer will automatically be closed. - * - * @param out The writer to send the output to. - * @return This object (for method chaining). - * @throws IOException If a problem occurred trying to send contents to the writer. - */ - public final ConfigFile serializeTo(Writer out) throws IOException { - return serializeTo(out, INI); - } - - /** - * Same as {@link #serializeTo(Writer)}, except allows you to explicitely specify a format. - * - * @param out The writer to send the output to. - * @param format The {@link ConfigFileFormat} of the output. - * @return This object (for method chaining). - * @throws IOException If a problem occurred trying to send contents to the writer. - */ - public abstract ConfigFile serializeTo(Writer out, ConfigFileFormat format) throws IOException; - - /** - * Add a listener to this config file to react to modification events. - * - * @param listener The new listener to add. - * @return This object (for method chaining). - * @throws UnsupportedOperationException If config file is read only. - */ - public abstract ConfigFile addListener(ConfigFileListener listener); - - /** - * Merges the contents of the specified config file into this config file. - * <p> - * Pretty much identical to just replacing this config file, but - * causes the {@link ConfigFileListener#onChange(ConfigFile, Set)} method to be invoked - * on differences between the file. - * @param cf The config file whose values should be copied into this config file. - * @return This object (for method chaining). - * @throws UnsupportedOperationException If config file is read only. - */ - public abstract ConfigFile merge(ConfigFile cf); - - /** - * Returns the config file contents as a string. - * <p> - * The contents of the string are the same as the contents that would be serialized to disk. - */ - @Override /* Object */ - public abstract String toString(); - - /** - * Returns a wrapped instance of this config file where calls to getters - * have their values first resolved by the specified {@link VarResolver}. - * - * @param vr The {@link VarResolver} for resolving variables in values. - * @return This config file wrapped in an instance of {@link ConfigFileWrapped}. - */ - public abstract ConfigFile getResolving(VarResolver vr); - - /** - * Returns a wrapped instance of this config file where calls to getters - * have their values first resolved by the specified {@link VarResolverSession}. - * - * @param vs The {@link VarResolverSession} for resolving variables in values. - * @return This config file wrapped in an instance of {@link ConfigFileWrapped}. - */ - public abstract ConfigFile getResolving(VarResolverSession vs); - - /** - * Returns a wrapped instance of this config file where calls to getters have their values - * first resolved by a default {@link VarResolver}. - * - * The default {@link VarResolver} is registered with the following {@link Var StringVars}: - * <ul class='spaced-list'> - * <li><code>$S{key}</code>,<code>$S{key,default}</code> - System properties. - * <li><code>$E{key}</code>,<code>$E{key,default}</code> - Environment variables. - * <li><code>$C{key}</code>,<code>$C{key,default}</code> - Values in this configuration file. - * </ul> - * - * @return A new config file that resolves string variables. - */ - public abstract ConfigFile getResolving(); - - /** - * Wraps this config file in a {@link Writable} interface that renders it as plain text. - * - * @return This config file wrapped in a {@link Writable}. - */ - public abstract Writable toWritable(); - - /** - * @return The string var resolver associated with this config file. - */ - protected VarResolver getVarResolver() { - // Only ConfigFileWrapped returns a value. - return null; - } - - - private int parseIntWithSuffix(String s) { - assertFieldNotNull(s, "s"); - int m = 1; - if (s.endsWith("M")) { - m = 1024*1024; - s = s.substring(0, s.length()-1).trim(); - } else if (s.endsWith("K")) { - m = 1024; - s = s.substring(0, s.length()-1).trim(); - } - return Integer.parseInt(s) * m; - } -} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2c3a7cb5/com.ibm.team.juno/src/main/java/org/apache/juneau/ini/ConfigFileFormat.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno/src/main/java/org/apache/juneau/ini/ConfigFileFormat.java b/com.ibm.team.juno/src/main/java/org/apache/juneau/ini/ConfigFileFormat.java deleted file mode 100644 index 821f7d3..0000000 --- a/com.ibm.team.juno/src/main/java/org/apache/juneau/ini/ConfigFileFormat.java +++ /dev/null @@ -1,29 +0,0 @@ -/*************************************************************************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - ***************************************************************************************************************************/ -package org.apache.juneau.ini; - -import java.io.*; - -/** - * Valid formats that can be passed to the {@link ConfigFile#serializeTo(Writer, ConfigFileFormat)} method. - */ -public enum ConfigFileFormat { - /** Normal INI file format*/ - INI, - - /** Batch file with "set X" commands */ - BATCH, - - /** Shell script file with "export X" commands */ - SHELL; -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2c3a7cb5/com.ibm.team.juno/src/main/java/org/apache/juneau/ini/ConfigFileImpl.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno/src/main/java/org/apache/juneau/ini/ConfigFileImpl.java b/com.ibm.team.juno/src/main/java/org/apache/juneau/ini/ConfigFileImpl.java deleted file mode 100644 index 4aaaa46..0000000 --- a/com.ibm.team.juno/src/main/java/org/apache/juneau/ini/ConfigFileImpl.java +++ /dev/null @@ -1,747 +0,0 @@ -/*************************************************************************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - ***************************************************************************************************************************/ -package org.apache.juneau.ini; - -import static org.apache.juneau.ini.ConfigUtils.*; -import static org.apache.juneau.internal.ThrowableUtils.*; - -import java.io.*; -import java.nio.charset.*; -import java.util.*; -import java.util.concurrent.locks.*; - -import org.apache.juneau.*; -import org.apache.juneau.internal.*; -import org.apache.juneau.json.*; -import org.apache.juneau.parser.*; -import org.apache.juneau.serializer.*; -import org.apache.juneau.svl.*; -import org.apache.juneau.svl.vars.*; - -/** - * Implementation class for {@link ConfigFile}. - * - * @author James Bognar ([email protected]) - */ -public final class ConfigFileImpl extends ConfigFile { - - private final File file; - private final Encoder encoder; - private final WriterSerializer serializer; - private final ReaderParser parser; - private final Charset charset; - final List<ConfigFileListener> listeners = Collections.synchronizedList(new ArrayList<ConfigFileListener>()); - - private Map<String,Section> sections; // The actual data. - - private static final String DEFAULT = "default"; - - private final boolean readOnly; - - volatile boolean hasBeenModified = false; - private ReadWriteLock lock = new ReentrantReadWriteLock(); - - long modifiedTimestamp; - - /** - * Constructor. - * <p> - * Loads the contents of the specified file into this config file. - * <p> - * If file does not initially exist, this object will start off empty. - * - * @param file The INI file on disk. - * If <jk>null</jk>, create an in-memory config file. - * @param readOnly Make this configuration file read-only. - * Attempting to set any values on this config file will cause {@link UnsupportedOperationException} to be thrown. - * @param encoder The encoder to use for encoding sensitive values in this configuration file. - * If <jk>null</jk>, defaults to {@link XorEncoder#INSTANCE}. - * @param serializer The serializer to use for serializing POJOs in the {@link #put(String, Object)} method. - * If <jk>null</jk>, defaults to {@link JsonSerializer#DEFAULT}. - * @param parser The parser to use for parsing POJOs in the {@link #getObject(Class,String)} method. - * If <jk>null</jk>, defaults to {@link JsonParser#DEFAULT}. - * @param charset The charset on the files. - * If <jk>null</jk>, defaults to {@link Charset#defaultCharset()}. - * @throws IOException - */ - public ConfigFileImpl(File file, boolean readOnly, Encoder encoder, WriterSerializer serializer, ReaderParser parser, Charset charset) throws IOException { - this.file = file; - this.encoder = encoder == null ? XorEncoder.INSTANCE : encoder; - this.serializer = serializer == null ? JsonSerializer.DEFAULT : serializer; - this.parser = parser == null ? JsonParser.DEFAULT : parser; - this.charset = charset == null ? Charset.defaultCharset() : charset; - load(); - this.readOnly = readOnly; - if (readOnly) { - this.sections = Collections.unmodifiableMap(this.sections); - for (Section s : sections.values()) - s.setReadOnly(); - } - } - - /** - * Constructor. - * Shortcut for calling <code><jk>new</jk> ConfigFileImpl(file, <jk>false</jk>, <jk>null</jk>, <jk>null</jk>, <jk>null</jk>, <jk>null</jk>);</code> - * - * @param file The config file. Does not need to exist. - * @throws IOException - */ - public ConfigFileImpl(File file) throws IOException { - this(file, false, null, null, null, null); - } - - /** - * Constructor. - * Shortcut for calling <code><jk>new</jk> ConfigFileImpl(<jk>null</jk>, <jk>false</jk>, <jk>null</jk>, <jk>null</jk>, <jk>null</jk>, <jk>null</jk>);</code> - * - * @throws IOException - */ - public ConfigFileImpl() throws IOException { - this(null); - } - - @Override /* ConfigFile */ - public ConfigFileImpl loadIfModified() throws IOException { - if (file == null) - return this; - writeLock(); - try { - if (file.lastModified() > modifiedTimestamp) - load(); - } finally { - writeUnlock(); - } - return this; - } - - @Override /* ConfigFile */ - public ConfigFileImpl load() throws IOException { - Reader r = null; - if (file != null && file.exists()) - r = new InputStreamReader(new FileInputStream(file), charset); - else - r = new StringReader(""); - try { - load(r); - } finally { - r.close(); - } - return this; - } - - @Override /* ConfigFile */ - public ConfigFileImpl load(Reader r) throws IOException { - assertFieldNotNull(r, "r"); - writeLock(); - try { - this.sections = Collections.synchronizedMap(new LinkedHashMap<String,Section>()); - BufferedReader in = new BufferedReader(r); - try { - writeLock(); - hasBeenModified = false; - try { - sections.clear(); - String line = null; - Section section = getSection(null, true); - ArrayList<String> lines = new ArrayList<String>(); - boolean canAppend = false; - while ((line = in.readLine()) != null) { - if (isSection(line)) { - section.addLines(null, lines.toArray(new String[lines.size()])); - lines.clear(); - canAppend = false; - String sn = StringUtils.replaceUnicodeSequences(line.substring(line.indexOf('[')+1, line.indexOf(']')).trim()); - section = getSection(sn, true).addHeaderComments(section.removeTrailingComments()); - } else { - char c = line.isEmpty() ? 0 : line.charAt(0); - if ((c == ' ' || c == '\t') && canAppend && ! (isComment(line) || isAssignment(line))) - lines.add(lines.remove(lines.size()-1) + '\n' + line.substring(1)); - else { - lines.add(line); - if (isAssignment(line)) - canAppend = true; - else - canAppend = canAppend && ! (StringUtils.isEmpty(line) || isComment(line)); - } - } - } - section.addLines(null, lines.toArray(new String[lines.size()])); - in.close(); - if (hasBeenModified) // Set when values need to be encoded. - save(); - if (file != null) - modifiedTimestamp = file.lastModified(); - } finally { - writeUnlock(); - } - } finally { - in.close(); - } - } finally { - writeUnlock(); - } - for (ConfigFileListener l : listeners) - l.onLoad(this); - return this; - } - - //-------------------------------------------------------------------------------- - // Map methods - //-------------------------------------------------------------------------------- - - @Override /* Map */ - public Section get(Object key) { - if (StringUtils.isEmpty(key)) - key = DEFAULT; - readLock(); - try { - return sections.get(key); - } finally { - readUnlock(); - } - } - - @Override /* Map */ - public Section put(String key, Section section) { - Set<String> changes = createChanges(); - Section old = put(key, section, changes); - signalChanges(changes); - return old; - } - - private Section put(String key, Section section, Set<String> changes) { - if (StringUtils.isEmpty(key)) - key = DEFAULT; - writeLock(); - try { - Section prev = sections.put(key, section); - findChanges(changes, prev, section); - return prev; - } finally { - writeUnlock(); - } - } - - @Override /* Map */ - public void putAll(Map<? extends String,? extends Section> map) { - Set<String> changes = createChanges(); - writeLock(); - try { - for (Map.Entry<? extends String,? extends Section> e : map.entrySet()) - put(e.getKey(), e.getValue(), changes); - } finally { - writeUnlock(); - } - signalChanges(changes); - } - - @Override /* Map */ - public void clear() { - Set<String> changes = createChanges(); - writeLock(); - try { - for (Section s : values()) - findChanges(changes, s, null); - sections.clear(); - } finally { - writeUnlock(); - } - signalChanges(changes); - } - - @Override /* Map */ - public boolean containsKey(Object key) { - if (StringUtils.isEmpty(key)) - key = DEFAULT; - return sections.containsKey(key); - } - - @Override /* Map */ - public boolean containsValue(Object value) { - return sections.containsValue(value); - } - - @Override /* Map */ - public Set<Map.Entry<String,Section>> entrySet() { - - // We need to create our own set so that entries are removed correctly. - return new AbstractSet<Map.Entry<String,Section>>() { - @Override /* Map */ - public Iterator<Map.Entry<String,Section>> iterator() { - return new Iterator<Map.Entry<String,Section>>() { - Iterator<Map.Entry<String,Section>> i = sections.entrySet().iterator(); - Map.Entry<String,Section> i2; - - @Override /* Iterator */ - public boolean hasNext() { - return i.hasNext(); - } - - @Override /* Iterator */ - public Map.Entry<String,Section> next() { - i2 = i.next(); - return i2; - } - - @Override /* Iterator */ - public void remove() { - Set<String> changes = createChanges(); - findChanges(changes, i2.getValue(), null); - i.remove(); - signalChanges(changes); - } - }; - } - - @Override /* Map */ - public int size() { - return sections.size(); - } - }; - } - - @Override /* Map */ - public boolean isEmpty() { - return sections.isEmpty(); - } - - @Override /* Map */ - public Set<String> keySet() { - - // We need to create our own set so that sections are removed correctly. - return new AbstractSet<String>() { - @Override /* Set */ - public Iterator<String> iterator() { - return new Iterator<String>() { - Iterator<String> i = sections.keySet().iterator(); - String i2; - - @Override /* Iterator */ - public boolean hasNext() { - return i.hasNext(); - } - - @Override /* Iterator */ - public String next() { - i2 = i.next(); - return i2; - } - - @Override /* Iterator */ - public void remove() { - Set<String> changes = createChanges(); - findChanges(changes, sections.get(i2), null); - i.remove(); - signalChanges(changes); - } - }; - } - - @Override /* Set */ - public int size() { - return sections.size(); - } - }; - } - - @Override /* Map */ - public int size() { - return sections.size(); - } - - @Override /* Map */ - public Collection<Section> values() { - return new AbstractCollection<Section>() { - @Override /* Collection */ - public Iterator<Section> iterator() { - return new Iterator<Section>() { - Iterator<Section> i = sections.values().iterator(); - Section i2; - - @Override /* Iterator */ - public boolean hasNext() { - return i.hasNext(); - } - - @Override /* Iterator */ - public Section next() { - i2 = i.next(); - return i2; - } - - @Override /* Iterator */ - public void remove() { - Set<String> changes = createChanges(); - findChanges(changes, i2, null); - i.remove(); - signalChanges(changes); - } - }; - } - @Override /* Collection */ - public int size() { - return sections.size(); - } - }; - } - - @Override /* Map */ - public Section remove(Object key) { - Set<String> changes = createChanges(); - Section prev = remove(key, changes); - signalChanges(changes); - return prev; - } - - private Section remove(Object key, Set<String> changes) { - writeLock(); - try { - Section prev = sections.remove(key); - findChanges(changes, prev, null); - return prev; - } finally { - writeUnlock(); - } - } - - //-------------------------------------------------------------------------------- - // API methods - //-------------------------------------------------------------------------------- - - @Override /* ConfigFile */ - public String get(String sectionName, String sectionKey) { - assertFieldNotNull(sectionKey, "sectionKey"); - Section s = get(sectionName); - if (s == null) - return null; - Object s2 = s.get(sectionKey); - return (s2 == null ? null : s2.toString()); - } - - @Override /* ConfigFile */ - public String put(String sectionName, String sectionKey, Object value, boolean encoded) { - assertFieldNotNull(sectionKey, "sectionKey"); - Section s = getSection(sectionName, true); - return s.put(sectionKey, value.toString(), encoded); - } - - @Override /* ConfigFile */ - public String remove(String sectionName, String sectionKey) { - assertFieldNotNull(sectionKey, "sectionKey"); - Section s = getSection(sectionName, false); - if (s == null) - return null; - return s.remove(sectionKey); - } - - @Override /* ConfigFile */ - public ConfigFileImpl addLines(String section, String...lines) { - Set<String> changes = createChanges(); - writeLock(); - try { - getSection(section, true).addLines(changes, lines); - } finally { - writeUnlock(); - } - signalChanges(changes); - return this; - } - - @Override /* ConfigFile */ - public ConfigFileImpl addHeaderComments(String section, String...headerComments) { - writeLock(); - try { - if (headerComments == null) - headerComments = new String[0]; - getSection(section, true).addHeaderComments(Arrays.asList(headerComments)); - } finally { - writeUnlock(); - } - return this; - } - - @Override /* ConfigFile */ - public ConfigFileImpl clearHeaderComments(String section) { - writeLock(); - try { - Section s = getSection(section, false); - if (s != null) - s.clearHeaderComments(); - } finally { - writeUnlock(); - } - return this; - } - - @Override /* ConfigFile */ - public Section getSection(String name) { - return getSection(name, false); - } - - @Override /* ConfigFile */ - public Section getSection(String name, boolean create) { - if (StringUtils.isEmpty(name)) - name = DEFAULT; - Section s = sections.get(name); - if (s != null) - return s; - if (create) { - s = new Section().setParent(this).setName(name); - sections.put(name, s); - return s; - } - return null; - } - - @Override /* ConfigFile */ - public ConfigFileImpl addSection(String name) { - writeLock(); - try { - getSection(name, true); - } finally { - writeUnlock(); - } - return this; - } - - @Override /* ConfigFile */ - public ConfigFile setSection(String name, Map<String,String> contents) { - writeLock(); - try { - put(name, new Section(contents).setParent(this).setName(name)); - } finally { - writeUnlock(); - } - return this; - } - - @Override /* ConfigFile */ - public ConfigFileImpl removeSection(String name) { - Set<String> changes = createChanges(); - writeLock(); - try { - Section prev = sections.remove(name); - if (changes != null && prev != null) - findChanges(changes, prev, null); - } finally { - writeUnlock(); - } - signalChanges(changes); - return this; - } - - @Override /* ConfigFile */ - public Set<String> getSectionKeys(String sectionName) { - Section s = get(sectionName); - if (s == null) - return null; - return s.keySet(); - } - - @Override /* ConfigFile */ - public boolean isEncoded(String key) { - assertFieldNotNull(key, "key"); - String section = getSectionName(key); - Section s = getSection(section, false); - if (s == null) - return false; - return s.isEncoded(getSectionKey(key)); - } - - @Override /* ConfigFile */ - public ConfigFileImpl save() throws IOException { - writeLock(); - try { - if (file == null) - throw new UnsupportedOperationException("No backing file specified for config file."); - Writer out = new OutputStreamWriter(new FileOutputStream(file), charset); - try { - serializeTo(out); - hasBeenModified = false; - modifiedTimestamp = file.lastModified(); - } finally { - out.close(); - } - for (ConfigFileListener l : listeners) - l.onSave(this); - return this; - } finally { - writeUnlock(); - } - } - - @Override /* ConfigFile */ - public ConfigFileImpl serializeTo(Writer out, ConfigFileFormat format) throws IOException { - readLock(); - try { - PrintWriter pw = (out instanceof PrintWriter ? (PrintWriter)out : new PrintWriter(out)); - for (Section s : sections.values()) - s.writeTo(pw, format); - pw.flush(); - pw.close(); - out.close(); - } finally { - readUnlock(); - } - return this; - } - - void setHasBeenModified() { - hasBeenModified = true; - } - - @Override /* ConfigFile */ - public String toString() { - try { - StringWriter sw = new StringWriter(); - toWritable().writeTo(sw); - return sw.toString(); - } catch (IOException e) { - return e.getLocalizedMessage(); - } - } - - @Override /* ConfigFile */ - public ConfigFile addListener(ConfigFileListener listener) { - assertFieldNotNull(listener, "listener"); - writeLock(); - try { - this.listeners.add(listener); - return this; - } finally { - writeUnlock(); - } - } - - List<ConfigFileListener> getListeners() { - return listeners; - } - - @Override /* ConfigFile */ - public Writable toWritable() { - return new ConfigFileWritable(this); - } - - @Override /* ConfigFile */ - public ConfigFile merge(ConfigFile cf) { - assertFieldNotNull(cf, "cf"); - Set<String> changes = createChanges(); - writeLock(); - try { - for (String sectionName : this.keySet()) - if (! cf.containsKey(sectionName)) - remove(sectionName, changes); - - for (Map.Entry<String,Section> e : cf.entrySet()) - put(e.getKey(), e.getValue(), changes); - - } finally { - writeUnlock(); - } - signalChanges(changes); - return this; - } - - Encoder getEncoder() { - return encoder; - } - - @Override /* ConfigFile */ - protected WriterSerializer getSerializer() throws SerializeException { - if (serializer == null) - throw new SerializeException("Serializer not defined on config file."); - return serializer; - } - - @Override /* ConfigFile */ - protected ReaderParser getParser() throws ParseException { - if (parser == null) - throw new ParseException("Parser not defined on config file."); - return parser; - } - - @Override /* ConfigFile */ - protected void readLock() { - lock.readLock().lock(); - } - - @Override /* ConfigFile */ - protected void readUnlock() { - lock.readLock().unlock(); - } - - private void writeLock() { - if (readOnly) - throw new UnsupportedOperationException("Cannot modify read-only ConfigFile."); - lock.writeLock().lock(); - hasBeenModified = true; - } - - private void writeUnlock() { - lock.writeLock().unlock(); - } - - @Override /* ConfigFile */ - public ConfigFile getResolving(VarResolver vr) { - assertFieldNotNull(vr, "vr"); - return new ConfigFileWrapped(this, vr); - } - - @Override /* ConfigFile */ - public ConfigFile getResolving(VarResolverSession vs) { - assertFieldNotNull(vs, "vs"); - return new ConfigFileWrapped(this, vs); - } - - @Override /* ConfigFile */ - public ConfigFile getResolving() { - return getResolving(VarResolver.DEFAULT.clone().addVars(ConfigFileVar.class).setContextObject(ConfigFileVar.SESSION_config, this)); - } - - /* - * Finds the keys that are different between the two sections and adds it to - * the specified set. - */ - private void findChanges(Set<String> s, Section a, Section b) { - if (s == null) - return; - String sname = (a == null ? b.name : a.name); - if (a == null) { - for (String k : b.keySet()) - s.add(getFullKey(sname, k)); - } else if (b == null) { - for (String k : a.keySet()) - s.add(getFullKey(sname, k)); - } else { - for (String k : a.keySet()) - addChange(s, sname, k, a.get(k), b.get(k)); - for (String k : b.keySet()) - addChange(s, sname, k, a.get(k), b.get(k)); - } - } - - private void addChange(Set<String> changes, String section, String key, String oldVal, String newVal) { - if (! StringUtils.isEquals(oldVal, newVal)) - changes.add(getFullKey(section, key)); - } - - private Set<String> createChanges() { - return (listeners.size() > 0 ? new LinkedHashSet<String>() : null); - } - - private void signalChanges(Set<String> changes) { - if (changes != null && ! changes.isEmpty()) - for (ConfigFileListener l : listeners) - l.onChange(this, changes); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2c3a7cb5/com.ibm.team.juno/src/main/java/org/apache/juneau/ini/ConfigFileListener.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno/src/main/java/org/apache/juneau/ini/ConfigFileListener.java b/com.ibm.team.juno/src/main/java/org/apache/juneau/ini/ConfigFileListener.java deleted file mode 100644 index 3a1d37a..0000000 --- a/com.ibm.team.juno/src/main/java/org/apache/juneau/ini/ConfigFileListener.java +++ /dev/null @@ -1,46 +0,0 @@ -/*************************************************************************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - ***************************************************************************************************************************/ -package org.apache.juneau.ini; - -import java.util.*; - - -/** - * Listener that can be used to listen for change events in config files. - * <p> - * Use the {@link ConfigFile#addListener(ConfigFileListener)} method to register listeners. - */ -public class ConfigFileListener { - - /** - * Gets called immediately after a config file has been loaded. - * - * @param cf The config file being loaded. - */ - public void onLoad(ConfigFile cf) {} - - /** - * Gets called immediately after a config file has been saved. - * - * @param cf The config file being saved. - */ - public void onSave(ConfigFile cf) {} - - /** - * Signifies that the specified values have changed. - * - * @param cf The config file being modified. - * @param changes The full keys (e.g. <js>"Section/key"</js>) of entries that have changed in the config file. - */ - public void onChange(ConfigFile cf, Set<String> changes) {} -} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2c3a7cb5/com.ibm.team.juno/src/main/java/org/apache/juneau/ini/ConfigFileWrapped.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno/src/main/java/org/apache/juneau/ini/ConfigFileWrapped.java b/com.ibm.team.juno/src/main/java/org/apache/juneau/ini/ConfigFileWrapped.java deleted file mode 100644 index c9175d4..0000000 --- a/com.ibm.team.juno/src/main/java/org/apache/juneau/ini/ConfigFileWrapped.java +++ /dev/null @@ -1,278 +0,0 @@ -/*************************************************************************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - ***************************************************************************************************************************/ -package org.apache.juneau.ini; - -import static org.apache.juneau.internal.ThrowableUtils.*; - -import java.io.*; -import java.util.*; - -import org.apache.juneau.*; -import org.apache.juneau.parser.*; -import org.apache.juneau.serializer.*; -import org.apache.juneau.svl.*; -import org.apache.juneau.svl.vars.*; - -/** - * Wraps an instance of {@link ConfigFileImpl} in an interface that will - * automatically replace {@link VarResolver} variables. - * <p> - * The {@link ConfigFile#getResolving(VarResolver)} returns an instance of this class. - * <p> - * This class overrides the {@link #getString(String, String)} to resolve string variables. - * All other method calls are passed through to the inner config file. - * - * @author James Bognar ([email protected]) - */ -public final class ConfigFileWrapped extends ConfigFile { - - private final ConfigFileImpl cf; - private final VarResolverSession vs; - - ConfigFileWrapped(ConfigFileImpl cf, VarResolver vr) { - this.cf = cf; - this.vs = vr.clone() - .addVars(ConfigFileVar.class) - .setContextObject(ConfigFileVar.SESSION_config, cf) - .createSession(); - } - - ConfigFileWrapped(ConfigFileImpl cf, VarResolverSession vs) { - this.cf = cf; - this.vs = vs; - } - - @Override /* ConfigFile */ - public void clear() { - cf.clear(); - } - - @Override /* ConfigFile */ - public boolean containsKey(Object key) { - return cf.containsKey(key); - } - - @Override /* ConfigFile */ - public boolean containsValue(Object value) { - return cf.containsValue(value); - } - - @Override /* ConfigFile */ - public Set<java.util.Map.Entry<String,Section>> entrySet() { - return cf.entrySet(); - } - - @Override /* ConfigFile */ - public Section get(Object key) { - return cf.get(key); - } - - @Override /* ConfigFile */ - public boolean isEmpty() { - return cf.isEmpty(); - } - - @Override /* ConfigFile */ - public Set<String> keySet() { - return cf.keySet(); - } - - @Override /* ConfigFile */ - public Section put(String key, Section value) { - return cf.put(key, value); - } - - @Override /* ConfigFile */ - public void putAll(Map<? extends String,? extends Section> map) { - cf.putAll(map); - } - - @Override /* ConfigFile */ - public Section remove(Object key) { - return cf.remove(key); - } - - @Override /* ConfigFile */ - public int size() { - return cf.size(); - } - - @Override /* ConfigFile */ - public Collection<Section> values() { - return cf.values(); - } - - @Override /* ConfigFile */ - public ConfigFile loadIfModified() throws IOException { - cf.loadIfModified(); - return this; - } - - @Override /* ConfigFile */ - public ConfigFile load() throws IOException { - cf.load(); - return this; - } - - @Override /* ConfigFile */ - public ConfigFile load(Reader r) throws IOException { - cf.load(r); - return this; - } - - - @Override /* ConfigFile */ - public boolean isEncoded(String key) { - return cf.isEncoded(key); - } - - @Override /* ConfigFile */ - public ConfigFile addLines(String section, String... lines) { - cf.addLines(section, lines); - return this; - } - - @Override /* ConfigFile */ - public ConfigFile addHeaderComments(String section, String... headerComments) { - cf.addHeaderComments(section, headerComments); - return this; - } - - @Override /* ConfigFile */ - public ConfigFile clearHeaderComments(String section) { - cf.clearHeaderComments(section); - return this; - } - - @Override /* ConfigFile */ - public Section getSection(String name) { - return cf.getSection(name); - } - - @Override /* ConfigFile */ - public Section getSection(String name, boolean create) { - return cf.getSection(name, create); - } - - @Override /* ConfigFile */ - public ConfigFile addSection(String name) { - cf.addSection(name); - return this; - } - - @Override /* ConfigFile */ - public ConfigFile setSection(String name, Map<String,String> contents) { - cf.setSection(name, contents); - return this; - } - - @Override /* ConfigFile */ - public ConfigFile removeSection(String name) { - cf.removeSection(name); - return this; - } - - @Override /* ConfigFile */ - public ConfigFile save() throws IOException { - cf.save(); - return this; - } - - @Override /* ConfigFile */ - public ConfigFile serializeTo(Writer out, ConfigFileFormat format) throws IOException { - cf.serializeTo(out, format); - return this; - } - - @Override /* ConfigFile */ - public String toString() { - return cf.toString(); - } - - @Override /* ConfigFile */ - public ConfigFile getResolving(VarResolver varResolver) { - assertFieldNotNull(varResolver, "vr"); - return new ConfigFileWrapped(cf, varResolver); - } - - @Override /* ConfigFile */ - public ConfigFile getResolving(VarResolverSession varSession) { - assertFieldNotNull(varSession, "vs"); - return new ConfigFileWrapped(cf, varSession); - } - - @Override /* ConfigFile */ - public ConfigFile getResolving() { - return new ConfigFileWrapped(cf, VarResolver.DEFAULT); - } - - @Override /* ConfigFile */ - public ConfigFile addListener(ConfigFileListener listener) { - cf.addListener(listener); - return this; - } - - @Override /* ConfigFile */ - public Writable toWritable() { - return cf.toWritable(); - } - - @Override /* ConfigFile */ - public ConfigFile merge(ConfigFile newCf) { - cf.merge(newCf); - return this; - } - - @Override /* ConfigFile */ - protected WriterSerializer getSerializer() throws SerializeException { - return cf.getSerializer(); - } - - @Override /* ConfigFile */ - protected ReaderParser getParser() throws ParseException { - return cf.getParser(); - } - - @Override /* ConfigFile */ - public String get(String sectionName, String sectionKey) { - String s = cf.get(sectionName, sectionKey); - if (s == null) - return null; - return vs.resolve(s); - } - - @Override /* ConfigFile */ - public String put(String sectionName, String sectionKey, Object value, boolean encoded) { - return cf.put(sectionName, sectionKey, value, encoded); - } - - @Override /* ConfigFile */ - public String remove(String sectionName, String sectionKey) { - return cf.remove(sectionName, sectionKey); - } - - @Override /* ConfigFile */ - public Set<String> getSectionKeys(String sectionName) { - return cf.getSectionKeys(sectionName); - } - - @Override /* ConfigFile */ - protected void readLock() { - cf.readLock(); - } - - @Override /* ConfigFile */ - protected void readUnlock() { - cf.readUnlock(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2c3a7cb5/com.ibm.team.juno/src/main/java/org/apache/juneau/ini/ConfigFileWritable.java ---------------------------------------------------------------------- diff --git a/com.ibm.team.juno/src/main/java/org/apache/juneau/ini/ConfigFileWritable.java b/com.ibm.team.juno/src/main/java/org/apache/juneau/ini/ConfigFileWritable.java deleted file mode 100644 index 1d076fa..0000000 --- a/com.ibm.team.juno/src/main/java/org/apache/juneau/ini/ConfigFileWritable.java +++ /dev/null @@ -1,44 +0,0 @@ -/*************************************************************************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - ***************************************************************************************************************************/ -package org.apache.juneau.ini; - -import java.io.*; - -import org.apache.juneau.*; - -/** - * Wraps a {@link ConfigFile} in a {@link Writable} to be rendered as plain text. - */ -class ConfigFileWritable implements Writable { - - private ConfigFileImpl cf; - - protected ConfigFileWritable(ConfigFileImpl cf) { - this.cf = cf; - } - - @Override /* Writable */ - public void writeTo(Writer out) throws IOException { - cf.readLock(); - try { - cf.serializeTo(out); - } finally { - cf.readUnlock(); - } - } - - @Override /* Writable */ - public String getMediaType() { - return "text/plain"; - } -}
