site_link1  
site_link2  
site_link3 http://1 
이글은 XStream 교재 중 "Alias Tutorial"을 번역한 것입니다.

문제

고객이 이미 정의된 기본 XML을 가지고 있어서 XStream으로 이것을 읽고 써야 한다고 가정해봅니다.
<blog author="Guilherme Silveira">
<entry>
<title>first</title>
<description>My first blog entry.</description>
</entry>
<entry>
<title>tutorial</title>
<description>
Today we have developed a nice alias tutorial. Tell your friends! NOW!
</description>
</entry>
</blog>
  1. 2분만에 배우기
  2. 별칭(Alias) 배우기
  3. 애노테이션(Annotations) 배우기
  4. 변환기(Converter) 배우기
  5. 객체 스트림(Object Streams) 배우기
  6. 영속화 API(Persistence API) 배우기
  7. JSON 배우기
위의 XML에 기반해서 모델 클래스 몇개를 만들고 이 포멧을 읽고 쓰도록 XStream을 설정할 것입니다.

모델 객체

XML 파일을 표현할 클래스들은 다음과 같습니다. 가장 먼저 간단한 Blog 객체부터 시작해 보겠습니다. 

First things first, the classes which shall represent our xml files are shown next, beginning with a simple Blog:

package com.thoughtworks.xstream;

public class Blog {
private Author author;
private List entries = new ArrayList();

public Blog(Author author) {
this.author = author;
}

public void add(Entry entry) {
entries.add(entry);
}

public List getContent() {
return entries;
}
}

이름을 갖는 저작자 객체는 이렇습니다.

package com.thoughtworks.xstream;

public class Author {
private String name;
public Author(String name) {
this.name = name;
}
public String getName() {
return name;
}
}

블로그에 등록된 글은 타이틀과 설명을 담고 있습니다.

package com.thoughtworks.xstream;

public class Entry {
private String title, description;
public Entry(String title, String description) {
this.title = title;
this.description = description;
}
}

여기서는 getter와 setter를 만들지 않았지만 코드를 이해하기 좋게 하고 싶다거나 그것들을 있었으면 좋겠다고 생각하면 임의로 만들어도 된다.

간단 테스트

간단히 blog 객체를 생성해서 xstream을 사용해 볼 수 있습니다.
public static void main(String[] args) {

Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
teamBlog.add(new Entry("first","My first blog entry."));
teamBlog.add(new Entry("tutorial",
"Today we have developed a nice alias tutorial. Tell your friends! NOW!"));

XStream xstream = new XStream();
System.out.println(xstream.toXML(teamBlog));

}

하지만 결과는 우리가 원하는 것처럼 깔끔하지 않네요.

<com.thoughtworks.xstream.Blog>
<author>
<name>Guilherme Silveira</name>
</author>
<entries>
<com.thoughtworks.xstream.Entry>
<title>first</title>
<description>My first blog entry.</description>
</com.thoughtworks.xstream.Entry>
<com.thoughtworks.xstream.Entry>
<title>tutorial</title>
<description>
Today we have developed a nice alias tutorial. Tell your friends! NOW!
</description>
</com.thoughtworks.xstream.Entry>
</entries>
</com.thoughtworks.xstream.Blog>


객체 별칭

먼저 할 일은 XStream이 com.thoughtworks.xstream.Blog 클래스를 지칭하는 법을 바꾸는 일입니다. 'blog'를 그 클래스를 가리키는 명칭이라고 하고 blog라는 클래스의 별칭을 만들겠습니다.
xstream.alias("blog", Blog.class);

같은 식으로 Entry 클래스의 별칭도 entry라고 하죠.

xstream.alias("entry", Entry.class);

이제 출력 결과는 이렇게 됩니다.

<blog>
<author>
<name>Guilherme Silveira</name>
</author>
<entries>
<entry>
<title>first</title>
<description>My first blog entry.</description>
</entry>
<entry>
<title>tutorial</title>
<description>
Today we have developed a nice alias tutorial. Tell your friends! NOW!
</description>
</entry>
</entries>
</blog>


entries 테그 빼기

이제 내재적 컬렉션(implicit collection)이라고 부르는 것을 구현해보겠습니다. 루트 테그를 표시할 필요가 없는 컬렉션을 가지고 있다면  내재적 컬렉션으로 대응시킬 수 있습니다.

우리의 경우는 entries 테그를 표시하지 않고 단순히 entry 테그들을 연달아 보여주고 싶습니다.

addImplicitCollection 메소드를 간단히 호출하는 것으로 위에 설명한 것 처럼 우리가 entries 테그를 출력하기 원지 않는 다는 것을XStream이 알게 할 수 있습니다.

package com.thoughtworks.xstream;

import java.util.ArrayList;
import java.util.List;

public class Test {

public static void main(String[] args) {

Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
teamBlog.add(new Entry("first","My first blog entry."));
teamBlog.add(new Entry("tutorial",
"Today we have developed a nice alias tutorial. Tell your friends! NOW!"));

XStream xstream = new XStream();
xstream.alias("blog", Blog.class);
xstream.alias("entry", Entry.class);

xstream.addImplicitCollection(Blog.class, "entries");

System.out.println(xstream.toXML(teamBlog));

}
}

addImplicitCollection 메소드 호출 부분을 주목해서 보십시오. 어떤 클레스와 어떤 멤버 변수가 우리가 원하는 데로 행동할지 알려주고 있습니다.

결과는 우리가 원하는 것과 아주 흡사합니다.

<blog>
<author>
<name>Guilherme Silveira</name>
</author>
<entry>
<title>first</title>
<description>My first blog entry.</description>
</entry>
<entry>
<title>tutorial</title>
<description>
Today we have developed a nice alias tutorial. Tell your friends! NOW!
</description>
</entry>
</blog>


