1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package net.sf.ehcache.config.generator;
18
19 import java.io.ByteArrayOutputStream;
20 import java.io.PrintWriter;
21
22 import net.sf.ehcache.config.CacheConfiguration;
23 import net.sf.ehcache.config.Configuration;
24 import net.sf.ehcache.config.generator.model.NodeElementVisitor;
25 import net.sf.ehcache.config.generator.model.XMLGeneratorVisitor;
26 import net.sf.ehcache.config.generator.model.elements.CacheConfigurationElement;
27 import net.sf.ehcache.config.generator.model.elements.ConfigurationElement;
28
29 /***
30 * Utility class with static methods for generating configuration texts in different ways based on input
31 *
32 * <p />
33 *
34 * @author <a href="mailto:asanoujam@terracottatech.com">Abhishek Sanoujam</a>
35 *
36 */
37 public abstract class ConfigurationUtil {
38
39 /***
40 * Generates Configuration text from a {@link Configuration}
41 *
42 * @param configuration
43 * the configuration
44 * @return text representing the {@link Configuration}
45 */
46 public static String generateCacheManagerConfigurationText(Configuration configuration) {
47 ByteArrayOutputStream baos = new ByteArrayOutputStream();
48 PrintWriter out = new PrintWriter(baos);
49 XMLGeneratorVisitor configGenerator = new XMLGeneratorVisitor(out);
50 visitConfiguration(configuration, configGenerator);
51 out.flush();
52 out.close();
53 return baos.toString();
54 }
55
56 /***
57 * package protected access so that tests can have access
58 *
59 * @param configuration
60 * @param visitor
61 */
62 static void visitConfiguration(Configuration configuration, NodeElementVisitor visitor) {
63 ConfigurationElement configElement = new ConfigurationElement(configuration);
64 configElement.accept(visitor);
65 }
66
67 /***
68 * Generates configuration text for a {@link CacheConfiguration}
69 *
70 * @param cacheConfiguration
71 * the {@link CacheConfiguration}
72 * @return text representing the {@link CacheConfiguration}
73 */
74 public static String generateCacheConfigurationText(CacheConfiguration cacheConfiguration) {
75 ByteArrayOutputStream baos = new ByteArrayOutputStream();
76 PrintWriter out = new PrintWriter(baos);
77 XMLGeneratorVisitor configGenerator = new XMLGeneratorVisitor(out);
78 visitCacheConfiguration(cacheConfiguration, configGenerator);
79 out.flush();
80 out.close();
81 return baos.toString();
82 }
83
84 /***
85 * package protected access so that tests can have access
86 *
87 * @param cacheConfiguration
88 * @param configGenerator
89 */
90 static void visitCacheConfiguration(CacheConfiguration cacheConfiguration, NodeElementVisitor configGenerator) {
91 CacheConfigurationElement element = new CacheConfigurationElement(null, cacheConfiguration);
92 element.accept(configGenerator);
93 }
94
95 }