?? configurationxmldocument.java
字號:
* Transforms the wrapped configuration into a w3c document. The root
* element will be given a default name.
* @param prefix a prefix for the keys to process; can be <b>null</b>,
* then all keys in the configuration will be added to the document
* @return the document
* @throws DocumentException if an error occurs
*/
public org.w3c.dom.Document getW3cDocument(String prefix)
throws DocumentException
{
return getW3cDocument(prefix, null);
}
/**
* Transforms the wrapped configuration into a w3c document. The root
* element will be given a default name.
* @return the document
* @throws DocumentException if an error occurs
*/
public org.w3c.dom.Document getW3cDocument() throws DocumentException
{
return getW3cDocument(null, null);
}
/**
* Converts a dom4j document into a w3c document.
* @param doc the dom4j document
* @return the w3c document
* @throws DocumentException if an error occurs
*/
static org.w3c.dom.Document toW3cDocument(Document doc)
throws DocumentException
{
return new DOMWriter().write(doc);
}
/**
* Helper method for constructing a subset if necessary. Depending on
* the passed in key this method either returns the wrapped configuration
* or the specified subset of it.
* @param key the key
* @return the configuration for that key
*/
private Configuration configForKey(String key)
{
Configuration conf = (key == null)
? getConfiguration()
: getConfiguration().subset(key);
// undefined?
if(conf == null || (conf instanceof CompositeConfiguration
&& ((CompositeConfiguration) conf).getNumberOfConfigurations() < 2))
{
throw new NoSuchElementException("No subset with key " + key);
} /* if */
return conf;
}
/**
* <p>Creates and initializes an object specified in the configuration
* using Digester.</p>
* <p>This method first constructs a subset configuration with the keys
* starting with the given prefix. It then transforms this subset into a
* XML document and let that be processed by Digester. The result of this
* processing is returned.</p>
* <p>The method is intended to be used for creating simple objects that
* are specified somewhere in the configuration in a standard way. The
* following fragment shows how a configuration file must look like to be
* understood by the default Digester rule set used by this method:</p>
* <p><pre>
* ...
* <class name="mypackage.MyClass"/>
* <args>
* <property name="myFirstProperty" value="myFirstValue"/>
* <property name="MySecondProperty" value="mySecondValue"/>
* ...
* </args>
* ...
* </pre></p>
* @param prefix the prefix of the keys that are passed to Digester; can
* be <b>null</b>, then the whole configuration will be processed
* @return the result of the Digester processing
* @throws IOException if an IOException occurs
* @throws SAXException if a SAXException occurs
*/
public Object callDigester(String prefix) throws IOException, SAXException
{
Digester digester = getDefaultDigester(prefix);
return digester.parse(getClass().getName());
}
/**
* Returns a default Digester instance. This instance is used for the
* simple object creation feature.
* @param prefix the prefix of the keys to be processed; can be
* <b>null</b>, then the whole configuration is meant
* @return the default Digester instance
*/
protected Digester getDefaultDigester(String prefix)
{
Digester digester = createDefaultDigester(prefix);
setupDefaultDigester(digester);
return digester;
}
/**
* Creates the default Digester instance for the given prefix. This method
* is called by <code>getDefaultDigester()</code>.
* @param prefix the prefix of the keys to be processed; can be
* <b>null</b>, then the whole configuration is meant
* @return the default Digester instance
*/
protected Digester createDefaultDigester(String prefix)
{
return new Digester(createXMLReader(prefix));
}
/**
* Initializes the default digester instance used for simple object
* creation. Here all needed properties and rules can be set. This base
* implementation sets default rules for object creation as explained in
* the comment for the <code>callDigester()</code> methods.
* @param digester the digester instance to be initialized
*/
protected void setupDefaultDigester(Digester digester)
{
digester.addObjectCreate(ELEM_CLASS, ATTR_NAME, Object.class);
digester.addSetProperty(ELEM_PROPERTY, ATTR_NAME, ATTR_VALUE);
}
/**
* Writes a configuration (or parts of it) to the given writer.
* @param out the output writer
* @param prefix the prefix of the subset to write; if <b>null</b>, the
* whole configuration is written
* @param root the name of the root element of the resulting document;
* <b>null</b> for a default name
* @param pretty flag for the pretty print mode
* @throws IOException if an IO error occurs
* @throws DocumentException if there is an error during processing
*/
public void write(Writer out, String prefix, String root, boolean pretty)
throws IOException, DocumentException
{
OutputFormat format =
(pretty)
? OutputFormat.createPrettyPrint()
: OutputFormat.createCompactFormat();
XMLWriter writer = new XMLWriter(out, format);
writer.write(getDocument(prefix, root));
}
/**
* Writes a configuration (or parts of it) to the given writer.
* This overloaded version always uses pretty print mode.
* @param out the output writer
* @param prefix the prefix of the subset to write; if <b>null</b>, the
* whole configuration is written
* @param root the name of the root element of the resulting document;
* <b>null</b> for a default name
* @throws IOException if an IO error occurs
* @throws DocumentException if there is an error during processing
*/
public void write(Writer out, String prefix, String root)
throws IOException, DocumentException
{
write(out, prefix, root, true);
}
/**
* Writes a configuration (or parts of it) to the given writer.
* The resulting document's root element will be given a default name.
* @param out the output writer
* @param prefix the prefix of the subset to write; if <b>null</b>, the
* whole configuration is written
* @param pretty flag for the pretty print mode
* @throws IOException if an IO error occurs
* @throws DocumentException if there is an error during processing
*/
public void write(Writer out, String prefix, boolean pretty)
throws IOException, DocumentException
{
write(out, prefix, null, pretty);
}
/**
* Writes a configuration (or parts of it) to the given writer.
* The resulting document's root element will be given a default name.
* This overloaded version always uses pretty print mode.
* @param out the output writer
* @param prefix the prefix of the subset to write; if <b>null</b>, the
* whole configuration is written
* @throws IOException if an IO error occurs
* @throws DocumentException if there is an error during processing
*/
public void write(Writer out, String prefix)
throws IOException, DocumentException
{
write(out, prefix, true);
}
/**
* Writes the wrapped configuration to the given writer.
* The resulting document's root element will be given a default name.
* @param out the output writer
* @param pretty flag for the pretty print mode
* @throws IOException if an IO error occurs
* @throws DocumentException if there is an error during processing
*/
public void write(Writer out, boolean pretty)
throws IOException, DocumentException
{
write(out, null, null, pretty);
}
/**
* Writes the wrapped configuration to the given writer.
* The resulting document's root element will be given a default name.
* This overloaded version always uses pretty print mode.
* @param out the output writer
* @throws IOException if an IO error occurs
* @throws DocumentException if there is an error during processing
*/
public void write(Writer out) throws IOException, DocumentException
{
write(out, true);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -