1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package atg.adapter.gsa.xml;
18
19
20
21 import java.io.*;
22 import java.util.*;
23
24 import org.xml.sax.*;
25 import org.xml.sax.helpers.*;
26
27 import atg.adapter.gsa.xml.TemplateParser;
28 import atg.xml.XMLFileEntityResolver;
29 import atg.epub.ImportService;
30
31 import javax.xml.parsers.*;
32
33 /***
34 * ImportFileParser
35 *
36 * File parser for startSQLRepository formatted files.
37 *
38 */
39
40 public class ImportFileParser extends DefaultHandler implements ContentHandler, ErrorHandler
41 {
42 private int mPhase;
43 private File mImportFile;
44 private SAXParser mParser = null;
45
46 private ArrayList<ImportItem> mImportItemsArrayList = null;
47 private int mAction;
48 private String mItemDescriptor = null;
49 private String mItemId = null;
50 private String mRepositoryName = null;
51 private Stack<String> mTagNames = new Stack<String>();
52 private StringBuffer mPropertyValue = null;
53 private HashMap<String, String> mProperties = null;
54 private String mPropertyName = null;
55
56 /***
57 * Construct a new import file parser.
58 */
59
60 public ImportFileParser (int pPhase, File pImportFile)
61 {
62 mPhase = pPhase;
63 mImportFile = pImportFile;
64 }
65
66 public ImportItem[] parseFile ()
67 {
68
69
70 SAXParserFactory factory = null;
71 XMLReader reader = null;
72 ImportItem[] importItems = null;
73
74
75
76 try
77 {
78 factory = SAXParserFactory.newInstance();
79 mParser = factory.newSAXParser();
80 reader = mParser.getXMLReader();
81 reader.setEntityResolver(new XMLFileEntityResolver());
82 }
83 catch (ParserConfigurationException e)
84 {
85 System.out.println ("PARSER: ParserConfigurationException: " + e.getMessage());
86 return (null);
87 }
88 catch (SAXException e)
89 {
90 System.out.println ("PARSER: SAXException: " + e.getMessage());
91 return (null);
92 }
93
94
95
96 mImportItemsArrayList = new ArrayList<ImportItem>();
97
98 try
99 {
100 mParser.parse(mImportFile, this);
101 }
102 catch (SAXException e)
103 {
104 System.out.println ("PARSER: SAXException: " + e.getMessage());
105 return (null);
106 }
107 catch (IOException e)
108 {
109 System.out.println ("PARSER: IOException: " + e.getMessage());
110 return (null);
111 }
112
113
114
115 importItems = new ImportItem[mImportItemsArrayList.size()];
116 importItems = (ImportItem[]) mImportItemsArrayList.toArray(importItems);
117
118 return (importItems);
119 }
120
121
122
123
124
125 public void startElement
126 (
127 String namespaceURI,
128 String localName,
129 String qname,
130 Attributes pAttributes
131 )
132 {
133 mTagNames.push (qname);
134
135 if (TemplateParser.TAG_ADD_ITEM.equals (qname))
136 {
137 readTagAttributes(pAttributes);
138 mAction = ImportItem.M_ACTION_ADD;
139 mProperties = new HashMap<String, String>();
140 }
141 else if (TemplateParser.TAG_UPDATE_ITEM.equals (qname))
142 {
143 readTagAttributes(pAttributes);
144 mAction = ImportItem.M_ACTION_UPDATE;
145 mProperties = new HashMap<String, String>();
146 }
147 else if (TemplateParser.TAG_REMOVE_ITEM.equals(qname))
148 {
149 readTagAttributes(pAttributes);
150 mAction = ImportItem.M_ACTION_DELETE;
151 mProperties = new HashMap<String, String>();
152 }
153 else if (TemplateParser.TAG_SET_PROPERTY.equals(qname))
154 {
155 String propertyName = pAttributes.getValue (TemplateParser.ATTR_NAME);
156 mPropertyName = propertyName;
157 mPropertyValue = new StringBuffer ();
158 }
159 }
160
161 private void readTagAttributes(Attributes pAttributes)
162 {
163 mItemDescriptor = pAttributes.getValue (TemplateParser.ATTR_ITEM_DESCRIPTOR);
164 mItemId = pAttributes.getValue (TemplateParser.ATTR_ID);
165 mRepositoryName = pAttributes.getValue (TemplateParser.ATTR_REPOSITORY);
166 }
167
168
169 /***
170 * Reads characters between tags <foo>characters</foo> For
171 * startSQLRepository, this is typically a property value Note that values
172 * with a comma are actually multi values
173 */
174 public void characters(char[] chars, int start, int length)
175 {
176 if (TemplateParser.TAG_SET_PROPERTY.equals (mTagNames.peek()))
177 {
178 mPropertyValue.append (new String (chars, start, length));
179 }
180 }
181
182
183 /***
184 * Called when a tag ends This method cleans up state between tags
185 */
186
187 public void endElement (String namespaceURI, String localName, String pQName)
188 {
189 if (TemplateParser.TAG_ADD_ITEM.equals(pQName))
190 {
191 if (mPhase != ImportService.PHASE_DELETE)
192 {
193 addImportItem ();
194 }
195 }
196 else if (TemplateParser.TAG_UPDATE_ITEM.equals(pQName))
197 {
198 if (mPhase != ImportService.PHASE_DELETE)
199 {
200 addImportItem ();
201 }
202 }
203 else if (TemplateParser.TAG_REMOVE_ITEM.equals(pQName))
204 {
205 if (mPhase == ImportService.PHASE_DELETE)
206 {
207 addImportItem ();
208 }
209 }
210 else if (TemplateParser.TAG_SET_PROPERTY.equals(pQName))
211 {
212 mProperties.put (mPropertyName, mPropertyValue.toString());
213 mPropertyName = null;
214 }
215
216 String last = mTagNames.pop();
217 }
218
219
220 /***
221 * Called when we start parsing this file
222 */
223 public void startDocument()
224 {
225 }
226
227
228 /***
229 * Called when we are done parsing this file
230 */
231 public void endDocument()
232 {
233 }
234
235 private void addImportItem ()
236 {
237 ImportItem importItem = new ImportItem ();
238
239 importItem.setAction (mAction);
240 importItem.setItemDescriptor (mItemDescriptor);
241 importItem.setItemId (mItemId);
242 importItem.setRepositoryName (mRepositoryName);
243 importItem.setProperties (mProperties);
244
245 mImportItemsArrayList.add (importItem);
246
247
248 }
249
250 private void displayImportItem (ImportItem pImportItem)
251 {
252 String propertyName;
253 String propertyValue;
254
255 System.out.println ("PARSER: ITEM: Property: Item Id Value: " + pImportItem.getItemId());
256 System.out.println ("PARSER: ITEM: Property: Item Descriptor Value: " + pImportItem.getItemDescriptor());
257 System.out.println ("PARSER: ITEM: Property: Action Value: " + pImportItem.getAction());
258
259 HashMap properties = pImportItem.getProperties();
260 Iterator iterator = properties.keySet().iterator();
261
262 while (iterator.hasNext())
263 {
264 propertyName = (String) iterator.next();
265 propertyValue = (String) properties.get (propertyName);
266
267 System.out.println ("PARSER: ITEM: Property: " + propertyName + " Value: " + propertyValue);
268 }
269 }
270
271
272
273
274
275 public void error (SAXParseException e)
276 {
277 System.out.println("ERROR: " + e.getMessage());
278 }
279
280 public void fatalError (SAXParseException e)
281 {
282 System.out.println("FATAL ERROR: " + e.getMessage());
283 }
284
285 public void warning(SAXParseException e)
286 {
287 System.out.println("WARNING: " + e.getMessage());
288 }
289 }
290