|
1 /* |
|
2 * Copyright (c) 2004-2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 using System; |
|
19 using System.Collections; |
|
20 using SymBuildParsingLib.Tree; |
|
21 using SymbianTree; |
|
22 |
|
23 namespace SymBuildParsingLib.Token |
|
24 { |
|
25 public class SymTokenDocument : SymDocument |
|
26 { |
|
27 #region Constructors & destructor |
|
28 public SymTokenDocument() |
|
29 { |
|
30 } |
|
31 #endregion |
|
32 |
|
33 #region API |
|
34 public string ChildrenAsString( bool aIgnoreWhiteSpace, bool aRecurse ) |
|
35 { |
|
36 SymNodeEnumeratorChildren enumerator = new SymNodeEnumeratorChildren( this ); |
|
37 string ret = EnumerateNodesAsString( enumerator, aIgnoreWhiteSpace, aRecurse ); |
|
38 return ret.ToString(); |
|
39 } |
|
40 |
|
41 public SymTokenContainer ExtractTokensAsContainer( bool aIgnoreWhiteSpace, bool aRecurse ) |
|
42 { |
|
43 SymTokenContainer container = new SymTokenContainer(); |
|
44 // |
|
45 SymNodeEnumeratorChildren enumerator = new SymNodeEnumeratorChildren( this ); |
|
46 ExtractTokens( enumerator, aIgnoreWhiteSpace, aRecurse, container ); |
|
47 // |
|
48 return container; |
|
49 } |
|
50 |
|
51 public SymTokenDocument ExtractTokensAsDocument( bool aIgnoreWhiteSpace, bool aRecurse ) |
|
52 { |
|
53 SymTokenDocument doc = new SymTokenDocument(); |
|
54 // |
|
55 SymNodeEnumeratorChildren enumerator = new SymNodeEnumeratorChildren( this ); |
|
56 ExtractTokens( enumerator, aIgnoreWhiteSpace, aRecurse, doc ); |
|
57 // |
|
58 return doc; |
|
59 } |
|
60 #endregion |
|
61 |
|
62 #region Utility methods |
|
63 public string EnumerateNodesAsString( IEnumerable aEnumerable, bool aIgnoreWhiteSpace, bool aRecurse ) |
|
64 { |
|
65 // Flatten the tokens into a container |
|
66 SymTokenContainer container = new SymTokenContainer(); |
|
67 ExtractTokens( aEnumerable, aIgnoreWhiteSpace, aRecurse, container ); |
|
68 |
|
69 // Convert the container to a string |
|
70 string ret = container.CoalescedTokenValue; |
|
71 return ret; |
|
72 } |
|
73 |
|
74 public void ExtractTokens( IEnumerable aEnumerable, bool aIgnoreWhiteSpace, bool aRecurse, SymTokenContainer aContainer ) |
|
75 { |
|
76 foreach( SymNode node in aEnumerable ) |
|
77 { |
|
78 if ( node.HasChildren ) |
|
79 { |
|
80 if ( aRecurse ) |
|
81 { |
|
82 ExtractTokens( node, aIgnoreWhiteSpace, aRecurse, aContainer ); |
|
83 } |
|
84 else |
|
85 { |
|
86 // Ignore - its just a placeholder for child nodes |
|
87 } |
|
88 } |
|
89 else if ( node is SymNodeToken ) |
|
90 { |
|
91 SymNodeToken tokenNode = (SymNodeToken) node; |
|
92 if ( !( aIgnoreWhiteSpace && tokenNode.Token.Class == SymToken.TClass.EClassWhiteSpace ) || aIgnoreWhiteSpace == false ) |
|
93 { |
|
94 aContainer.Append( tokenNode.Token ); |
|
95 } |
|
96 } |
|
97 else if ( NodeIsExtractable( node ) ) |
|
98 { |
|
99 ExtractToContainer( node, aContainer ); |
|
100 } |
|
101 } |
|
102 } |
|
103 |
|
104 public void ExtractTokens( IEnumerable aEnumerable, bool aIgnoreWhiteSpace, bool aRecurse, SymTokenDocument aDocument ) |
|
105 { |
|
106 foreach( SymNode node in aEnumerable ) |
|
107 { |
|
108 if ( node.HasChildren ) |
|
109 { |
|
110 if ( aRecurse ) |
|
111 { |
|
112 SymNode newLevelNode = new SymNodeAddAsChild(); |
|
113 aDocument.CurrentNode.Add( newLevelNode ); |
|
114 aDocument.CurrentNode = newLevelNode; |
|
115 ExtractTokens( node, aIgnoreWhiteSpace, aRecurse, aDocument ); |
|
116 aDocument.MakeParentCurrent(); |
|
117 } |
|
118 else |
|
119 { |
|
120 // Ignore - its just a placeholder for child nodes and we're not recursing |
|
121 } |
|
122 } |
|
123 else if ( node is SymNodeToken ) |
|
124 { |
|
125 SymNodeToken tokenNode = (SymNodeToken) node; |
|
126 if ( !( aIgnoreWhiteSpace && tokenNode.Token.Class == SymToken.TClass.EClassWhiteSpace ) || aIgnoreWhiteSpace == false ) |
|
127 { |
|
128 SymNodeToken copy = new SymNodeToken( tokenNode.Token ); |
|
129 aDocument.CurrentNode.Add( copy ); |
|
130 } |
|
131 } |
|
132 else if ( NodeIsExtractable( node ) ) |
|
133 { |
|
134 ExtractToDocument( node, aDocument ); |
|
135 } |
|
136 } |
|
137 } |
|
138 #endregion |
|
139 |
|
140 #region Internal framework API |
|
141 protected virtual void ExtractToContainer( SymNode aNode, SymTokenContainer aContainer ) |
|
142 { |
|
143 } |
|
144 |
|
145 protected virtual void ExtractToDocument( SymNode aNode, SymTokenDocument aDocument ) |
|
146 { |
|
147 } |
|
148 |
|
149 protected virtual bool NodeIsExtractable( SymNode aNode ) |
|
150 { |
|
151 return false; |
|
152 } |
|
153 #endregion |
|
154 } |
|
155 } |