File: NonValidator.java

package info (click to toggle)
libnanoxml2-java 2.2.3.dfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 992 kB
  • sloc: java: 5,085; xml: 150; makefile: 89; sh: 59
file content (632 lines) | stat: -rw-r--r-- 18,046 bytes parent folder | download | duplicates (2)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/* NonValidator.java                                               NanoXML/Java
 *
 * $Revision: 1.4 $
 * $Date: 2002/02/03 21:19:38 $
 * $Name: RELEASE_2_2_1 $
 *
 * This file is part of NanoXML 2 for Java.
 * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
 *
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from the
 * use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 *  1. The origin of this software must not be misrepresented; you must not
 *     claim that you wrote the original software. If you use this software in
 *     a product, an acknowledgment in the product documentation would be
 *     appreciated but is not required.
 *
 *  2. Altered source versions must be plainly marked as such, and must not be
 *     misrepresented as being the original software.
 *
 *  3. This notice may not be removed or altered from any source distribution.
 */

package net.n3.nanoxml;


import java.io.Reader;
import java.io.IOException;
import java.io.StringReader;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Stack;


/**
 * NonValidator is a concrete implementation of IXMLValidator which processes
 * the DTD and handles entity definitions. It does not do any validation
 * itself.
 *
 * @author Marc De Scheemaecker
 * @version $Name: RELEASE_2_2_1 $, $Revision: 1.4 $
 */
