View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  package org.apache.jsieve.util;
20  
21  import java.io.IOException;
22  import java.io.Writer;
23  
24  import org.apache.jsieve.exception.SieveException;
25  import org.apache.jsieve.parser.generated.Node;
26  
27  /**
28   * Output utilities.
29   * These are mostly convenience methods.
30   * More power and flexibility is available when using the objects directly.
31   */
32  public class OutputUtils {
33  
34      /**
35       * <p>Writes the given node as xml.
36       * This convenience method first writes a prolog before calling {@link #toXml(Node, Writer)}.
37       * </p><p>
38       * The output format is a subset of the
39       * <a href='http://tools.ietf.org/html/draft-freed-sieve-in-xml-04'>sieve-in-xml</a>
40       * Internet Draft.
41       * </p>
42       * @param node not null
43       * @param writer not null
44       * @throws IOException when prolog cannot be written
45       * @throws SieveException when script cannot be converted to xml
46       * @see #toXml(Node, Writer)
47       */
48      public static void toXmlDocument(final Node node, final Writer writer) throws IOException, SieveException {
49          writer.append("<?xml version='1.0'?>");
50          toXml(node, writer);
51      }
52      
53      /**
54       * <p>Writes the given node as xml.
55       * Note that the xml will be written as a fragment.
56       * An appropriate prolog must be added to convert this fragment 
57       * to a document.
58       * </p><p>
59       * The output format is a subset of the
60       * <a href='http://tools.ietf.org/html/draft-freed-sieve-in-xml-04'>sieve-in-xml</a>
61       * Internet Draft. Note that this support is experimental.
62       * </p>
63       * @param node not null
64       * @param writer not null
65       * @throws SieveException when script cannot be converted to xml
66       * @see XmlOut
67       * @see SieveToXml
68       * @see SieveHandler
69       */
70      public static void toXml(final Node node, final Writer writer) throws SieveException {
71          final XmlOut out = new XmlOut(writer);
72          final SieveToXml sieveToXml = new SieveToXml();
73          final SieveHandler handler = sieveToXml.build(out);
74          final NodeTraverser traverser = new NodeTraverser();
75          traverser.traverse(handler, node);
76      }
77      
78      /**
79       * <p>Writes the tree rooted at the given node to a Sieve script.
80       * @param node not null
81       * @param writer not null
82       * @throws SieveException whenever the serialization fails
83       */
84      public static void toSieve(final Node node, final Writer writer) throws SieveException {
85          final ToSieveHandlerFactory factory = new ToSieveHandlerFactory();
86          final SieveHandler handler = factory.build(writer);
87          final NodeTraverser traverser = new NodeTraverser();
88          traverser.traverse(handler, node);
89      }
90  }