KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > jcr > impl > util > XMLWriter


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5
6 package org.exoplatform.services.jcr.impl.util;
7
8 import java.util.Map JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.Stack JavaDoc;
11 import java.util.Properties JavaDoc;
12 import java.util.HashMap JavaDoc;
13
14 /**
15  * Created by The eXo Platform SARL .
16  *
17  * @author <a HREF="mailto:geaz@users.sourceforge.net">Gennady Azarenkov</a>
18  * @version $Id: XMLWriter.java,v 1.4 2004/08/18 00:09:07 benjmestrallet Exp $
19  */

20
21 public class XMLWriter {
22
23   private boolean nsWritten;
24   private Map JavaDoc namespaces;
25   private StringBuffer JavaDoc buffer;
26   private Stack JavaDoc nodes;
27
28   public XMLWriter(Map JavaDoc namespaces) {
29     this.buffer = new StringBuffer JavaDoc();
30     this.namespaces = namespaces;
31     this.nodes = new Stack JavaDoc();
32     this.nsWritten = false;
33   }
34
35   public XMLWriter() {
36     this(new HashMap JavaDoc());
37   }
38
39   public void startElement(String JavaDoc qname, Properties JavaDoc attrs) {
40     Iterator JavaDoc keys;
41     buffer.append("<" + qname);
42     if (!nsWritten) {
43       keys = namespaces.keySet().iterator();
44       while (keys.hasNext()) {
45         String JavaDoc key = (String JavaDoc) keys.next();
46         String JavaDoc value = (String JavaDoc) namespaces.get(key);
47         writeAttribute("xmlns:" + key, value);
48       }
49       nsWritten = true;
50     }
51     if (attrs != null) {
52       keys = attrs.keySet().iterator();
53       while (keys.hasNext()) {
54         String JavaDoc key = (String JavaDoc) keys.next();
55         String JavaDoc value = (String JavaDoc) attrs.get(key);
56         writeAttribute(key, value);
57       }
58     }
59     if (!nodes.empty()) {
60       ((Context) nodes.peek()).isOpen = false;
61     }
62     nodes.push(new Context(qname));
63     buffer.append(">");
64   }
65
66   public void endElement() {
67     Context curNode;
68     if (!nodes.empty())
69       curNode = (Context) nodes.pop();
70     else
71       throw new RuntimeException JavaDoc("Unexpected Empty Stack at End element !!");
72
73 // TODO
74
// if(curNode.isOpen)
75
// buffer.append("/>");
76
// else
77
buffer.append("</" + curNode.nodeName + ">");
78   }
79
80   private void writeAttribute(String JavaDoc qname, String JavaDoc value) {
81     buffer.append(" " + qname + "=\"" + value + "\"");
82   }
83
84   public void writeText(String JavaDoc text) {
85     if (!nodes.empty()) {
86       if (text.length() > 0)
87         ((Context) nodes.peek()).isOpen = false;
88     } else
89       throw new RuntimeException JavaDoc("Unexpected Empty Stack at Text '" + text + "' !!!");
90     buffer.append(text);
91   }
92
93   public byte[] getBytes() {
94     return buffer.toString().getBytes();
95   }
96
97   public String JavaDoc toString() {
98     return buffer.toString();
99   }
100
101   private class Context {
102     private Context(String JavaDoc nodeName) {
103       this.nodeName = nodeName;
104       this.isOpen = true;
105     }
106
107     private String JavaDoc nodeName;
108     private boolean isOpen;
109   }
110 }
111
Popular Tags