public class NonValidator
   implements IXMLValidator
{

   /**
    * The parameter entity resolver.
    */
   protected IXMLEntityResolver parameterEntityResolver;


   /**
    * Contains the default values for attributes for the different element
    * types.
    */
   protected Hashtable attributeDefaultValues;


   /**
    * The stack of elements to be processed.
    */
   protected Stack currentElements;


   /**
    * Creates the "validator".
    */
   public NonValidator()
   {
      this.attributeDefaultValues = new Hashtable();
      this.currentElements = new Stack();
      this.parameterEntityResolver = new XMLEntityResolver();
   }


   /**
    * Cleans up the object when it's destroyed.
    */
   protected void finalize()
      throws Throwable
   {
      this.parameterEntityResolver = null;
      this.attributeDefaultValues.clear();
      this.attributeDefaultValues = null;
      this.currentElements.clear();
      this.currentElements = null;
      super.finalize();
   }


   /**
    * Sets the parameter entity resolver.
    *
    * @param resolver the entity resolver.
    */
   public void setParameterEntityResolver(IXMLEntityResolver resolver)
   {
      this.parameterEntityResolver = resolver;
   }


   /**
    * Returns the parameter entity resolver.
    *
    * @return the entity resolver.
    */
   public IXMLEntityResolver getParameterEntityResolver()
   {
      return this.parameterEntityResolver;
   }


   /**
    * Parses the DTD. The validator object is responsible for reading the
    * full DTD.
    *
    * @param publicID       the public ID, which may be null.
    * @param reader         the reader to read the DTD from.
    * @param entityResolver the entity resolver.
    * @param external       true if the DTD is external.
    *
    * @throws java.lang.Exception
    *     If something went wrong.
    */
   public void parseDTD(String             publicID,
                        IXMLReader         reader,
                        IXMLEntityResolver entityResolver,
                        boolean            external)
      throws Exception
   {
      XMLUtil.skipWhitespace(reader, null);
      int origLevel = reader.getStreamLevel();

      for (;;) {
         String str = XMLUtil.read(reader, '%');
         char ch = str.charAt(0);

         if (ch == '%') {
            XMLUtil.processEntity(str, reader,
                                  this.parameterEntityResolver);
            continue;
         } else if (ch == '<') {
            this.processElement(reader, entityResolver);
         } else if (ch == ']') {
            return; // end internal DTD
         } else {
            XMLUtil.errorInvalidInput(reader.getSystemID(),
                                      reader.getLineNr(),
                                      str);
         }

         do {
            ch = reader.read();

            if (external && (reader.getStreamLevel() < origLevel)) {
               reader.unread(ch);
               return; // end external DTD
            }
         } while ((ch == ' ') || (ch == '\t') || (ch == '\n')
                  || (ch == '\r'));

         reader.unread(ch);
      }
   }


   /**
    * Processes an element in the DTD.
    *
    * @param reader         the reader to read data from.
    * @param entityResolver the entity resolver.
    *
    * @throws java.lang.Exception
    *     If something went wrong.
    */
   protected void processElement(IXMLReader         reader,
                                 IXMLEntityResolver entityResolver)
      throws Exception
   {
      String str = XMLUtil.read(reader, '%');
      char ch = str.charAt(0);

      if (ch != '!') {
         XMLUtil.skipTag(reader);
         return;
      }

      str = XMLUtil.read(reader, '%');
      ch = str.charAt(0);

      switch (ch) {
         case '-':
            XMLUtil.skipComment(reader);
            break;

         case '[':
            this.processConditionalSection(reader, entityResolver);
            break;

         case 'E':
            this.processEntity(reader, entityResolver);
            break;

         case 'A':
            this.processAttList(reader, entityResolver);
            break;

         default:
            XMLUtil.skipTag(reader);
      }
   }


   /**
    * Processes a conditional section.
    *
    * @param reader         the reader to read data from.
    * @param entityResolver the entity resolver.
    *
    * @throws java.lang.Exception
    *     If something went wrong.
    */
   protected void processConditionalSection(IXMLReader         reader,
                                            IXMLEntityResolver entityResolver)
      throws Exception
   {
      XMLUtil.skipWhitespace(reader, null);

      String str = XMLUtil.read(reader, '%');
      char ch = str.charAt(0);

      if (ch != 'I') {
         XMLUtil.skipTag(reader);
         return;
      }

      str = XMLUtil.read(reader, '%');
      ch = str.charAt(0);

      switch (ch) {
         case 'G':
            this.processIgnoreSection(reader, entityResolver);
            return;

         case 'N':
            break;

         default:
            XMLUtil.skipTag(reader);
            return;
      }

      if (! XMLUtil.checkLiteral(reader, "CLUDE")) {
         XMLUtil.skipTag(reader);
         return;
      }

      XMLUtil.skipWhitespace(reader, null);

      str = XMLUtil.read(reader, '%');
      ch = str.charAt(0);

      if (ch != '[') {
         XMLUtil.skipTag(reader);
         return;
      }

      Reader subreader = new CDATAReader(reader);
      StringBuffer buf = new StringBuffer(1024);

      for (;;) {
         int ch2 = subreader.read();

         if (ch2 < 0) {
            break;
         }

         buf.append((char) ch2);
      }

      subreader.close();
      reader.startNewStream(new StringReader(buf.toString()));
   }


   /**
    * Processes an ignore section.
    *
    * @param reader         the reader to read data from.
    * @param entityResolver the entity resolver.
    *
    * @throws java.lang.Exception
    *     If something went wrong.
    */
   protected void processIgnoreSection(IXMLReader         reader,
                                       IXMLEntityResolver entityResolver)
      throws Exception
   {
      if (! XMLUtil.checkLiteral(reader, "NORE")) {
         XMLUtil.skipTag(reader);
         return;
      }

      XMLUtil.skipWhitespace(reader, null);

      String str = XMLUtil.read(reader, '%');
      char ch = str.charAt(0);

      if (ch != '[') {
         XMLUtil.skipTag(reader);
         return;
      }

      Reader subreader = new CDATAReader(reader);
      subreader.close();
   }


   /**
    * Processes an ATTLIST element.
    *
    * @param reader         the reader to read data from.
    * @param entityResolver the entity resolver.
    *
    * @throws java.lang.Exception
    *     If something went wrong.
    */
   protected void processAttList(IXMLReader         reader,
                                 IXMLEntityResolver entityResolver)
      throws Exception
   {
      if (! XMLUtil.checkLiteral(reader,  "TTLIST")) {
         XMLUtil.skipTag(reader);
         return;
      }

      XMLUtil.skipWhitespace(reader, null);
      String str = XMLUtil.read(reader, '%');
      char ch = str.charAt(0);
      while (ch == '%') {
         XMLUtil.processEntity(str, reader,
                               this.parameterEntityResolver);
         str = XMLUtil.read(reader, '%');
         ch = str.charAt(0);
      }
      reader.unread(ch);
      String elementName = XMLUtil.scanIdentifier(reader);
      XMLUtil.skipWhitespace(reader, null);
      
      str = XMLUtil.read(reader, '%');
      ch = str.charAt(0);
      while (ch == '%') {
         XMLUtil.processEntity(str, reader,
                               this.parameterEntityResolver);
         str = XMLUtil.read(reader, '%');
         ch = str.charAt(0);
      }

      Properties props = new Properties();

      while (ch != '>') {
         reader.unread(ch);
         String attName = XMLUtil.scanIdentifier(reader);
         XMLUtil.skipWhitespace(reader, null);
         str = XMLUtil.read(reader, '%');
         ch = str.charAt(0);
         while (ch == '%') {
             XMLUtil.processEntity(str, reader,
                                   this.parameterEntityResolver);
             str = XMLUtil.read(reader, '%');
             ch = str.charAt(0);
         }

         if (ch == '(') {
             while (ch != ')') {
                 str = XMLUtil.read(reader, '%');
                 ch = str.charAt(0);
                 while (ch == '%') {
                     XMLUtil.processEntity(str, reader,
                                           this.parameterEntityResolver);
                     str = XMLUtil.read(reader, '%');
                     ch = str.charAt(0);
                 }
             }
         } else {
            reader.unread(ch);
            XMLUtil.scanIdentifier(reader);
         }

         XMLUtil.skipWhitespace(reader, null);
         str = XMLUtil.read(reader, '%');
         ch = str.charAt(0);
         while (ch == '%') {
             XMLUtil.processEntity(str, reader,
                                   this.parameterEntityResolver);
             str = XMLUtil.read(reader, '%');
             ch = str.charAt(0);
         }
         
         if (ch == '#') {
            str = XMLUtil.scanIdentifier(reader);
            XMLUtil.skipWhitespace(reader, null);

            if (! str.equals("FIXED")) {
               XMLUtil.skipWhitespace(reader, null);

               str = XMLUtil.read(reader, '%');
               ch = str.charAt(0);
               while (ch == '%') {
                  XMLUtil.processEntity(str, reader,
                                        this.parameterEntityResolver);
                  str = XMLUtil.read(reader, '%');
                  ch = str.charAt(0);
               }

               continue;
            }
         } else {
            reader.unread(ch);
         }

         String value = XMLUtil.scanString(reader, '%',
                                           this.parameterEntityResolver);
         props.put(attName, value);
         XMLUtil.skipWhitespace(reader, null);

         str = XMLUtil.read(reader, '%');
         ch = str.charAt(0);
         while (ch == '%') {
            XMLUtil.processEntity(str, reader,
                                  this.parameterEntityResolver);
            str = XMLUtil.read(reader, '%');
            ch = str.charAt(0);
         }
      }

      if (! props.isEmpty()) {
         this.attributeDefaultValues.put(elementName, props);
      }
   }


   /**
    * Processes an ENTITY element.
    *
    * @param reader         the reader to read data from.
    * @param entityResolver the entity resolver.
    *
    * @throws java.lang.Exception
    *     If something went wrong.
    */
   protected void processEntity(IXMLReader         reader,
                                IXMLEntityResolver entityResolver)
      throws Exception
   {
      if (! XMLUtil.checkLiteral(reader, "NTITY")) {
         XMLUtil.skipTag(reader);
         return;
      }

      XMLUtil.skipWhitespace(reader, null);
      char ch = XMLUtil.readChar(reader, '\0');

      if (ch == '%') {
         XMLUtil.skipWhitespace(reader, null);
         entityResolver = this.parameterEntityResolver;
      } else {
         reader.unread(ch);
      }

      String key = XMLUtil.scanIdentifier(reader);
      XMLUtil.skipWhitespace(reader, null);
      ch = XMLUtil.readChar(reader, '%');
      String systemID = null;
      String publicID = null;

      switch (ch) {
         case 'P':
            if (! XMLUtil.checkLiteral(reader, "UBLIC")) {
               XMLUtil.skipTag(reader);
               return;
            }

            XMLUtil.skipWhitespace(reader, null);
            publicID = XMLUtil.scanString(reader, '%',
                                          this.parameterEntityResolver);
            XMLUtil.skipWhitespace(reader, null);
            systemID = XMLUtil.scanString(reader, '%',
                                          this.parameterEntityResolver);
            XMLUtil.skipWhitespace(reader, null);
            XMLUtil.readChar(reader, '%');
            break;

         case 'S':
            if (! XMLUtil.checkLiteral(reader, "YSTEM")) {
               XMLUtil.skipTag(reader);
               return;
            }

            XMLUtil.skipWhitespace(reader, null);
            systemID = XMLUtil.scanString(reader, '%',
                                          this.parameterEntityResolver);
            XMLUtil.skipWhitespace(reader, null);
            XMLUtil.readChar(reader, '%');
            break;

         case '"':
         case '\'':
            reader.unread(ch);
            String value = XMLUtil.scanString(reader, '%',
                                              this.parameterEntityResolver);
            entityResolver.addInternalEntity(key, value);
            XMLUtil.skipWhitespace(reader, null);
            XMLUtil.readChar(reader, '%');
            break;

         default:
            XMLUtil.skipTag(reader);
      }

      if (systemID != null) {
         entityResolver.addExternalEntity(key, publicID, systemID);
      }
   }


   /**
    * Indicates that an element has been started.
    *
    * @param name       the name of the element.
    * @param systemId   the system ID of the XML data of the element.
    * @param lineNr     the line number in the XML data of the element.
    */
   public void elementStarted(String name,
                              String systemId,
                              int    lineNr)
   {
      Properties attribs
         = (Properties) this.attributeDefaultValues.get(name);

      if (attribs == null) {
         attribs = new Properties();
      } else {
         attribs = (Properties) attribs.clone();
      }

      this.currentElements.push(attribs);
   }


   /**
    * Indicates that the current element has ended.
    *
    * @param name       the name of the element.
    * @param systemId   the system ID of the XML data of the element.
    * @param lineNr     the line number in the XML data of the element.
    */
   public void elementEnded(String name,
                            String systemId,
                            int    lineNr)
   {
      // nothing to do
   }


   /**
    * This method is called when the attributes of an XML element have been
    * processed.
    * If there are attributes with a default value which have not been
    * specified yet, they have to be put into <I>extraAttributes</I>.
    *
    * @param name            the name of the element.
    * @param extraAttributes where to put extra attributes.
    * @param systemId        the system ID of the XML data of the element.
    * @param lineNr          the line number in the XML data of the element.
    */
   public void elementAttributesProcessed(String     name,
                                          Properties extraAttributes,
                                          String     systemId,
                                          int        lineNr)
   {
      Properties props = (Properties) this.currentElements.pop();
      Enumeration enu = props.keys();

      while (enu.hasMoreElements()) {
         String key = (String) enu.nextElement();
         extraAttributes.put(key, props.get(key));
      }
   }


   /**
    * Indicates that an attribute has been added to the current element.
    *
    * @param key        the name of the attribute.
    * @param value      the value of the attribute.
    * @param systemId   the system ID of the XML data of the element.
    * @param lineNr     the line number in the XML data of the element.
    */
   public void attributeAdded(String key,
                              String value,
                              String systemId,
                              int    lineNr)
   {
      Properties props = (Properties) this.currentElements.peek();

      if (props.containsKey(key)) {
         props.remove(key);
      }
   }


   /**
    * Indicates that a new #PCDATA element has been encountered.
    *
    * @param systemId the system ID of the XML data of the element.
    * @param lineNr   the line number in the XML data of the element.
    */
   public void PCDataAdded(String systemId,
                           int    lineNr)
   {
      // nothing to do
   }

}