📄 xmlconfig.java
字号:
/* XmlConfig interface definition; every XmlConfig
has to have an initFromEnv and an initDomTree function,
along with an Env containing subbeans, each of which
is also an XmlConfig. The normal approach is to have
the subbeans specified in the config file; they will
be dynamically loaded into a subbeans Env, and
their initializations will be called. In general
an initialization by Envs is equivalent to one by
Dom trees, except that the Dom tree is more easily
checked whereas the Envs are more easily generated,
e.g. by html forms. If initialization is carried out
more than once, it is up to the individual XmlConfig
to decide whether new values overwrite the old.
(For example, new values from an html page Env may be
used to override defaults from an xml initialization;
this is obviously crucial for usernames, passwords
and such.)
<mailTop>
<class>simpleMail.SimpleMailBean</class>
<subbeans>
<mailSource>
<class>MyNa.xml.MailViewSource</class>
<dbDriver>pop3</dbDriver>
<dbName>mail.dreamscape.com</dbName>
<dbUser>JohnSmith</dbUser>
...
</mailSource>
The DTD (if any) should make sure that the first property of an
XmlConfig element is its class, followed by subbeans, followed by any
additional properties. The XML opening above
should be equivalent to the Env resulting from
<input type=hidden name="beanName" value="mailTop">
<input type=hidden name="class" value="simpleMail.SimpleMailBean">
<input type=hidden name=mailSource_beanName value="mailSource">
<input type=hidden name=mailSource_class value="MyNa.xml.MailViewSource">
<input type=hidden name=mailSource_dbDriver value="pop3">
Whatever traversal is defined for the Dom tree should therefore
make sure that the toplevel tag (e.g., "mailTop") is placed
as the beanName property of the resulting Env, and similarly
for any subbean such as mailSource. The top-level "class" is
redundant (the configuration is being performed by an object of
this class and can be used for error-checking or ignored, but
by placing it there we make it easy to include this configuration
as a subbean of an outer configuration. The top-level initFromTopEnv
and initFromDomTree(String fileName) methods normally just set
things up for (recursive) initFromEnv() and initFromDomTree() methods.
For maximal uniformity we have the initFromDomTree version work
by calling on initFromEnv(), after building the tree into the
same Env that would have been constructed from the html form;
this is probably not the best answer, but it shows how the
ideas interlock. We'd like to have XmlConfig as a class,
but we'd need multiple inheritance to make that work, so
instead we leave it as an interface and define these defaults
as static methods within a utility XmlConfigure class (which
is never to be instantiated.) the static XmlConfigure methods
will be called by XmlConfig methods of the same name, which
will then use the Env to set properties.
Each subbean might, for example, have a setEnv(Env E) method to
consist mainly of calls on specific setters, giving them data
from the Env.
*/
package MyNa.xml;
import MyNa.utils.Env;
import org.w3c.dom.Node;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Attr;
import org.xml.sax.SAXParseException;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.sun.xml.parser.Resolver;
import com.sun.xml.tree.XmlDocument;
import org.w3c.dom.Element;
public interface XmlConfig {
public void setBeanName(String S);
public void setVerbose(String S);
public void setDebug(String S);
public String getBeanName();
public void setString(String nm,String val);
// usually of the form
// if(nm==null)return;
// if(nm.equals("debug"))setDebug(val);
// else if(nm.equals("verbose"))setVerbose(val);
// and so forth.
public String getString(String nm);
// same idea as setString, of course.
public String initFromEnv(Env E);
// init using values from the Env,
// which includes subenvironments for beans;
public String initFromTopEnv(Env E);
// init using values from the Env,
// with keys of the form xxx_yyy_zzz to refer
// to subbean locations.
public String initFromXmlFile(String fileName,
boolean validate,Env E);
// top level; construct tree, call Node version.
public String initFromDomTree(Node nd,Env E);
// init from Node; use subtrees for subbeans.
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -