comparison dbus-d/dsrc/org/freedesktop/dbus/tool/XmlSupport.d @ 0:a5576806d36d

recreate repository without any libs for lightweight repository
author Frank Benoit <benoit@tionex.de>
date Sat, 20 Oct 2007 18:07:18 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:a5576806d36d
1 /**
2 * Author: Frank Benoit
3 * License: Public domain
4 * File type: D programming language
5 */
6 module org.freedesktop.dbus.tool.XmlSupport;
7
8 import tango.core.Array;
9 import tango.io.FileConduit;
10
11 import tango.text.Util;
12 import tango.util.collection.LinkSeq;
13 import mango.xml.sax.DefaultSAXHandler;
14 import mango.xml.sax.parser.teqXML;
15
16 class Element {
17 char[] mName;
18 Element[] mChilds;
19 char[][char[]] mAttributes;
20 char[] mText;
21 Element mParent;
22
23 public this( char[] aName, Element aParent ){
24 mName = aName.dup;
25 mParent = aParent;
26 mText = "";
27 if( aParent ){
28 aParent.addChild( this );
29 }
30 }
31
32 protected void addChild( Element aChild ){
33 mChilds ~= aChild;
34 }
35 public Element createChildElement( char[] aName ){
36 Element result = new Element( aName, this );
37 return result;
38 }
39 public void completeChildElement( char[] aName, Element aChild ){
40 }
41 public void completeYourself(){
42 mText = Text.trim( mText );
43 }
44 public char[] getText(){
45 return mText;
46 }
47 public char[] getName(){
48 return mName;
49 }
50 public void setText( char[] aText ){
51 mText = aText.dup;
52 }
53 public void addAttribute(char[] key, char[] value){
54 mAttributes[ key.dup ] = value.dup;
55 }
56 public bool hasAttribute( char[] aName ){
57 return cast(bool)( aName in mAttributes );
58 }
59 public bool hasChild( char[] aName ){
60 foreach( Element child; mChilds ){
61 if( child.mName == aName ){
62 return true;
63 }
64 }
65
66 return false;
67 }
68 public Element tryGetChild( char[] aName ){
69 foreach( Element child; mChilds ){
70 if( child.mName == aName ){
71 return child;
72 }
73 }
74
75 return null;
76 }
77 public Element getChild( char[] aName ){
78 Element result = tryGetChild( aName );
79 check( result !is null, "Element.getChild mName=%0, aName=%1", mName, aName );
80 return result;
81 }
82 public Element[] getChilds(){
83 return mChilds.dup;
84 }
85 public Element[] getChilds( char[] aName ){
86 Element[] result;
87 foreach( Element child; mChilds ){
88 if( child.mName == aName ){
89 result ~= child;
90 }
91 }
92
93 return result;
94 }
95 public Element tryFindChild( bool delegate( Element aChild ) aDg ){
96 foreach( Element child; mChilds ){
97 if( aDg( child ) ){
98 return child;
99 }
100 }
101
102 return null;
103 }
104 public Element findChild( bool delegate( Element aChild ) aDg ){
105 Element result = tryFindChild( aDg );
106 check( result !is null, "Element.findChild mName=%0, not found", mName );
107 return result;
108 }
109 public char[] getAttribute( char[] aKey ){
110 check( cast(bool)(aKey in mAttributes), "Element %0 does not contain the attribute %1", mName, aKey );
111 return mAttributes[ aKey ].dup;
112 }
113
114 public void checkAllowedChilds( char[][] names... ){
115 foreach( Element child; mChilds ){
116 check( tango.core.Array.find( names, child.mName ) != names.length,
117 "in element %0, a not allowed child node name exist %1", mName, child.mName );
118 }
119 }
120 }
121
122 class Document {
123 private Element mElement;
124 public Element createRootElement( char[] aName ){
125 mElement = new Element( aName, null );
126 return mElement;
127 }
128 public void completeRootElement( char[] aName, Element aChild ){
129 }
130 public void setRoot( Element aRoot ){
131 mElement = aRoot;
132 }
133 public Element getRoot(){
134 return mElement;
135 }
136 }
137
138 public class DomConstructionSaxHandler : DefaultSAXHandler!( char ) {
139 Document mDocument;
140 alias LinkSeq!(Element) TElementList;
141 TElementList mElements;
142
143 public this( Document aDocument ){
144 mElements = new TElementList();
145 mDocument = aDocument;
146 }
147 void startDocument() {
148 //Stdout( "startDocument" ).newline;
149 }
150 void endDocument() {
151 //Stdout( "endDocument" ).newline;
152 }
153 void processingInstruction(char[] target, char[] data) {
154 //Stdout( "processingInstruction" ).newline;
155 }
156 void startElement(char[] name) {
157 if( mElements.size() > 0 ){
158 //Stdout.format( "startElement {0} {1} ", name.utf8, mElements.size() ).newline;
159 Element el = mElements.head().createChildElement( name );
160 mElements.prepend( el );
161 }
162 else{
163 //Stdout( "startRootElement" ~ name.utf8 ).newline;
164 Element el = mDocument.createRootElement( name );
165 mElements.prepend( el );
166 }
167
168 //Stdout( "startElement" ).newline;
169 }
170 void addAttribute(char[] key, char[] value) {
171 Element el = mElements.head();
172 el.addAttribute( key, value );
173 //Stdout( "addAttribute" ).newline;
174 }
175 void endElement(char[] name) {
176 Element child = mElements.take();
177 child.completeYourself();
178 if( mElements.size() > 0 ){
179 Element el = mElements.head();
180 el.completeChildElement( name, child );
181 }
182 else{
183 mDocument.completeRootElement( name, child );
184 }
185
186 //Stdout( "endElement" ).newline;
187 }
188 void comment(char[] text) {
189 //Stdout( "comment" ).newline;
190 }
191 }
192
193 void check( bool aCondition, char[][] aMessage ... ){
194 if( !aCondition ){
195 char[200] buf;
196 throw new Exception( layout( buf, aMessage ));
197 }
198 }
199
200 Element parse(char[] aFileName){
201 auto parser = new XMLReader!(char) ();
202 auto doc = new Document();
203 auto handler = new DomConstructionSaxHandler( doc );
204 parser.parse( new FileConduit( aFileName ), handler);
205 return doc.getRoot();
206 }
207
208
209