/** * Internal helper method for loading a file from the given input stream. * * @param in the input stream * @param locator the current {@code FileLocator} * @throws ConfigurationException if an error occurs */ private void load(final InputStream in, final FileLocator locator) throws ConfigurationException { load(in, locator.getEncoding()); }
/** * Returns the encoding of the associated file. Result can be <b>null</b> if * no encoding has been set. * * @return the encoding of the associated file */ public String getEncoding() { return getFileLocator().getEncoding(); }
/** * Internal helper method for saving a file to the given output stream. * * @param out the output stream * @param locator the current {@code FileLocator} * @throws ConfigurationException if an error occurs during the save * operation */ private void save(final OutputStream out, final FileLocator locator) throws ConfigurationException { save(out, locator.getEncoding()); }
/** * Creates and initializes the transformer used for save operations. This * base implementation initializes all of the default settings like * indention mode and the DOCTYPE. Derived classes may overload this method * if they have specific needs. * * @return the transformer to use for a save operation * @throws ConfigurationException if an error occurs * @since 1.3 */ protected Transformer createTransformer() throws ConfigurationException { final Transformer transformer = XMLDocumentHelper.createTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); if (locator.getEncoding() != null) { transformer.setOutputProperty(OutputKeys.ENCODING, locator.getEncoding()); } if (publicID != null) { transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, publicID); } if (systemID != null) { transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, systemID); } return transformer; }
/** * Returns a hash code for this object. * * @return a hash code for this object */ @Override public int hashCode() { return new HashCodeBuilder().append(getFileName()) .append(getBasePath()).append(sourceURLAsString()) .append(getEncoding()).append(getFileSystem()) .append(getLocationStrategy()).toHashCode(); }
/** * Returns a string representation of this object. This string contains the * values of all properties. * * @return a string for this object */ @Override public String toString() { return new ToStringBuilder(this).append("fileName", getFileName()) .append("basePath", getBasePath()) .append("sourceURL", sourceURLAsString()) .append("encoding", getEncoding()) .append("fileSystem", getFileSystem()) .append("locationStrategy", getLocationStrategy()).toString(); }
@Override public void write(final Writer out) throws ConfigurationException { if (locator == null) { throw new ConfigurationException("Save operation not properly " + "initialized! Do not call write(Writer) directly," + " but use a FileHandler to save a configuration."); } final PrintWriter writer = new PrintWriter(out); if (locator.getEncoding() != null) { writer.println("<?xml version=\"1.0\" encoding=\"" + locator.getEncoding() + "\"?>"); } else { writer.println("<?xml version=\"1.0\"?>"); } writer.println("<!DOCTYPE plist SYSTEM \"file://localhost/System/Library/DTDs/PropertyList.dtd\">"); writer.println("<plist version=\"1.0\">"); printNode(writer, 1, getNodeModel().getNodeHandler().getRootNode()); writer.println("</plist>"); writer.flush(); }
/** * Stores the specified {@code FileLocator} in the given map. With the * {@link #fromMap(Map)} method a new {@code FileLocator} with the same * properties as the original one can be created. * * @param locator the {@code FileLocator} to be stored * @param map the map in which to store the {@code FileLocator} (must not be * <b>null</b>) * @throws IllegalArgumentException if the map is <b>null</b> */ public static void put(final FileLocator locator, final Map<String, Object> map) { if (map == null) { throw new IllegalArgumentException("Map must not be null!"); } if (locator != null) { map.put(PROP_BASE_PATH, locator.getBasePath()); map.put(PROP_ENCODING, locator.getEncoding()); map.put(PROP_FILE_NAME, locator.getFileName()); map.put(PROP_FILE_SYSTEM, locator.getFileSystem()); map.put(PROP_SOURCE_URL, locator.getSourceURL()); map.put(PROP_STRATEGY, locator.getLocationStrategy()); } }
String encoding = (locator != null) ? locator.getEncoding() : null; if (encoding == null)
/** * Compares this object with another one. Two instances of * {@code FileLocatorImpl} are considered equal if all of their properties * are equal. * * @param obj the object to compare to * @return a flag whether these objects are equal */ @Override public boolean equals(final Object obj) { if (this == obj) { return true; } if (!(obj instanceof FileLocator)) { return false; } final FileLocator c = (FileLocator) obj; return new EqualsBuilder().append(getFileName(), c.getFileName()) .append(getBasePath(), c.getBasePath()) .append(sourceURLAsString(), c.sourceURLAsString()) .append(getEncoding(), c.getEncoding()) .append(getFileSystem(), c.getFileSystem()) .append(getLocationStrategy(), c.getLocationStrategy()) .isEquals(); }
/** * Internal helper method for saving data to the given {@code File}. * * @param file the target file * @param locator the current {@code FileLocator} * @throws ConfigurationException if an error occurs during the save * operation */ private void save(final File file, final FileLocator locator) throws ConfigurationException { OutputStream out = null; try { out = FileLocatorUtils.obtainFileSystem(locator).getOutputStream(file); saveToStream(out, locator.getEncoding(), file.toURI().toURL()); } catch (final MalformedURLException muex) { throw new ConfigurationException(muex); } finally { closeSilent(out); } }
/** * Initializes the properties of this builder from the passed in locator * object. * * @param src the source {@code FileLocator} */ private void initBuilder(final FileLocator src) { basePath = src.getBasePath(); fileName = src.getFileName(); sourceURL = src.getSourceURL(); encoding = src.getEncoding(); fileSystem = src.getFileSystem(); locationStrategy = src.getLocationStrategy(); } }
/** * Internal helper method for loading a file from the given URL. * * @param url the URL * @param locator the current {@code FileLocator} * @throws ConfigurationException if an error occurs */ private void load(final URL url, final FileLocator locator) throws ConfigurationException { InputStream in = null; try { in = FileLocatorUtils.obtainFileSystem(locator).getInputStream(url); loadFromStream(in, locator.getEncoding(), url); } catch (final ConfigurationException e) { throw e; } catch (final Exception e) { throw new ConfigurationException( "Unable to load the configuration from the URL " + url, e); } finally { closeSilent(in); } }
saveToStream(out, locator.getEncoding(), url); if (out instanceof VerifiableOutputStream)