summaryrefslogtreecommitdiff
path: root/plugins/ui-designer/src/com/intellij/uiDesigner/XmlWriter.java
blob: e7132efbce2153ff31d718ac4c124e831e241e8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.uiDesigner;

import com.intellij.openapi.util.text.StringUtil;
import com.intellij.uiDesigner.lw.ColorDescriptor;
import com.intellij.uiDesigner.lw.FontDescriptor;
import com.intellij.uiDesigner.lw.StringDescriptor;
import com.intellij.xml.util.XmlStringUtil;
import org.jdom.Attribute;
import org.jdom.Element;
import org.jetbrains.annotations.NonNls;

import java.awt.*;
import java.util.Stack;

/**
 * This is utility for serialization of component hierarchy.
 *
 * @author Anton Katilin
 * @author Vladimir Kondratyev
 */
public final class XmlWriter{
  private static final int INDENT = 2;

  private final Stack<String> myElementNames;
  private final Stack<Boolean> myElementHasBody;
  @NonNls private final StringBuffer myBuffer;

  public XmlWriter(){
    myElementNames = new Stack<String>();
    myElementHasBody = new Stack<Boolean>();
    myBuffer = new StringBuffer();
    myBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
  }

  public String getText(){
    return myBuffer.toString();
  }

  public void writeDimension(final Dimension dimension, @NonNls final String elementName) {
    if (dimension.width == -1 && dimension.height == -1) {
      return;
    }
    startElement(elementName);
    try {
      addAttribute("width", dimension.width);
      addAttribute("height", dimension.height);
    }
    finally {
      endElement();
    }
  }

  public void startElement(@NonNls final String elementName){
    startElement(elementName, null);
  }

  public void startElement(@NonNls final String elementName, final String namespace){
    if (myElementNames.size() > 0) {
      if(!myElementHasBody.peek().booleanValue()){
        myBuffer.append(">\n");
      }
      myElementHasBody.set(myElementHasBody.size()-1,Boolean.TRUE);
    }

    writeSpaces(myElementNames.size()*INDENT);
    myBuffer.append("<").append(elementName);

    if (namespace != null) {
      myBuffer.append(" xmlns=\"").append(namespace).append('"');
    }

    myElementNames.push(elementName);
    myElementHasBody.push(Boolean.FALSE);
  }

  public void endElement() {
    final String elementName = myElementNames.peek();
    final boolean hasBody = myElementHasBody.peek().booleanValue();

    myElementNames.pop();
    myElementHasBody.pop();

    if (hasBody) {
      writeSpaces(myElementNames.size()*INDENT);
      myBuffer.append("</").append(elementName).append(">\n");
    } else {
      myBuffer.append("/>\n");
    }
  }

  /**
   * Helper method
   */
  private void addAttributeImpl(final String name,final String value){
    myBuffer.append(' ').append(name).append("=\"").append(value).append('"');
  }

  /**
   * Helper method
   */
  public void addAttribute(@NonNls final String name, final String value){
    addAttributeImpl(name, StringUtil.convertLineSeparators(XmlStringUtil.escapeString(value, true, false)));
  }

  /**
   * Helper method
   */
  public void addAttribute(@NonNls final String name, final int value){
    addAttributeImpl(name, Integer.toString(value));
  }

  /**
   * Helper method
   */
  public void addAttribute(@NonNls final String name, final boolean value){
    addAttributeImpl(name, Boolean.toString(value));
  }

  public void addAttribute(@NonNls final String name, final Double value){
    addAttributeImpl(name, Double.toString(value));
  }

  public void writeElement(final Element element){
    startElement(element.getName());
    try {
      for (final Object o1 : element.getAttributes()) {
        final Attribute attribute = (Attribute)o1;
        addAttribute(attribute.getName(), attribute.getValue());
      }
      for (final Object o : element.getChildren()) {
        final Element child = (Element)o;
        writeElement(child);
      }
    }
    finally {
      endElement();
    }
  }

  /**
   * Helper method
   */
  private void writeSpaces(final int count){
    for (int i=0; i < count; i++) {
      myBuffer.append(' ');
    }
  }

  public void writeStringDescriptor(final StringDescriptor descriptor,
                                    final String valueAttr,
                                    final String bundleAttr,
                                    final String keyAttr) {
    if(descriptor.getValue() != null){ // direct value
      addAttribute(valueAttr, descriptor.getValue());
      if (descriptor.isNoI18n()) {
        addAttribute(UIFormXmlConstants.ATTRIBUTE_NOI18N, Boolean.TRUE.toString());
      }
    }
    else{ // via resource bundle
      addAttribute(bundleAttr, descriptor.getBundleName());
      addAttribute(keyAttr, descriptor.getKey());
    }
  }

  public void writeColorDescriptor(final ColorDescriptor value) {
    Color color = value.getColor();
    if (color != null) {
      addAttribute(UIFormXmlConstants.ATTRIBUTE_COLOR, color.getRGB());
    }
    else if (value.getSwingColor() != null) {
      addAttribute(UIFormXmlConstants.ATTRIBUTE_SWING_COLOR, value.getSwingColor());
    }
    else if (value.getSystemColor() != null) {
      addAttribute(UIFormXmlConstants.ATTRIBUTE_SYSTEM_COLOR, value.getSystemColor());
    }
    else if (value.getAWTColor() != null) {
      addAttribute(UIFormXmlConstants.ATTRIBUTE_AWT_COLOR, value.getAWTColor());
    }
  }

  public void writeFontDescriptor(final FontDescriptor value) {
    if (value.getSwingFont() != null) {
      addAttribute(UIFormXmlConstants.ATTRIBUTE_SWING_FONT, value.getSwingFont());
    }
    else {
      if (value.getFontName() != null) {
        addAttribute(UIFormXmlConstants.ATTRIBUTE_NAME, value.getFontName());
      }
      if (value.getFontSize() >= 0) {
        addAttribute(UIFormXmlConstants.ATTRIBUTE_SIZE, value.getFontSize());
      }
      if (value.getFontStyle() >= 0) {
        addAttribute(UIFormXmlConstants.ATTRIBUTE_STYLE, value.getFontStyle());
      }
    }
  }

  public void writeInsets(final Insets value) {
    addAttribute(UIFormXmlConstants.ATTRIBUTE_TOP, value.top);
    addAttribute(UIFormXmlConstants.ATTRIBUTE_LEFT, value.left);
    addAttribute(UIFormXmlConstants.ATTRIBUTE_BOTTOM, value.bottom);
    addAttribute(UIFormXmlConstants.ATTRIBUTE_RIGHT, value.right);
  }
}