View Javadoc
1   /**
2    * Copyright 2014 Internet2
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    Copyright (C) 2004-2007 University Corporation for Advanced Internet Development, Inc.
18    Copyright (C) 2004-2007 The University Of Chicago
19  
20    Licensed under the Apache License, Version 2.0 (the "License");
21    you may not use this file except in compliance with the License.
22    You may obtain a copy of the License at
23  
24      http://www.apache.org/licenses/LICENSE-2.0
25  
26    Unless required by applicable law or agreed to in writing, software
27    distributed under the License is distributed on an "AS IS" BASIS,
28    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29    See the License for the specific language governing permissions and
30    limitations under the License.
31  */
32  
33  package edu.internet2.middleware.grouper.xml;
34  import java.io.IOException;
35  import java.io.Writer;
36  
37  import org.apache.commons.lang.StringEscapeUtils;
38  import org.apache.commons.lang.StringUtils;
39  
40  import edu.internet2.middleware.grouper.cfg.GrouperConfig;
41  import edu.internet2.middleware.grouper.exception.GrouperException;
42  
43  /**
44   * Create XML representation of the Groups Registry.
45   * <p/>
46   * @author  blair christensen.
47   * @version $Id: XmlWriter.java,v 1.4 2009-03-23 02:59:25 mchyzer Exp $
48   * @since   1.1.0
49   */
50  class XmlWriter {
51  
52    // PRIVATE INSTANCE VARIABLES //
53    private String  newLine;
54    private String  padding;
55    private Writer  w;
56  
57  
58    // CONSTRUCTORS //
59  
60    // @since   1.1.0
61    protected XmlWriter(Writer w) {
62      this.newLine  = GrouperConfig.NL;
63      this.padding  = GrouperConfig.EMPTY_STRING;
64      this.w        = w;
65    } // protected XmlWriter(w)
66  
67  
68    // PROTECTED INSTANCE METHODS //
69  
70    // Close {@link Writer}.
71    // @since   1.2.0
72    protected void internal_close() 
73      throws  IOException
74    {
75      this.w.close();
76    } // protected void internal_close()
77  
78    // Return a XML comment.
79    // @since   1.2.0
80    protected String internal_comment(String s) 
81    {
82      return "<!-- " + StringEscapeUtils.escapeXml(s) + " -->";
83    } // protected String internal_comment(s)
84  
85    // @since   1.2.0
86    protected void internal_indent() {
87      this.padding = this._getPadding() + "  ";
88    } // protected void internal_indent();
89  
90    // Output string to {@link Writer} with leading padding.
91    // @since   1.2.0
92    protected void internal_put(String s) 
93      throws  IOException
94    {
95      this.w.write( this._getPadding() + s );
96    } // protected void internal_put(s)
97  
98    // Output platform-appropriate newline to {@link Writer}.
99    // @since   1.2.0
100   protected void internal_puts() 
101     throws  IOException
102   {
103     this.w.write(this.newLine);
104   } // protected void internal_puts(s)
105   
106   /**
107    * Output string to {@link Writer} with platform-appropriate newline and leading padding.
108    * @param s
109    * @throws IOException
110    */
111   protected void internal_puts(String s) 
112     throws  IOException {
113     //nothing to do if blank, right?
114     if (StringUtils.isBlank(s)) {
115       return;
116     }
117     this.internal_put( s + this.newLine );
118   } 
119 
120   // @since   1.2.0
121   protected void internal_undent() 
122     throws  GrouperException
123   {
124     if (this._getPadding().length() < 2) {
125       throw new GrouperException(
126         "CANNOT UNDENT WHEN PADDING SIZE IS " + this._getPadding().length()
127       );
128     }
129     this.padding = this._getPadding().substring(2);
130   }
131   
132 
133   // PRIVATE INSTANCE METHODS //
134 
135   // @since   1.1.0
136   private String _getPadding() {
137     return this.padding;
138   } // private String _getPadding()
139 
140 } // class XmlWriter
141