¿ä±â¼´Â XMLBeans¿¡ ´ëÇÏ¿© ¾Ë¾Æº¼±î ÇÑ´Ù. XMLBeans~ óÀ½ µé¾îº¸´Â »ç¶÷µµ ÀÖÀ» °ÍÀÌ°í
ÀÚÄ«¸£Å¸ ÇÁ·ÎÁ§Æ®¿¡ °ü½ÉÀÌ ¸¹Àº »ç¶÷À̶ó¸é ÀÎÅ¥º£ÀÌÆÃÀÇ ¿µ¿ª¿¡¼ ÇÑ ºÎºÐÀ» Â÷ÁöÇÏ¸ç ±×¸®
¸¹Àº µµÅ¥¸ÕÆ®¸¦ °¡Áö°í ÀÖÁö ¾Ê¾Æ »ç¿ëÇÏ°íÀÚ ÇßÀ¸³ª ÇÒ ¼ö ¾ø´Â »ç¶÷µé ¶ÇÇÑ ÀÖÀ» °ÍÀÌ´Ù.
±×·¯¸é ¿©±â¼ °£´ÜÇÏ°Ô XMLBeans°¡ ´ëü ¹«¾ùÀ» ÇÏ´Â °ÍÀÌ¸ç ¾î¶»°Ô ½á¾ßÇÏ´Â Áö¿¡ ´ëÇØ °£·«ÇÏ°Ô
¾Ë¾Æº¸µµ·Ï ÇÏÀÚ.
¢º XMLBeans
À̸§À» º¸¸é ±×´ë·Î ÀüÇØÁö´Â ´À³¦ÀÌ ¾î¶°ÇÑ°¡? XML? Beans?
"Èì~ XML°ú BeanÀ̶ó... XML-->Bean?" À̶ó°í »ý°¢ÇÏ¸é ´ëÃæÀº ¶§·Á¸ÂÃá°ÍÀ̶ó°í º»´Ù.
À̱ÛÀ» Àд ´ç½ÅÀº jakarta-commonsÀÇ digester¸¦ º¸¾Ò°í ÄÚµùÀ» Çغ¸¾Ò´Â°¡?
±âº»ÀûÀÎ digester´Â ½ÇÁ¦ jakarta¿¡¼ »ç¿ëµÇ´Â xml-configÀÇ javaÃø object·Î º¯È¯Çϱâ À§ÇÏ¿© óÀ½¿¡
strut¿¡ Æ÷ÇԵǾî ÀÖ´Ù°¡ °øÅëÀûÀÎ Çʿ伺¿¡ ÀÇÇÏ¿© ¹ÛÀ¸·Î »Ì¾ÆÁ® ³ª¿ÔÀ½À» ¸ÕÀú ¾Ë°í ÀÖ´Ù¸é
Á¶±ÝÀº ¼³¸íÀ» µè±â°¡ ¼ö¿ùÇØÁüÀ» ¾Ë ¼ö ÀÖÀ» °ÍÀÌ´Ù.
DigesterÀÇ ±âÃÊÀûÀÎ ¸ðŵµ ¹¹ sun¿¡¼ °¡Áö°í ÀÖ´Â JAXB(Java API for XML Binding)ÀÇ ±â¼úÀÌ´Ù.
Áï xmlÀ» Àоîµé¿© ¾î¶»°Ô ÀÚ¹ÙÃøÀÇ object·Î º¯È¯½Ãų ¼ö ÀÖ´À³Ä´Ù.
XMLBeans¿¡¼ intro·Î ÇѸ¶µð·Î ¾î¶»°Ô ÀûÇôÀÖ´À³Ä ÇÏ¸é ¹Ù·Î ´ÙÀ½°ú °°´Ù.
"It is an XML-Java binding tool"
Àú³ðÀº XML-Java¹ÙÀεù ÅøÀ̿ýôÙ.¶ó°í ¸»ÀÌ´Ù. ±×·³ digester¿Í º°¹Ý ¹«½¼ Â÷ÀÌ°¡ ÀÖ°í, JDOMÇÏ°í´Â
¶Ç ¹«½¼ Â÷ÀÌ°¡ ÀֳĴ °ÍÀε¥...Â÷ÀÌ? ¾î¸¶¾î¸¶ÇÏ´Ù.(³ªÁß¿¡ ¼³¸í)
¿ì¼± ¿Ö ÀÌ·± ÀÎÅ¥º£ÀÌƼÀÌ ÇÁ·ÎÁ§Æ®°¡ ³ª¿Ô´Â°¡? 2003³â 9¿ù¿¡ David Bau¶õ ¶È¶ÈÇÑ »ç¶÷ÀÌ ¸Í±Û¾î¼
BEAÀÇ workshop¿¡ Æ÷ÇÔµÇ¾î »ç¿ëµÇ°í ÀÖ¾ú´Âµ¥ ±×°É apache¿¡ submitÇÏ¿© ÇÁ·ÎÁ§Æ®·Î ¸¸µé¾î
¿ÀǼҽº·Î ó¸®Çϱâ·Î ¸¾À» ¸Ô¾ú´Ù³ª ¾î·´Ù³ª~
±×·¡¼ Áö±ÝÀÇ ÀÚÄ«¸£Å¸ÀÇ xml°ü·Ã ÀÎÅ¥º£ÀÌÆà ÇÁ·ÎÁ§Æ®·Î °³¹ßµÇ°í ÀÖÀ¸¸ç ÇöÀç 1.0¹öÁ¯ÀÌ »ç¿ëµÇ°í ÀÖÁö¸¸Àº
»ç½Ç dev2devÀÇ bea»çÀÌÆ®¿¡¼ ¸ÕÀú ¸¸³ªº¼ ¼ö ÀÖ¾ú´ø ÆÐÅ°Áö ¶Ç´Â ÅøÀ̶ó°í ÇÒ ¼ö ÀÖ´Ù.
¢ºXMLBeans ´ëü ¹¹ÇÏ´Â ³ðÀΰí?
¾ê°¡ XML-javaÀÇ bindingÀ» ÇØÁÖ±ä Çϴµ¥ À§¿¡¼ °£·«ÇÏ°Ô À̾߱âÇÑ digester¿Í´Â È®¿¬ÇÑ Â÷À̸¦ º¸ÀÌ´Â°Ô ÀÖ´Ù.
Digester°°Àº °æ¿ì´Â ½ÇÁ¦ ¹ÙÀεùµÉ object¿¡ ´ëÇÏ¿© °³¹ßÀÚ°¡ ¿³ª°Ô ÄÚµùÇØÁà¾ß ÇÑ´Ù´Â °ÍÀÌ´Ù.
ÇÏÁö¸¸ XMLBeansÀÇ °æ¿ì ¸ðµç ³»¿ëÀº »ý·«µÇ°í xml¿¡ ´ëÇÑ ¹®¼¿Í ȯ°æÆÄÀϸ¸ ÀÖÀ¸¸é factoryÀÇ ÇüÅ·Î
Ŭ·¡½ºÀÇ ¼Ò½º¸¦ ¸ðµÎ »ý¼ºÇÏ¿© ÆÐŰ¡ ÇÑÈÄ jar¸¦ ¸¸µé¾î ¹èÆ÷ÇÒ ¼ö ÀÖµµ·Ï ±¸¼ºÇØÁشٴ Ư¡À» °¡Áö°í ÀÖ´Ù.
¶ÇÇÑ xmlÀÇ webserviceÇüÅÂÀÎ wsdl±îÁöµµ ´Ù ¾Ë¾Æ¼ ÇØÁØ´Ù.
parsing? query?
°ÆÁ¤¸»¶ó. ¸ðµç ÇüÅ°¡ classÀÇ ¸Þ¼Òµå·Î ÀÚµ¿»ý¼ºµÇ¾î Áö¿øµÇ°í ÀÖÀ¸¸ç, ±âº» xsdÆÄÀÏ°ú xsd¿¡ ´ëÇÑ config·Î
¼Ò½ºÀÇ ÆÐŰ¡ À§Ä¡, ¹èÆ÷ÇüŵîÀ» ¸ðµÎ °ü¸®ÇÏ¿© »ý¼ºÇÒ ¼ö ÀÖµµ·Ï ÇØÁÖ´Â ³»°¡ º¸±â¿£
"XMLÀ» ¾ÆÁÖ ÆíÇÏ°í, ¾ÆÁÖ »ç¿ëÇϱ⠽¬°í, °£ÆíÇÏ°Ô ÀÚ¹Ù¿Í ¸ÅÇνÃÄÑÁÖ´Â ÁÁÀº ³ð"
ÀÌ´Ù.
¶ÇÇÑ ¹¹°¡ ´Ù¸¥ bindingÆÐÅ°Áö¿Í Ʋ¸®³ÄÇϸé
1. ¿Ïº®ÇÑ XMLSchemaÁö¿øÇϸç
2. ¿Ïº®ÇÑ InfosetÀ» ÁöÅ°°í ÀÖ´Ù.
Áï XMLSchema°¡ °¡Áö°í ÀÖ´Â ÁÖ¿äÇÑ ±â´ÉÀ» ¸ðµÎ ÀÚ¹ÙÃøÀÇ ¿ÀºêÁ§Æ®·Î º¯È¯½Ãų¼ö ÀÖ´Â ¹æ¹ýÀ» Á¦°øÇÑ´Ù.
¶ÇÇÑ XML½ºÅ°¸¶ º¯°æ¿¡ ÀÇÇÑ °³¹ßº¯°æÀÌ ÀÖÀ» ½Ã¿¡µµ º°¹Ý ´Ù¸£°Ô ÇؾßÇÒ ÀÏÀÌ ¾ø´Ù´Â °ÍÀÌ´Ù.
¶ÇÇÑ ¸ðµç µ¥ÀÌÅ͵éÀÌ streamÀÇ ÇüÅ·Π»ç¿ëµÇ´Â °ÍÀÌ ¾Æ´Ï¶ó ¸ðµÎ ¸Þ¸ð¸®¿¡¼ »ç¿ëµÇ´Â °ÍÀ̸ç,
¸ðµç xmlÀÇ µ¥ÀÌÅÍ Å¸ÀÔÀÌ ÀÌ»Ú°Ô ÀÚ¹ÙÃøÀ¸·Î ½º¸çµé°Ô µÇ¾î ÀÖ´Ù.
ÇöÀç xml apache¿¡¼ cvs¸¦ ÀÌ¿ëÇÑ ´Ù¿î·Îµå¸¸À» ¹ÞÀ» ¼ö ÀÖÀ¸¸ç, BEA System¿¡¼ ¹Þ´Â °ÍÀº À̹Ì
weblogic workshop¿¡ Æ÷ÇԵǾî ÀÖ´Â ÆÐÅ°Áö¸íÀÌ ´Ù¸¥ xml beanÀÇ ¶óÀ̺귯¸®¸¦ ¹ÞÀ» ¼ö ÀÖ´Ù.
¢º°³¹ßÀÇ ¼ø¼
¸ÕÀú °³¹ßÀ» ¾î¶»°Ô ÇÏ¸ç ¾î¶² API¸¦ ½á¾ß ÇÏ´ÂÁö¸¦ È®ÀÎÇغ¸µµ·Ï Çغ¸ÀÚ.
1. ¸ÕÀú »ç¿ëÇÏ°Ô µÉ XML Schema¿Í xsd¿¡ ´ëÇÑ configÆÄÀÏÀ» ±¸¼ºÇϵµ·Ï ÇÑ´Ù.
XMLÆÄÀÏÀÇ ±¸Á¶¸¦ DTD°¡ ¾Æ´Ñ ½ºÅ°¸¶¸¦ ÅëÇÏ¿© ±¸ÇöÀ» ÇÏ°í ÇØ´ç xsdÆÄÀÏÀÌ ÆÄ½ÌµÇ¾î ¾î¶°ÇÑ ÀÚ¹ÙÃø
¼Ò½º·Î ¸¸µé¾î¼ º¯È¯µÉÁö¸¦ °áÁ¤ÇÒ xsdconfigÆÄÀÏÀ» ±¸¼ºÇϵµ·Ï ÇÑ´Ù.
source 1 : mappings.xsd
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mp="http://www.javapattern.info/mappings"
targetNamespace="http://www.javapattern.info/mappings"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="mappings">
<xs:complexType>
<xs:sequence>
<xs:element name="url-mapping" type="mp:url-mapping" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="url-mapping">
<xs:sequence>
<xs:element name="class-name" type="xs:string"/>
<xs:element name="method-name" type="xs:string"/>
<xs:element name="form-name" type="xs:string"/>
</xs:sequence>
<xs:attribute name="uri" type="xs:string"/>
</xs:complexType>
</xs:schema>
|
xsd¿¡ ´ëÇÑ configÆÄÀÏÀ» ¸¸µç´Ù. ÀÌ ÆÄÀÏÀº generating µÇ´Â ¼Ò½º¿¡ ´ëÇÑ È¯°æ¼³Á¤À» ´ã´Â´Ù.
package : ¼Ò½ºÀÇ ÆÐÅ°Áö¸í
prefix : Java Source Prefix
suffix : Java Source Suffix
source 2 : mappings.xsdconfig
<xb:config xmlns:xb="http://xml.apache.org/xmlbeans/2004/02/xbean/config"
xmlns:mp="http://www.javapattern.info/mappings">
<xb:namespace uri="http://www.javapattern.info/mappings">
<xb:package>com.javapattern.controller.mappings</xb:package>
</xb:namespace>
<xb:namespace uri="##any">
<xb:prefix>Xml</xb:prefix>
<xb:suffix>Bean</xb:suffix>
</xb:namespace>
</xb:config>
|
2. ºôµå´Â ½ÃµµÇÑ´Ù.
ȯ°æ¼³Á¤¿¡ ÀâÇôÀÖ¾î¾ß Çϴ°ÍÀº xbean.jar(XMLBeans±¸Çöü), jsr173_api.jar(Sun XML stream±¸Çöü)
build.xml
<project name="Xbean" default="mappings.jar" basedir=".">
<property name="build.compiler" value="javac1.4"/>
<property name="product.version" value="1.0.0"/>
<property name="repackage_arg" value=""/>
<property name="schema_compiler" value="org.apache.xmlbeans.impl.tool.SchemaCompiler"/>
<property name="bootstrap_schema_compiler"
value="org.apache.xmlbeans.impl.tool.SchemaCompiler"/>
<path id="scomp.run.path">
<pathelement location="lib/xbean.jar"/>
<pathelement location="lib/jsr173_api.jar"/>
<fileset dir='lib'>
<include name='**/*.jar'/>
<include name='**/*.zip'/>
</fileset>
</path>
<target name="mappings.check">
<uptodate property="mappings.notRequired" targetfile="lib/mappings.jar">
<srcfiles dir="src/com/javapattern/controller/mappings" includes="**/*"/>
<srcfiles dir="lib" includes="xbean.jar"/>
</uptodate>
</target>
<target name="mappings.jar" depends="mappings.check" unless="mappings.notRequired">
<java classname="${schema_compiler}" classpathref="scomp.run.path" fork="true" failonerror="true">
<jvmarg value="-ea"/>
<arg line="-out lib/mappings.jar -src src schemas"/>
</java>
</target>
<taskdef name="xmlbean"
classname="org.apache.xmlbeans.impl.tool.XMLBean"
classpath="lib/xbean.jar"/>
<target name="generate.jar" depends="mappings.check" unless="mappings.notRequired">
<xmlbean classgendir="build" classpath="lib/xbean.jar;lib/jsr173_api.jar" failonerror="true">
<fileset file="schema" includes="mappings.xsdconfig"/>
<fileset file="schema" includes="mappings.xsd"/>
</xmlbean>
</target>
</project>
|
À§ÀÇ ºôµåÆÄÀÏÀ» º¸°ÔµÇ¸é 2°¡ÁöÀÇ ¸í·ÉÀ¸·Î ½ÇÁ¦ schema compiler¸¦ ±¸»çÇÏ°Ô µÇ´Âµ¥ taskdefineµÇ¾îÁø
xmlbeanű״ ÇöÀç µ¿ÀÛÀÌ µÇÁö ¾Ê°í ÀÖÀ¸¸ç ±âº» mapping.jar targetÀ» ÀÌ¿ëÇÏ¿© ¸¸µé°í ÀÖ´Ù.
³»¿ëÀº ¼Ò½º¿Í ÄÄÆÄÀÏµÈ libÀÇ ÆÄÀϸíÀÌ ¹«¾ùÀÎÁö¸¦ È®ÀÎÇÏ¿© üũÇϵµ·Ï ÇÏ°í ÀÖÀ¸¸ç Á¤»óÀûÀ¸·Î ºôµåµÇ¾îÁú
°æ¿ì srcµð·ºÅ丮¿¡ ÇØ´ç xmlÀÇ Ã³¸®¼Ò½º°¡ »ý±â¸ç, libµð·ºÅ丮¿¡ mappings.jarÆÄÀÏÀÌ src¸¦ ÄÄÆÄÀÏÇÑ
°á°ú·Î »ý±â°Ô µÈ´Ù.
¿©·¯ºÐµéÀº °¡Àå ¸ÕÀú ¼Ò½º¸¦ È®ÀÎÇϱ⠹ٶõ´Ù.
3. ÀÌÁ¦ ó¸®ÇÁ·Î±×·¥À» Çϳª ¸¸µé¾î º¸µµ·Ï ÇÑ´Ù.
MappingTest.java
import java.io.*;
import com.javapattern.controller.mappings.*;
import org.apache.xmlbeans.*;
public class MappingTest {
public static void main(String args[]) {
File xmlFile = new File(args[0]);
try {
XmlMappingsDocumentBean doc = (XmlMappingsDocumentBean)
XmlObject.Factory.parse(xmlFile);
System.out.println(doc.xmlText());
XmlUrlMappingBean[] bean = doc.getMappings().getUrlMappingArray(); //doc.getMappings().
for ( int i=0; i<bean.length; i++ ) {
System.out.println("--- " + i + " ---");
System.out.println(bean[i].getUri());
System.out.println(bean[i].getClassName());
System.out.println(bean[i].getFormName());
System.out.println(bean[i].getMethodName());
}
doc.save(new File("backup-mappings.xml"));
} catch (XmlException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
|
À§ÀÇ ÇÁ·Î±×·¥Àº °£´ÜÇÏ°Ô ·çÇÁ¸¦ µ¹¸ç ÀÚµ¿À¸·Î »ý¼ºµÈ xmlÆÄÀÏÀÇ ³»¿ëÀ» º¸¿©ÁÖ°Ô µÇ´Âµ¥ ¾Æ·¡¿¡ Å×½ºÆ®
xmlÀ» Àûµµ·Ï ÇÑ´Ù.
source : mappings.xml
<mappings xmlns="http://www.javapattern.info/mappings">
<url-mapping uri="guestBookList.xrq">
<class-name>com.javapattern.wli.web.action.GuestBookAction</class-name>
<form-name>com.javapattern.wli.web.form.GuestBookForm</form-name>
</url-mapping>
<url-mapping uri="guestBookList">
<class-name>com.javapattern.wli.web.action.XmlGuestBookMapAction</class-name>
<method-name>getGuestBookList</method-name>
<form-name>com.javapattern.wli.web.form.GuestBookForm</form-name>
</url-mapping>
<url-mapping uri="guestBookList2">
<class-name>com.javapattern.wli.web.action.XmlGuestBookAction</class-name>
<method-name>getGuestBookList</method-name>
<form-name>com.javapattern.wli.web.form.GuestBookForm</form-name>
</url-mapping>
<url-mapping uri="testWAFCall">
<class-name>com.javapattern.wli.web.action.WAFCallActionTest</class-name>
<method-name>test</method-name>
</url-mapping>
<url-mapping uri="testWAFCall2">
<class-name>com.javapattern.wli.web.action.WAFCallActionTest</class-name>
<method-name>test2</method-name>
<form-name>com.javapattern.wli.web.form.GuestBookForm</form-name>
</url-mapping>
<url-mapping uri="getGuestBookList">
<class-name>com.javapattern.wli.web.action.GuestBookAction</class-name>
<method-name>getGBookList</method-name>
<form-name>com.javapattern.wli.web.form.GuestBookForm</form-name>
</url-mapping>
</mappings>
|
Test
java -classpath .;lib\mappings.jar;lib\xbean.jar;lib\jsr173_api.jar MappingTest config/mapping.xml
À§ÀÇ ³»¿ëÀ¸·Î ó¸®ÇÏ°Ô µÇ¸é ¸ðµç ³»¿ëÀÌ ÀÚµ¿À¸·Î »ý¼ºµÈ beanÀÇÇÏ¿© ¸Þ¸ð¸®¿¡¼ 󸮵ǰí
ÀúÀåµÈ ³»¿ëÀ» arrayÀÇ ÇüÅ·ΠÁ¢±ÙÇÏ¿© °ªÀ» °¡Á®¿Àµµ·Ï ÇÑ ÈÄ ÆÄÀÏ¿¡ backÀÇ À̸§À¸·Î ÀúÀåÇÏ´Â
¿¹Á¦¸¦ º¸¿©ÁÖ°í ÀÖ´Ù.
ÀÀ¿ë¿¡ ´ëÇÑ ºÎºÐÀº ¿©·¯ºÐµé¿¡°Ô ¸Ã±âµµ·Ï ÇÏ¸ç ¼Ò½º¸¦ ´Ù¿î¹ÞÀ» ¼ö ÀÖ´Â ¸µÅ©´Â ´ÙÀ½°ú °°´Ù.
XMLBeanTest.zip
´Ü±µ.
|