속성 별칭

다음 단계는 author 멤버 변수를 XML 속성이 되게 하는 것 입니다. 이를 위해서는 Blog 클래스의 author 필드를 "author" 속성이 되도록 XStream에게 알려줘야 합니다.
                xstream.useAttributeFor(Blog.class, "author");

이제 한가지 문제만 남았습니다. 어떻게 하면 XStream이 Author 객체를 문자열로 변환해서 xml 테그 속성으로 쓰도록 할 수 있을까요?

SimpeValueConver를 구현해서 Author 변환기를 만들어 보겠습니다.
class AuthorConverter implements SingleValueConverter {
}

먼저 XStream에게 처리 할 타입이 뭔지 알려주는 메소드를 구현하겠습니다.


The first method to implement tells XStream which types it can deal with:

        public boolean canConvert(Class type) {
return type.equals(Author.class);
}

다음은 Author 객체에서 문자열을 추출하는데 사용될 메소드입니다.

        public String toString(Object obj) {
return ((Author) obj).getName();
}

세번째는 반대로 String를 주면 Author 객체를 반환하는 메소드입니다.

        public Object fromString(String name) {
return new Author(name);
}

결국 문자열을 객체(여기서는 Author)로 변환하는 일을 담당 할 단일 값 변환기의 전체 모습은 다음과 같습니다.

class AuthorConverter implements SingleValueConverter {

public String toString(Object obj) {
return ((Author) obj).getName();
}

public Object fromString(String name) {
return new Author(name);
}

public boolean canConvert(Class type) {
return type.equals(Author.class);
}

}

이 변환기를 등록해보겠습니다.

public class Test {

public static void main(String[] args) {

Blog teamBlog = new Blog(new Author("Guilherme Silveira"));
teamBlog.add(new Entry("first","My first blog entry."));
teamBlog.add(new Entry("tutorial",
"Today we have developed a nice alias tutorial. Tell your friends! NOW!"));

XStream xstream = new XStream();
xstream.alias("blog", Blog.class);
xstream.alias("entry", Entry.class);

xstream.addImplicitCollection(Blog.class, "entries");

xstream.useAttributeFor(Blog.class, "author");
xstream.registerConverter(new AuthorConverter());

System.out.println(xstream.toXML(teamBlog));

}
}

결과는?

<blog author="Guilherme Silveira">
<entry>
<title>first</title>
<description>My first blog entry.</description>
</entry>
<entry>
<title>tutorial</title>
<description>
Today we have developed a nice alias tutorial. Tell your friends! NOW!
</description>
</entry>
</blog>

useAttributeFor 메소드와 비슷한 기능을 하는 다른 중첩 메소드가 있는데 이 메소드는 (Class, String, String) 처럼 끝 부분에 필드의 별칭인 문자열을 추가로 받습니다. 예를 들어 useAttributeFor(Blog.clas, "author", "auth")라고 하면 author 필드는 "auth"라는 속성에 대응됩니다.

(역자주 : 이 부분은 이 교제가 잘못된 것  같습니다. XStream에는 패러미터를 세 개 받는 useAttributeFor(Class, String, String) 메소드는 없습니다. 대신 aliasAttribute(Class, String,  String) 메소드가 있습니다.)



종합

별칭(alias)과 변환기(converter)를 사용하면 원하는 거의 대부분의 XML을 구성할 수 있는 충분한 능력 갖게 됩니다.

변환기에 대한 교제를 읽는 것도 잊지 마십시오. XStream을 사용하서 만들 수 있는 다른 유형의 변환기들을 볼 수 있습니다.

번호 제목 글쓴이 날짜 조회 수
47 XpressEngine(XE) 에서 엮인글 스팸 방지법 file 황제낙엽 2017.08.25 461
46 [Hibernate] 페이징 처리 정리 황제낙엽 2007.02.26 457
45 [Hibernate] Hibernate 프레임워크를 이용한 효율적인 개발 전략 황제낙엽 2007.01.30 389
» XStream 배우기 : 별칭(Alias) 배우기 황제낙엽 2011.04.20 340
43 JSTL과 Velocity를 활용한 UI 레이어 구현 방법 황제낙엽 2007.01.30 339
42 XStream 배우기 : 변환기(Converter) 배우기 황제낙엽 2011.04.20 298
41 Custom XStream Converter 황제낙엽 2011.04.26 212
40 [Hibernate] Hibernate Tutorial 황제낙엽 2012.11.15 166
39 [Hibernate] Hibernate 와 Ant 에서 composite id 사용하기 예제 file 황제낙엽 2005.11.29 134
38 검색엔진 루씬 Lucene... Analyzer의 선택 황제낙엽 2007.11.27 102
37 Map <-> XML (2) 황제낙엽 2011.04.29 99
36 [Hibernate] 하이버네이트 참조문서 버전 3.2 cr3의 최신 업데이트 한글 번역본 file 황제낙엽 2007.07.03 98
35 벨로시티에서 loop 작성 황제낙엽 2005.12.13 85
34 XE 서버 이전 계획 file 황제낙엽 2018.08.29 80
33 SiteMesh를 이용하여 웹 페이지의 레이아웃을 처리 황제낙엽 2007.08.13 67
32 XML -> (Map) XML 황제낙엽 2011.04.29 61
31 Jakarta Lucene (루씬) - 들어가기 황제낙엽 2007.07.30 57
30 Xdoclet 사용하기 1부 황제낙엽 2006.10.04 45
29 XStream 배우기 : 2분만에 배우는 XStream 황제낙엽 2011.04.20 40
28 Xdoclet 사용하기 2부 황제낙엽 2006.10.04 40