在项目解析xml的方式很多,常用的如JDOM,dom4j,SAX,DOM,XStream 进行xml的解析,在Struts1.0中解析xml 的方式是通过Commons-Digister实现,同样在QuickServer中也采用同种方式解析相关的方式。
QuickServer中ConfigReader中解析xml的源代码如下:
public static QuickServerConfig read(File input) throws Exception { Digester digester = new Digester(); digester.setValidating(false); String mainTag = "quickserver"; String subTag = ""; digester.addObjectCreate(mainTag, QuickServerConfig.class); digester.addBeanPropertySetter(mainTag + "/name", "name"); digester.addBeanPropertySetter(mainTag + "/server-banner", "serverBanner"); digester.addBeanPropertySetter(mainTag + "/port", "port"); digester.addBeanPropertySetter(mainTag + "/bind-address", "bindAddr"); String curTag = mainTag + "/server-mode"; digester.addObjectCreate(curTag, ServerMode.class); digester.addBeanPropertySetter(curTag + "/blocking", "blocking"); digester.addSetNext(curTag, "setServerMode"); digester.addBeanPropertySetter(mainTag + "/client-event-handler", "clientEventHandler"); digester.addBeanPropertySetter(mainTag + "/client-command-handler", "clientCommandHandler"); digester.addBeanPropertySetter(mainTag + "/client-object-handler", "clientObjectHandler"); digester.addBeanPropertySetter(mainTag + "/client-binary-handler", "clientBinaryHandler"); digester.addBeanPropertySetter(mainTag + "/client-write-handler", "clientWriteHandler"); digester.addBeanPropertySetter(mainTag + "/authenticator", "authenticator"); digester.addBeanPropertySetter(mainTag + "/client-authentication-handler", "clientAuthenticationHandler"); digester.addBeanPropertySetter(mainTag + "/client-data", "clientData"); digester.addBeanPropertySetter(mainTag + "/client-extended-event-handler", "clientExtendedEventHandler"); digester.addBeanPropertySetter(mainTag + "/timeout", "timeout"); digester.addBeanPropertySetter(mainTag + "/timeout-msg", "timeoutMsg"); digester.addBeanPropertySetter(mainTag + "/max-auth-try", "maxAuthTry"); digester.addBeanPropertySetter(mainTag + "/max-auth-try-msg", "maxAuthTryMsg"); digester.addBeanPropertySetter(mainTag + "/max-connection", "maxConnection"); digester.addBeanPropertySetter(mainTag + "/max-connection-msg", "maxConnectionMsg"); digester.addBeanPropertySetter(mainTag + "/console-logging-level", "consoleLoggingLevel"); digester.addBeanPropertySetter(mainTag + "/console-logging-formatter", "consoleLoggingFormatter"); curTag = mainTag + "/default-data-mode"; digester.addObjectCreate(curTag, DefaultDataMode.class); digester.addBeanPropertySetter(curTag + "/data-type-in", "dataModeIn"); digester.addBeanPropertySetter(curTag + "/data-type-out", "dataModeOut"); digester.addSetNext(curTag, "setDefaultDataMode"); curTag = mainTag + "/object-pool"; digester.addObjectCreate(curTag, ObjectPoolConfig.class); digester.addBeanPropertySetter(curTag + "/max-active", "maxActive"); digester.addBeanPropertySetter(curTag + "/max-idle", "maxIdle"); digester.addBeanPropertySetter(curTag + "/init-size", "initSize"); digester.addBeanPropertySetter(curTag + "/pool-manager", "poolManager"); digester.addObjectCreate(curTag + "/thread-object-pool", ThreadObjectPoolConfig.class); digester.addBeanPropertySetter(curTag + "/thread-object-pool/max-active", "maxActive"); digester.addBeanPropertySetter(curTag + "/thread-object-pool/max-idle", "maxIdle"); digester.addBeanPropertySetter(curTag + "/thread-object-pool/init-size", "initSize"); digester.addSetNext(curTag + "/thread-object-pool", "setThreadObjectPoolConfig"); digester.addObjectCreate(curTag + "/client-handler-object-pool", ClientHandlerObjectPoolConfig.class); digester.addBeanPropertySetter(curTag + "/client-handler-object-pool/max-active", "maxActive"); digester.addBeanPropertySetter(curTag + "/client-handler-object-pool/max-idle", "maxIdle"); digester.addBeanPropertySetter(curTag + "/client-handler-object-pool/init-size", "initSize"); digester.addSetNext(curTag + "/client-handler-object-pool", "setClientHandlerObjectPoolConfig"); digester.addObjectCreate(curTag + "/byte-buffer-object-pool", ByteBufferObjectPoolConfig.class); digester.addBeanPropertySetter(curTag + "/byte-buffer-object-pool/max-active", "maxActive"); digester.addBeanPropertySetter(curTag + "/byte-buffer-object-pool/max-idle", "maxIdle"); digester.addBeanPropertySetter(curTag + "/byte-buffer-object-pool/init-size", "initSize"); digester.addSetNext(curTag + "/byte-buffer-object-pool", "setByteBufferObjectPoolConfig"); digester.addObjectCreate(curTag + "/client-data-object-pool", ClientDataObjectPoolConfig.class); digester.addBeanPropertySetter(curTag + "/client-data-object-pool/max-active", "maxActive"); digester.addBeanPropertySetter(curTag + "/client-data-object-pool/max-idle", "maxIdle"); digester.addBeanPropertySetter(curTag + "/client-data-object-pool/init-size", "initSize"); digester.addSetNext(curTag + "/client-data-object-pool", "setClientDataObjectPoolConfig"); digester.addSetNext(curTag, "setObjectPoolConfig"); digester.addBeanPropertySetter(mainTag + "/communication-logging/enable", "communicationLogging"); digester.addBeanPropertySetter(mainTag + "/security-manager-class", "securityManagerClass"); digester.addObjectCreate(mainTag + "/access-constraint", AccessConstraintConfig.class); digester.addObjectCreate(mainTag + "/access-constraint/ip-filter", IpFilterConfig.class); digester.addBeanPropertySetter(mainTag + "/access-constraint/ip-filter/enable", "enable"); digester.addBeanPropertySetter(mainTag + "/access-constraint/ip-filter/allow-access", "allowAccess"); digester.addCallMethod(mainTag + "/access-constraint/ip-filter/ip-collection/client-ip-address", "addClientIpAddress", 0); digester.addSetNext(mainTag + "/access-constraint/ip-filter", "setIpFilterConfig"); digester.addSetNext(mainTag + "/access-constraint", "setAccessConstraintConfig"); digester.addBeanPropertySetter(mainTag + "/application-jar-path", "applicationJarPath"); digester.addObjectCreate(mainTag + "/server-hooks", ServerHooks.class); digester.addCallMethod(mainTag + "/server-hooks/class-name", "addClassName", 0); digester.addSetNext(mainTag + "/server-hooks", "setServerHooks"); curTag = mainTag + "/secure"; digester.addObjectCreate(curTag, Secure.class); digester.addBeanPropertySetter(curTag + "/enable", "enable"); digester.addBeanPropertySetter(curTag + "/load", "load"); digester.addBeanPropertySetter(curTag + "/port", "port"); digester.addBeanPropertySetter(curTag + "/protocol", "protocol"); digester.addBeanPropertySetter(curTag + "/client-auth-enable", "clientAuthEnable"); digester.addObjectCreate(curTag + "/secure-store", SecureStore.class); digester.addBeanPropertySetter(curTag + "/secure-store/type", "type"); digester.addBeanPropertySetter(curTag + "/secure-store/algorithm", "algorithm"); digester.addBeanPropertySetter(curTag + "/secure-store/provider", "provider"); digester.addObjectCreate(curTag + "/secure-store/key-store-info", KeyStoreInfo.class); digester.addBeanPropertySetter(curTag + "/secure-store/key-store-info/store-file", "storeFile"); digester.addBeanPropertySetter(curTag + "/secure-store/key-store-info/store-password", "storePassword"); digester.addBeanPropertySetter(curTag + "/secure-store/key-store-info/key-password", "keyPassword"); digester.addSetNext(curTag + "/secure-store/key-store-info", "setKeyStoreInfo"); digester.addObjectCreate(curTag + "/secure-store/trust-store-info", TrustStoreInfo.class); digester.addBeanPropertySetter(curTag + "/secure-store/trust-store-info/store-file", "storeFile"); digester.addBeanPropertySetter(curTag + "/secure-store/trust-store-info/store-password", "storePassword"); digester.addBeanPropertySetter(curTag + "/secure-store/trust-store-info/type", "type"); digester.addBeanPropertySetter(curTag + "/secure-store/trust-store-info/provider", "provider"); digester.addSetNext(curTag + "/secure-store/trust-store-info", "setTrustStoreInfo"); digester.addBeanPropertySetter(curTag + "/secure-store/secure-store-manager", "secureStoreManager"); digester.addSetNext(curTag + "/secure-store", "setSecureStore"); digester.addSetNext(curTag, "setSecure"); curTag = mainTag + "/advanced-settings"; digester.addObjectCreate(curTag, AdvancedSettings.class); digester.addBeanPropertySetter(curTag + "/charset", "charset"); digester.addBeanPropertySetter(curTag + "/byte-buffer-size", "byteBufferSize"); digester.addBeanPropertySetter(curTag + "/backlog", "backlog"); digester.addBeanPropertySetter(curTag + "/use-direct-byte-buffer", "useDirectByteBuffer"); digester.addBeanPropertySetter(curTag + "/socket-linger", "socketLinger"); digester.addBeanPropertySetter(curTag + "/debug-non-blocking-mode", "debugNonBlockingMode"); digester.addBeanPropertySetter(curTag + "/client-identifier", "clientIdentifier"); digester.addBeanPropertySetter(curTag + "/qsobject-pool-maker", "qSObjectPoolMaker"); digester.addBeanPropertySetter(curTag + "/max-threads-for-nio-write", "maxThreadsForNioWrite"); digester.addSetNext(curTag, "setAdvancedSettings"); subTag = "qsadmin-server"; curTag = mainTag + "/" + subTag; digester.addObjectCreate(curTag, QSAdminServerConfig.class); digester.addBeanPropertySetter(curTag + "/name", "name"); digester.addBeanPropertySetter(curTag + "/server-banner", "serverBanner"); digester.addBeanPropertySetter(curTag + "/port", "port"); digester.addObjectCreate(curTag + "/server-mode", ServerMode.class); digester.addBeanPropertySetter(curTag + "/server-mode/blocking", "blocking"); digester.addSetNext(curTag + "/server-mode", "setServerMode"); digester.addBeanPropertySetter(curTag + "/client-event-handler", "clientEventHandler"); digester.addBeanPropertySetter(curTag + "/client-command-handler", "clientCommandHandler"); digester.addBeanPropertySetter(curTag + "/client-object-handler", "clientObjectHandler"); digester.addBeanPropertySetter(curTag + "/client-binary-handler", "clientBinaryHandler"); digester.addBeanPropertySetter(curTag + "/client-write-handler", "clientWriteHandler"); digester.addBeanPropertySetter(curTag + "/authenticator", "authenticator"); digester.addBeanPropertySetter(curTag + "/client-authentication-handler", "clientAuthenticationHandler"); digester.addBeanPropertySetter(curTag + "/client-data", "clientData"); digester.addBeanPropertySetter(curTag + "/client-extended-event-handler", "clientExtendedEventHandler"); digester.addBeanPropertySetter(curTag + "/timeout", "timeout"); digester.addBeanPropertySetter(curTag + "/max-auth-try", "maxAuthTry"); digester.addBeanPropertySetter(curTag + "/max-auth-try-msg", "maxAuthTryMsg"); digester.addBeanPropertySetter(curTag + "/timeout-msg", "timeoutMsg"); digester.addBeanPropertySetter(curTag + "/max-connection", "maxConnection"); digester.addBeanPropertySetter(curTag + "/max-connection-msg", "maxConnectionMsg"); digester.addBeanPropertySetter(curTag + "/bind-address", "bindAddr"); digester.addBeanPropertySetter(curTag + "/client-object-handler", "clientObjectHandler"); digester.addBeanPropertySetter(curTag + "/console-logging-level", "consoleLoggingLevel"); digester.addBeanPropertySetter(curTag + "/console-logging-formatter", "consoleLoggingFormatter"); digester.addObjectCreate(curTag + "/default-data-mode", DefaultDataMode.class); digester.addBeanPropertySetter(curTag + "/default-data-mode/data-type-in", "dataModeIn"); digester.addBeanPropertySetter(curTag + "/default-data-mode/data-type-out", "dataModeOut"); digester.addSetNext(curTag + "/default-data-mode", "setDefaultDataMode"); digester.addObjectCreate(curTag + "/object-pool", ObjectPoolConfig.class); digester.addBeanPropertySetter(curTag + "/object-pool/max-active", "maxActive"); digester.addBeanPropertySetter(curTag + "/object-pool/max-idle", "maxIdle"); digester.addBeanPropertySetter(curTag + "/object-pool/init-size", "initSize"); digester.addBeanPropertySetter(curTag + "/object-pool/pool-manager", "poolManager"); digester.addObjectCreate(curTag + "/object-pool/thread-object-pool", ThreadObjectPoolConfig.class); digester.addBeanPropertySetter(curTag + "/object-pool/thread-object-pool/max-active", "maxActive"); digester.addBeanPropertySetter(curTag + "/object-pool/thread-object-pool/max-idle", "maxIdle"); digester.addBeanPropertySetter(curTag + "/object-pool/thread-object-pool/init-size", "initSize"); digester.addSetNext(curTag + "/object-pool/thread-object-pool", "setThreadObjectPoolConfig"); digester.addObjectCreate(curTag + "/object-pool/client-handler-object-pool", ClientHandlerObjectPoolConfig.class); digester.addBeanPropertySetter(curTag + "/object-pool/client-handler-object-pool/max-active", "maxActive"); digester.addBeanPropertySetter(curTag + "/object-pool/client-handler-object-pool/max-idle", "maxIdle"); digester.addBeanPropertySetter(curTag + "/object-pool/client-handler-object-pool/init-size", "initSize"); digester.addSetNext(curTag + "/object-pool/client-handler-object-pool", "setClientHandlerObjectPoolConfig"); digester.addObjectCreate(curTag + "/object-pool/byte-buffer-object-pool", ByteBufferObjectPoolConfig.class); digester.addBeanPropertySetter(curTag + "/object-pool/byte-buffer-object-pool/max-active", "maxActive"); digester.addBeanPropertySetter(curTag + "/object-pool/byte-buffer-object-pool/max-idle", "maxIdle"); digester.addBeanPropertySetter(curTag + "/object-pool/byte-buffer-object-pool/init-size", "initSize"); digester.addSetNext(curTag + "/object-pool/byte-buffer-object-pool", "setByteBufferObjectPoolConfig"); digester.addObjectCreate(curTag + "/object-pool/client-data-object-pool", ClientDataObjectPoolConfig.class); digester.addBeanPropertySetter(curTag + "/object-pool/client-data-object-pool/max-active", "maxActive"); digester.addBeanPropertySetter(curTag + "/object-pool/client-data-object-pool/max-idle", "maxIdle"); digester.addBeanPropertySetter(curTag + "/object-pool/client-data-object-pool/init-size", "initSize"); digester.addSetNext(curTag + "/object-pool/client-data-object-pool", "setClientDataObjectPoolConfig"); digester.addSetNext(curTag + "/object-pool", "setObjectPoolConfig"); digester.addBeanPropertySetter(curTag + "/command-shell/enable", "commandShellEnable"); digester.addBeanPropertySetter(curTag + "/command-shell/prompt-name", "commandShellPromptName"); digester.addBeanPropertySetter(curTag + "/communication-logging/enable", "communicationLogging"); digester.addObjectCreate(curTag + "/access-constraint", AccessConstraintConfig.class); digester.addObjectCreate(curTag + "/access-constraint/ip-filter", IpFilterConfig.class); digester.addBeanPropertySetter(curTag + "/access-constraint/ip-filter/enable", "enable"); digester.addBeanPropertySetter(curTag + "/access-constraint/ip-filter/allow-access", "allowAccess"); digester.addCallMethod(curTag + "/access-constraint/ip-filter/ip-collection/client-ip-address", "addClientIpAddress", 0); digester.addSetNext(curTag + "/access-constraint/ip-filter", "setIpFilterConfig"); digester.addSetNext(curTag + "/access-constraint", "setAccessConstraintConfig"); digester.addObjectCreate(curTag + "/server-hooks", ServerHooks.class); digester.addCallMethod(curTag + "/server-hooks/class-name", "addClassName", 0); digester.addSetNext(curTag + "/server-hooks", "setServerHooks"); digester.addObjectCreate(curTag + "/secure", Secure.class); digester.addBeanPropertySetter(curTag + "/secure/enable", "enable"); digester.addBeanPropertySetter(curTag + "/secure/load", "load"); digester.addBeanPropertySetter(curTag + "/secure/port", "port"); digester.addBeanPropertySetter(curTag + "/secure/protocol", "protocol"); digester.addBeanPropertySetter(curTag + "/secure/client-auth-enable", "clientAuthEnable"); digester.addObjectCreate(curTag + "/secure/secure-store", SecureStore.class); digester.addBeanPropertySetter(curTag + "/secure/secure-store/type", "type"); digester.addBeanPropertySetter(curTag + "/secure/secure-store/algorithm", "algorithm"); digester.addBeanPropertySetter(curTag + "/secure/secure-store/provider", "provider"); digester.addObjectCreate(curTag + "/secure/secure-store/key-store-info", KeyStoreInfo.class); digester.addBeanPropertySetter(curTag + "/secure/secure-store/key-store-info/store-file", "storeFile"); digester.addBeanPropertySetter(curTag + "/secure/secure-store/key-store-info/store-password", "storePassword"); digester.addBeanPropertySetter(curTag + "/secure/secure-store/key-store-info/key-password", "keyPassword"); digester.addSetNext(curTag + "/secure/secure-store/key-store-info", "setKeyStoreInfo"); digester.addObjectCreate(curTag + "/secure/secure-store/trust-store-info", TrustStoreInfo.class); digester.addBeanPropertySetter(curTag + "/secure/secure-store/trust-store-info/store-file", "storeFile"); digester.addBeanPropertySetter(curTag + "/secure/secure-store/trust-store-info/store-password", "storePassword"); digester.addBeanPropertySetter(curTag + "/secure/secure-store/trust-store-info/type", "type"); digester.addBeanPropertySetter(curTag + "/secure/secure-store/trust-store-info/provider", "provider"); digester.addSetNext(curTag + "/secure/secure-store/trust-store-info", "setTrustStoreInfo"); digester.addBeanPropertySetter(curTag + "/secure/secure-store/secure-store-manager", "secureStoreManager"); digester.addSetNext(curTag + "/secure/secure-store", "setSecureStore"); digester.addSetNext(curTag + "/secure", "setSecure"); digester.addBeanPropertySetter(curTag + "/command-plugin", "commandPlugin"); digester.addObjectCreate(curTag + "/advanced-settings", AdvancedSettings.class); digester.addBeanPropertySetter(curTag + "/advanced-settings/charset", "charset"); digester.addBeanPropertySetter(curTag + "/advanced-settings/byte-buffer-size", "byteBufferSize"); digester.addBeanPropertySetter(curTag + "/advanced-settings/backlog", "backlog"); digester.addBeanPropertySetter(curTag + "/advanced-settings/use-direct-byte-buffer", "useDirectByteBuffer"); digester.addBeanPropertySetter(curTag + "/advanced-settings/socket-linger", "socketLinger"); digester.addBeanPropertySetter(curTag + "/advanced-settings/debug-non-blocking-mode", "debugNonBlockingMode"); digester.addBeanPropertySetter(curTag + "/advanced-settings/client-identifier", "clientIdentifier"); digester.addBeanPropertySetter(curTag + "/advanced-settings/qsobject-pool-maker", "qSObjectPoolMaker"); digester.addBeanPropertySetter(curTag + "/advanced-settings/max-threads-for-nio-write", "maxThreadsForNioWrite"); digester.addSetNext(curTag + "/advanced-settings", "setAdvancedSettings"); digester.addSetNext(curTag, "setQSAdminServerConfig"); subTag = "db-object-pool"; digester.addObjectCreate(mainTag + "/" + subTag, DBObjectPoolConfig.class); digester.addObjectCreate(mainTag + "/" + subTag + "/database-connection-set", DatabaseConnectionSet.class); curTag = mainTag + "/" + subTag + "/database-connection-set/database-connection"; digester.addObjectCreate(curTag, DatabaseConnectionConfig.class); digester.addBeanPropertySetter(curTag + "/id", "id"); digester.addBeanPropertySetter(curTag + "/driver", "driver"); digester.addBeanPropertySetter(curTag + "/url", "url"); digester.addBeanPropertySetter(curTag + "/username", "username"); digester.addBeanPropertySetter(curTag + "/password", "password"); digester.addSetNext(curTag, "addDatabaseConnection"); digester.addSetNext(mainTag + "/" + subTag + "/database-connection-set", "setDatabaseConnectionSet"); curTag = mainTag + "/" + subTag + "/db-pool-util"; digester.addBeanPropertySetter(curTag, "dbPoolUtil"); digester.addSetNext(mainTag + "/" + subTag, "setDBObjectPoolConfig"); subTag = "application-configuration"; digester.addObjectCreate(mainTag + "/" + subTag, ApplicationConfiguration.class); digester.addBeanPropertySetter(mainTag + "/" + subTag + "/prompt-type", "promptType"); curTag = mainTag + "/" + subTag + "/" + "property"; digester.addObjectCreate(curTag, Property.class); digester.addBeanPropertySetter(curTag + "/property-name", "name"); digester.addBeanPropertySetter(curTag + "/property-value", "value"); digester.addSetNext(curTag, "addProperty"); digester.addSetNext(mainTag + "/" + subTag, "setApplicationConfiguration"); subTag = "init-server-hooks"; digester.addObjectCreate(mainTag + "/" + subTag, InitServerHooks.class); digester.addCallMethod(mainTag + "/" + subTag + "/class-name", "addClassName", 0); digester.addSetNext(mainTag + "/" + subTag, "setInitServerHooks"); logger.fine("Loading config from xml file : " + input.getAbsolutePath()); QuickServerConfig qsc = (QuickServerConfig)digester.parse(input); qsc.setConfigFile(input.getAbsolutePath()); loadMissingApplicationConfiguration(qsc); QuickServer.setDebugNonBlockingMode(qsc.getAdvancedSettings().getDebugNonBlockingMode()); return qsc; }