001: /*
002: * Copyright (C) 2007 XStream Committers.
003: * All rights reserved.
004: *
005: * The software in this package is published under the terms of the BSD
006: * style license a copy of which has been included with this distribution in
007: * the LICENSE.txt file.
008: *
009: * Created on 14. May 2007 by Guilherme Silveira
010: */
011: package com.thoughtworks.xstream.builder;
012:
013: import java.util.ArrayList;
014: import java.util.List;
015:
016: import com.thoughtworks.xstream.ReadOnlyXStream;
017: import com.thoughtworks.xstream.XStream;
018: import com.thoughtworks.xstream.builder.processor.AbsoluteReferencesProcessor;
019: import com.thoughtworks.xstream.builder.processor.AliasFieldProcessor;
020: import com.thoughtworks.xstream.builder.processor.AliasTypeProcessor;
021: import com.thoughtworks.xstream.builder.processor.ConfigProcessor;
022: import com.thoughtworks.xstream.builder.processor.ConverterProcessor;
023: import com.thoughtworks.xstream.builder.processor.FieldConfigProcessor;
024: import com.thoughtworks.xstream.builder.processor.IdReferencesProcessor;
025: import com.thoughtworks.xstream.builder.processor.IgnoreFieldProcessor;
026: import com.thoughtworks.xstream.builder.processor.ImplementedByProcessor;
027: import com.thoughtworks.xstream.builder.processor.NoReferencesProcessor;
028: import com.thoughtworks.xstream.builder.processor.TypeConfigProcessor;
029: import com.thoughtworks.xstream.builder.processor.annotations.AnnotatedTypeProcessor;
030: import com.thoughtworks.xstream.converters.Converter;
031:
032: /**
033: * The base xstream builder. This is xstream's new entrypoint. Instantiate a builder, configure it
034: * and invoke buildXStream at the end.
035: *
036: * @author Guilherme Silveira
037: * @since upcoming
038: */
039: public class XStreamBuilder {
040:
041: private final List childrenNodes = new ArrayList();
042:
043: protected TypeConfig handle(Class type) {
044: TypeConfig classConfig = new TypeConfig(type);
045: childrenNodes.add(classConfig);
046: return classConfig;
047: }
048:
049: public ReadOnlyXStream buildXStream() {
050: XStream instance = createBasicInstance();
051: for (int i = 0; i < childrenNodes.size(); i++) {
052: ConfigProcessor node = (ConfigProcessor) childrenNodes
053: .get(i);
054: node.process(instance);
055: }
056: return new ReadOnlyXStream(instance);
057: }
058:
059: /**
060: * Extension point to allow lower-level programmers to create their own xstream instance.
061: * @return the xstream instance to configure and wrap
062: */
063: protected XStream createBasicInstance() {
064: return new XStream();
065: }
066:
067: protected TypeConfigProcessor alias(String alias) {
068: return new AliasTypeProcessor(alias);
069: }
070:
071: protected FieldConfigProcessor as(String alias) {
072: return new AliasFieldProcessor(alias);
073: }
074:
075: protected TypeConfigProcessor ignores(String fieldName) {
076: return new IgnoreFieldProcessor(fieldName);
077: }
078:
079: protected TypeConfigProcessor implementedBy(
080: Class defaultImplementation) {
081: return new ImplementedByProcessor(defaultImplementation);
082: }
083:
084: protected FieldConfig field(String fieldName) {
085: return new FieldConfig(fieldName);
086: }
087:
088: protected XStreamBuilder register(ConfigProcessor[] nodes) {
089: for (int i = 0; i < nodes.length; i++) {
090: ConfigProcessor node = nodes[i];
091: childrenNodes.add(node);
092: }
093: return this ;
094: }
095:
096: protected XStreamBuilder register(ConfigProcessor node) {
097: childrenNodes.add(node);
098: return this ;
099: }
100:
101: protected ConfigProcessor converter(Converter converter) {
102: return new ConverterProcessor(converter);
103: }
104:
105: protected TypeConfigProcessor annotated() {
106: return new AnnotatedTypeProcessor();
107: }
108:
109: protected ConfigProcessor absoluteReferences() {
110: return new AbsoluteReferencesProcessor();
111: }
112:
113: protected ConfigProcessor idReferences() {
114: return new IdReferencesProcessor();
115: }
116:
117: protected ConfigProcessor noReferences() {
118: return new NoReferencesProcessor();
119: }
120:
121: protected void with(ConfigProcessor processor) {
122: this.childrenNodes.add(processor);
123: }
124:
125: }
|