source: fedd/abac-src/rtml/src/edu/stanford/rt/parser/RTParser.java @ 8780cbec

axis_examplecompt_changesinfo-opsversion-1.30version-2.00version-3.01version-3.02
Last change on this file since 8780cbec was 8780cbec, checked in by Jay Jacobs <Jay.Jacobs@…>, 15 years ago

ABAC sources from Cobham

  • Property mode set to 100644
File size: 73.1 KB
Line 
1package edu.stanford.rt.parser;
2
3//imports Apache Xerces API
4import org.apache.xerces.parsers.DOMParser;
5import org.xml.sax.InputSource;
6import org.w3c.dom.*;
7
8// imports XML Security PublicKey api
9import org.apache.xml.security.keys.content.keyvalues.DSAKeyValue;
10import org.apache.xml.security.keys.content.keyvalues.RSAKeyValue;
11import org.apache.xml.security.exceptions.XMLSecurityException;
12
13import java.io.*;
14import java.util.*;
15
16import edu.stanford.rt.datatype.*;
17import edu.stanford.rt.credential.*;
18import edu.stanford.rt.util.*;
19
20/**
21 * @author Ninghui Li, Sandra Qiu <br>
22 *
23 * RTParser parses a XML-format CredentialStore document and transforms
24 * the various elements into their corresponding Java objects.
25 */
26public class RTParser implements TagNameConstants, Constants
27{
28        /** The DOM parser this transformer uses*/
29        private DOMParser parser;
30
31        /**
32         * Constructor for RTParser.
33         */
34        public RTParser() throws Exception
35        {
36                parser = new DOMParser();
37                parser.setFeature(
38                        "http://apache.org/xml/features/validation/schema",
39                        true);
40                parser.setErrorHandler(new ParsingErrorHandler());
41        }
42
43        /**
44         * Method createSystemDomain.
45         *      The method for creating system domain.
46         *      *** Only useful for our parser.
47         * @return ApplicationDomain
48         * @throws Exception
49         */
50        public synchronized ApplicationDomain createSystemDomain()
51                throws Exception
52        {
53        String spec = 
54            System.getProperty("edu.stanford.rt.SystemDomain",
55                               "C:\\rtml\\schemas\\systemSpec.xml");
56                return parseSystemDomain(new FileInputStream(new File(spec)));
57        }
58
59        /**
60         * Method parseSystemDomain. <br>
61         *      parses system domain from a XML input source, typically a
62         *      XML file. The system domain element in the source should be
63         *      the root element.
64         * @param in
65         * @return ApplicationDomain
66         * @throws Exception
67         */
68        public synchronized ApplicationDomain parseSystemDomain(InputStream in)
69                throws Exception
70        {
71                parser.parse(new InputSource(in));
72                Document document = parser.getDocument();
73                Element root = document.getDocumentElement();
74                return transformSystemDomain(root);
75        }
76
77        /**
78         * Method parseSystemDomain. <br>
79         *      parses system domain from a XML input source, typically a
80         *      XML file. The system domain element in the source should be
81         *      the root element.
82         * @param in
83         * @return CredentialDomain
84         * @throws Exception
85         */
86        public synchronized CredentialDomain
87            parseCredentialDomain(InputStream in, RTContext context) 
88            throws Exception
89        {
90            parser.parse(new InputSource(in));
91            Document document = parser.getDocument();
92                Element root = document.getDocumentElement();
93                return transformCredential(root, context);
94        }
95
96
97        /**
98         * Method transformSystemDomain.<br>
99         *    transforms the XML element to the built-in system
100         *    <code>ApplicationDomain</code>.
101         * @param root
102         *      XML element of system domain.
103         * @return ApplicationDomain
104         * @throws DomainSpecException
105         */
106        public synchronized ApplicationDomain transformSystemDomain(Element root)
107                throws DomainSpecException
108        {
109                if (!root.getTagName().equals(APP_DOMAIN_SPEC))
110                        throw new DomainSpecException("Not an ApplicationDomainSpecification element.");
111                String uri = root.getAttribute(URI);
112                ApplicationDomain spec =
113                        new ApplicationDomain(uri, true, null);
114                String hashID = root.getAttribute(ID);
115                if (hashID.length() == 0)
116                {
117                        // compute the hashID ourselves.
118                        // hashID = getHash(spec);
119                }
120                HashID id = new HashID(HashID.APPLICATION_DOMAIN, hashID);
121                spec.setHashID(id);
122                // Add XML schema date types
123                spec.addType(new TimeType("dateTime"));
124                spec.addType(new TimeType("date"));
125                spec.addType(new TimeType("time"));
126                spec.addType(new TimeType("gYear"));
127                spec.addType(new TimeType("gYearMonth"));
128                spec.addType(new TimeType("gMonth"));
129                spec.addType(new TimeType("gMonthDay"));
130                spec.addType(new TimeType("gDay"));
131                Element[] elements = RTParser.getChildElements(root);
132                for (int i = 0; i < elements.length; i++)
133                {
134                        Element childEle = elements[i];
135                        String childName = childEle.getTagName();
136                        if (childName.equals(INTEGER_TYPE))
137                        {
138                                IntegerType intType =
139                                        transformIntegerTypeDeclaration(childEle);
140                                spec.addType(intType);
141                        }
142                        else if (childName.equals(DECIMAL_TYPE))
143                        {
144                                DecimalType decimalType =
145                                        transformDecimalTypeDeclaration(childEle);
146                                spec.addType(decimalType);
147                        }
148                        else if (childName.equals(STRING_TYPE))
149                        {
150                                StringType stringType =
151                                        transformStringTypeDeclaration(childEle);
152                                spec.addType(stringType);
153                        }
154                        else if (childName.equals(ENUM_TYPE))
155                        {
156                                EnumType enumType =
157                                        transformEnumTypeDeclaration(childEle, spec);
158                                spec.addType(enumType);
159                        }
160                        else if (childName.equals(RECORD_TYPE))
161                        {
162                                RecordType recordType =
163                                        transformRecordTypeDeclaration(childEle, spec);
164                                spec.addType(recordType);
165                        }
166                        else if (childName.equals(TREE_TYPE))
167                        {
168                                TreeType treeType =
169                                        transformTreeTypeDeclaration(childEle);
170                                spec.addType(treeType);
171                        }
172                } // end of for()...
173                spec.setComplete();
174                return spec;
175        }
176
177        /**
178         * Method getFirstChildElement.<br>
179                Returns the first child element of ELEMENT_NODE type for the
180                given parent element.
181         * @param parent
182         * @return Element
183         */
184        static public Element getFirstChildElement(Element parent)
185        {
186                Element ele = null;
187                NodeList nodes = parent.getChildNodes();
188                int n = nodes.getLength();
189                for (int i = 0; i < n; i++)
190                {
191                        Node node = nodes.item(i);
192                        short type = node.getNodeType();
193                        if (type != org.w3c.dom.Node.ELEMENT_NODE)
194                                continue;
195                        ele = (Element) node;
196                        break;
197                }
198                return ele;
199        }
200
201        /**
202         * Method getFirstChildElementByTagName.<br>
203         *   returns the first child element with the given tag name.
204         * @param parent
205         * @param tagName
206         * @return Element
207         */
208        /**
209        */
210        static public Element getFirstChildElementByTagName(
211                Element parent,
212                String tagName)
213        {
214                Element ele = null;
215                NodeList nodes = parent.getElementsByTagName(tagName);
216                ele = (Element) nodes.item(0);
217                return ele;
218        }
219
220        /**
221         * Method getChildElements.<br>
222         *      returns an array of child elements of ELEMNENT_NODE type.
223         * @param parent
224         * @return Element[]
225         */
226        static public Element[] getChildElements(Element parent)
227        {
228                ArrayList elements = new ArrayList();
229                NodeList nodes = parent.getChildNodes();
230                int n = nodes.getLength();
231                for (int i = 0; i < n; i++)
232                {
233                        Node node = nodes.item(i);
234                        short type = node.getNodeType();
235                        if (type != org.w3c.dom.Node.ELEMENT_NODE)
236                                continue;
237                        elements.add((Element) node);
238                }
239                // put into an array of Elements.
240                int size = elements.size();
241                Element[] eleArray = new Element[size];
242                for (int i = 0; i < size; i++)
243                {
244                        eleArray[i] = (Element) elements.get(i);
245                }
246                return eleArray;
247        }
248
249        /**
250         * Method parseCredentialStore.<br>
251         * parses credential store from an XML input source, typically an
252         * XML file. The <code>CredentialStore</code> element should be a
253         * root element.
254         * @param in
255         * @param rtContext
256         *      the context in which the parsing occurs.
257         * @param credentialStore
258         *      the credential store being constructed.
259         * @throws DomainSpecException
260         */
261        public synchronized void parseCredentialStore(
262                InputStream in,
263                RTContext rtContext,
264                CredentialStore credentialStore)
265                throws Exception
266        {
267                parser.parse(new InputSource(in));
268                Document document = parser.getDocument();
269                Element root = document.getDocumentElement();
270                transformCredentialStore(root, rtContext, credentialStore);
271        }
272
273        /**
274         * Method transformCredentialStore.<br>
275         * @param root
276         *  the XML element of credential store
277         * @param rtContext
278         * @param credentialStore
279         * @throws DomainSpecException
280         * @throws CredException
281         */
282        public synchronized void transformCredentialStore(
283                Element root,
284                RTContext rtContext,
285                CredentialStore credentialStore)
286                throws DomainSpecException, CredException
287        {
288                String rootTag = root.getTagName();
289                if (!rootTag.equals(CREDENTIAL_STORE))
290                {
291                        throw new DomainSpecException("Not a CredentialStore document.");
292                }
293                Element[] elements = getChildElements(root);
294                for (int i = 0; i < elements.length; i++)
295                {
296                        String tag = elements[i].getTagName();
297                        if (tag.equals(APP_DOMAIN_SPEC))
298                        {
299                                ApplicationDomain spec =
300                                        transformDomain(elements[i], rtContext);
301                                HashID id = spec.getHashID();
302                                rtContext.addApplicationDomain(id, spec);
303                        }
304                        else if (tag.equals(PRINCIPAL_INFO))
305                        {
306                                // TODO
307                                // rtContext.addPrincipal(PublickeyPrincipal, PublicKeyPrincipalInfo);
308                        }
309                        else if (tag.equals(CREDENTIAL))
310                        {
311                                CredentialDomain credentialDomain =
312                                        transformCredential(elements[i], rtContext);
313                                HashID id = credentialDomain.getHashID();
314                                credentialStore.addCredentialDomain(
315                                        id,
316                                        credentialDomain);
317                        }
318//                      else if (tag.equals(ACCESS_RULE))
319//                      {
320//                              CredentialDomain accessRuleDomain =
321//                                      transformAccessRule(elements[i], rtContext);
322//                              HashID id = accessRuleDomain.getHashID();
323//                              credentialStore.addCredentialDomain(
324//                                      id,
325//                                      accessRuleDomain);
326//                      }
327                }
328        }
329
330        /**
331         * Method transformDomain.<br>
332         *      parses non-system <code>ApplicationDomain</code>.
333         * @param ele
334         *  the <code>ApplicationDomain</code> element
335         * @param rtContext
336         *  the context in which the transformation occurs.
337         * @return ApplicationDomain
338         * @throws DomainSpecException
339         */
340        public synchronized ApplicationDomain transformDomain(
341                Element ele,
342                RTContext rtContext)
343                throws DomainSpecException
344        {
345                if (!ele.getTagName().equals(APP_DOMAIN_SPEC))
346                        throw new DomainSpecException("Not an ApplicationDomainSpecification element");
347                String uri = ele.getAttribute(URI);
348                //              if (uri.length() == 0)
349                //                      throw new DomainSpecException(
350                //                              missing_attr_value + "'uri'");
351                ApplicationDomain spec =
352                        new ApplicationDomain(uri, false, rtContext);
353                String hashID = ele.getAttribute(ID);
354                if (hashID.length() == 0)
355                {
356                        // compute the hashID ourselves.
357                        // hashID = getHash(spec);
358                }
359                HashID id = new HashID(HashID.APPLICATION_DOMAIN, hashID);
360                spec.setHashID(id);
361                Element[] elements = getChildElements(ele);
362                for (int i = 0; i < elements.length; i++)
363                {
364                        Element childEle = elements[i];
365                        String childName = childEle.getTagName();
366                        if (childName.equals(IMPORT_DOMAIN))
367                        {
368                                String name = childEle.getAttribute(NAME);
369                                if (name.length() == 0)
370                                        throw new DomainSpecException(
371                                                missing_attr_value + "'name'");
372                                String importedUri = childEle.getAttribute(URI);
373                                String idref = childEle.getAttribute(IDREF);
374                                if (idref.length() == 0)
375                                        throw new DomainSpecException(
376                                                missing_attr_value + "'idref'");
377                                ApplicationDomain importedDomain =
378                                        locateDomain(idref, importedUri, rtContext);
379                                spec.importDomain(name, importedDomain);
380                        }
381                        else if (childName.equals(INTEGER_TYPE))
382                        {
383                                IntegerType intType =
384                                        transformIntegerTypeDeclaration(childEle);
385                                spec.addType(intType);
386                        }
387                        else if (childName.equals(DECIMAL_TYPE))
388                        {
389                                DecimalType decimalType =
390                                        transformDecimalTypeDeclaration(childEle);
391                                spec.addType(decimalType);
392                        }
393                        else if (childName.equals(ENUM_TYPE))
394                        {
395                                EnumType enumType =
396                                        transformEnumTypeDeclaration(childEle, spec);
397                                spec.addType(enumType);
398                        }
399                        else if (childName.equals(STRING_TYPE))
400                        {
401                                StringType stringType =
402                                        transformStringTypeDeclaration(childEle);
403                                spec.addType(stringType);
404                        }
405                        else if (childName.equals(RECORD_TYPE))
406                        {
407                                RecordType recordType =
408                                        transformRecordTypeDeclaration(childEle, spec);
409                                spec.addType(recordType);
410                        }
411                        else if (childName.equals(TREE_TYPE))
412                        {
413                                TreeType treeType =
414                                        transformTreeTypeDeclaration(childEle);
415                                spec.addType(treeType);
416                        }
417                        else if (childName.equals(PRINCIPAL_TYPE))
418                        {
419                                // This element specifies the principal type for the entire system
420                                // Later on, the principal value can be checked against the type
421                                // defined here.
422                                SimpleType prinType =
423                                        transformPrincipalTypeDeclaration(childEle, spec);
424                                spec.setPrincipalType(prinType);
425                        }
426                        else if (childName.equals(ROLE_DECLARATION))
427                        {
428                                RoleDeclaration role =
429                                        transformRoleDeclaration(childEle, spec);
430                                spec.addRole(role);
431                        }
432                } //end of for( )....
433                spec.setComplete();
434                return spec;
435        } //==== End of parseDomainSpec()
436        /**
437           Parses non-system domain specification object from a file pointed by the
438           given uri.
439        */
440        //      private void parseDomainSpec(String uri)
441        //      {
442        //              DomainSpecification spec = null;
443        //              try
444        //              {
445        //                      DOMParser parser = new DOMParser();
446        //                      parser.setFeature(
447        //                              "http://apache.org/xml/features/validation/schema",
448        //                              true);
449        //                      parser.parse(new InputSource(new FileInputStream(new File(uri))));
450        //                      Document doc = parser.getDocument();
451        //                      transform(doc);
452        //              }
453        //              catch (SAXException e)
454        //              {
455        //                      System.out.println("error in setting up parser feature");
456        //              }
457        //              catch (Exception ex)
458        //              {
459        //                      ex.printStackTrace();
460        //              }
461        //      }
462        //
463        //      private void transform(Document document) throws Exception
464        //      {
465        //              Element root = document.getDocumentElement();
466        //              String rootTag = root.getTagName();
467        //
468        //      }
469        /**
470         * Method locateDomain.<br>
471         * @param rtContext
472         * @param idref
473         *      the hash value of the target
474         *      <code>ApplicationDomainSpecification</code> object.
475         * @param uri
476         * @return ApplicationDomain
477         * @throws DomainSpecException
478         *  if cannot find the domain by the given idref.
479         */
480        private ApplicationDomain locateDomain(
481                String idref,
482                String uri,
483                RTContext rtContext)
484                throws DomainSpecException
485        {
486                ApplicationDomain spec = null;
487                if (!rtContext.hasDomainWithID(idref))
488                {
489                        //TODO: load domain from given uri, which is optional.
490                        //parseDomainSpec(uri);
491                        //spec = (DomainSpecification)applicationDomains.get(uri);
492                        throw new DomainSpecException(
493                                "Cannot find domain with id '" + idref + "'.");
494                }
495                else
496                {
497                        spec = rtContext.getApplicationDomain(idref);
498                }
499                if (!spec.isComplete())
500                {
501                        throw new DomainSpecException("Circular Dependency");
502                }
503                return spec;
504        }
505        /*###########################################################
506            #####
507            ##### Parsing DateType Declaration in DomainSpecification
508            #####
509        #############################################################*/
510        /**
511         * Method transformIntegerTypeDeclaration.<br>
512         *
513         * Parses IntegerType declaration in ADSD.
514         * Returns an IntegerType object by parsing element:<br>
515            <pre>
516                &lt;IntegerType name="" min="" max=""/&gt;
517            </pre>
518         * @param ele
519         * @return IntegerType
520         * @throws DomainSpecException
521         */
522        private IntegerType transformIntegerTypeDeclaration(Element ele)
523                throws DomainSpecException
524        {
525                if (!ele.getTagName().equals(INTEGER_TYPE))
526                        throw new DomainSpecException("Not an IntegerType element");
527                String name = ele.getAttribute(NAME);
528                String minStr = ele.getAttribute(MIN);
529                String maxStr = ele.getAttribute(MAX);
530                if (name.length() == 0)
531                        throw new DomainSpecException(
532                                missing_attr_value + "'name'");
533                if (minStr.length() == 0)
534                        throw new DomainSpecException(
535                                missing_attr_value + "'min'");
536                if (maxStr.length() == 0)
537                        throw new DomainSpecException(
538                                missing_attr_value + "'max'");
539                long min = Long.parseLong(minStr);
540                long max = Long.parseLong(maxStr);
541                //        RTUtil.debugInfo("name = " + name);
542                //        RTUtil.debugInfo("min = " + ele.getAttribute(MIN));
543                //        RTUtil.debugInfo("max = " + ele.getAttribute(MAX));
544                //        RTUtil.debugInfo("base = " + ele.getAttribute(BASE));
545                //        RTUtil.debugInfo("step = " + ele.getAttribute(STEP));
546                //        RTUtil.debugInfo("includeMin = " + ele.getAttribute(INCLUDE_MIN));
547                //        RTUtil.debugInfo("includeMax = " + ele.getAttribute(INCLUDE_MAX));
548                boolean includeMin =
549                        RTUtil.parseBoolean(ele.getAttribute(INCLUDE_MIN), true);
550                boolean includeMax =
551                        RTUtil.parseBoolean(ele.getAttribute(INCLUDE_MAX), true);
552                long base = RTUtil.parseLong(ele.getAttribute(BASE), 0);
553                long step = RTUtil.parseLong(ele.getAttribute(STEP), 1);
554                return new IntegerType(
555                        name,
556                        min,
557                        max,
558                        includeMin,
559                        includeMax,
560                        base,
561                        step);
562        }
563        /**
564         * Method transformDecimalTypeDeclaration.<br>
565         * Parses DecimalType declaration in ADSD.
566            Returns a DecimalType object by parsing element:
567            <pre>
568                &lt;DecimalType name="" min="" max=""/&gt;
569            </pre>
570         * @param ele
571         * @return DecimalType
572         * @throws DomainSpecException
573         */
574        private DecimalType transformDecimalTypeDeclaration(Element ele)
575                throws DomainSpecException
576        {
577                if (!ele.getTagName().equals(DECIMAL_TYPE))
578                        throw new DomainSpecException("Not a DecimalType element");
579                // TODO
580                throw new UnsupportedOperationException(
581                        no_support_for + "'DecimalType'");
582        }
583        /**
584         * Method transformStringTypeDeclaration.<br>
585     * Parses StringType declaration in ADSD.
586        Returns a StringType object by parsing element:<br>
587        <pre>
588            &lt;StringType name="" ignoreCase="false" ordered="false"/&gt;
589        </pre>
590         * @param ele
591         * @return StringType
592         * @throws DomainSpecException
593         */
594        private StringType transformStringTypeDeclaration(Element ele)
595                throws DomainSpecException
596        {
597                if (!ele.getTagName().equals(STRING_TYPE))
598                        throw new DomainSpecException("Not a StringType element");
599                String name = ele.getAttribute(NAME);
600                if (name.length() == 0)
601                        throw new DomainSpecException(
602                                missing_attr_value + "'name'");
603                boolean ignoreCase =
604                        RTUtil.parseBoolean(ele.getAttribute(IGNORE_CASE), false);
605                boolean ordered =
606                        RTUtil.parseBoolean(ele.getAttribute(ORDERED), false);
607                return new StringType(name, ignoreCase, ordered);
608        }
609        /**
610         * Method transformEnumTypeDeclaration.<br>
611     * Parses EnumType declaration in ADSD.
612        Returns an EnumType object by parsing element:<br>
613        <pre>
614            &lt;EnumType name="" ignoreCase="false" ordered="false"/&gt;
615                &lt;EnumValue&gt;Monday&lt;/EnumValue&gt;
616                &lt;EnumValue&gt;Wednesday&lt;/EnumValue&gt;
617                &lt;EnumValue&gt;Friday&lt;/EnumValue&gt;
618                ...
619            &lt;/EnumType&gt;
620        </pre>
621         * @param ele
622         * @param currentSpec
623         * @return EnumType
624         * @throws DomainSpecException
625         */
626        private EnumType transformEnumTypeDeclaration(
627                Element ele,
628                DomainSpecification currentSpec)
629                throws DomainSpecException
630        {
631                if (!ele.getTagName().equals(ENUM_TYPE))
632                        throw new DomainSpecException("Not an EnumType element");
633                OrderedMap enumValues =
634                        new OrderedMap(String.class, Object.class);
635                DataType valueType = null;
636                String name = ele.getAttribute(NAME);
637                if (name.length() == 0)
638                        throw new DomainSpecException(
639                                missing_attr_value + "'name'");
640                boolean ignoreCase =
641                        RTUtil.parseBoolean(ele.getAttribute(IGNORE_CASE), false);
642                boolean ordered =
643                        RTUtil.parseBoolean(ele.getAttribute(ORDERED), false);
644                int size = RTUtil.parseInt(ele.getAttribute(SIZE), 0);
645                Element[] elements = getChildElements(ele);
646                for (int i = 0; i < elements.length; i++)
647                {
648                        Element child = elements[i];
649                        String childName = child.getTagName();
650                        if (childName.equals(TYPE)) // nameRef type
651                        {
652                                // QQ: This element has been removed , as of 8/28/02
653                                String domain = child.getAttribute(DOMAIN);
654                                String n = child.getAttribute(NAME);
655                                valueType = currentSpec.lookupType(domain, n);
656                        }
657                        else if (childName.equals(ENUM_VALUE))
658                        {
659                                String value =
660                                        transformEnumValue(null, child).getValue();
661                                //RTUtil.debugInfo("value = " + value);
662                                enumValues.put(value, null);
663                        }
664                }
665                return new EnumType(
666                        name,
667                        valueType,
668                        enumValues,
669                        ignoreCase,
670                        ordered,
671                        size);
672        }
673        /**
674         * Method transformTreeTypeDeclaration.<br>
675     * Parses TreeType declaration in ADSD.
676        Returns a TreeType object by parsing element:<br>
677        <pre>
678            &lt;TreeType name="DNSType" sparator="." order="rootLast"/&gt;
679        </pre>
680         * @param ele
681         * @return TreeType
682         * @throws DomainSpecException
683         */
684        private TreeType transformTreeTypeDeclaration(Element ele)
685                throws DomainSpecException
686        {
687                if (!ele.getTagName().equals(TREE_TYPE))
688                        throw new DomainSpecException("Not a TreeType element");
689                String name = ele.getAttribute(NAME);
690                String separator = ele.getAttribute(SEPARATOR);
691                String order = ele.getAttribute(ORDER);
692                if (name.length() == 0)
693                        throw new DomainSpecException(
694                                missing_attr_value + "'name'");
695                if (separator.length() == 0)
696                        throw new DomainSpecException(
697                                missing_attr_value + "'sepatator'");
698                if (order.length() == 0)
699                        throw new DomainSpecException(
700                                missing_attr_value + "'order'");
701                //        RTUtil.debugInfo("name = " +name);
702                //        RTUtil.debugInfo("separator = " + separator);
703                //        RTUtil.debugInfo("order = " + order);
704                return new TreeType(
705                        name,
706                        separator,
707                        order.equals(ROOT_FIRST));
708        }
709        /**
710         * Method transformRecordTypeDeclaration.<br>
711     * Parses RecordType declaration in ADSD.
712        Returns a RecordType object by parsing element:
713        <pre>
714            &lt;RecordType name="record"&gt;
715                &lt;Field name="field1"&gt;
716                    &lt;Type domain="" name=""/&gt;
717                &lt;/Field&gt;
718                &lt;Field name="field2"&gt;
719                    &lt;Type domain="" name=""/&gt;
720                &lt;/Field&gt;
721                ...
722            &lt;/RecordType&gt;
723        </pre>
724         * @param ele
725         * @param currentSpec
726         * @return RecordType
727         * @throws DomainSpecException
728         */
729        private RecordType transformRecordTypeDeclaration(
730                Element ele,
731                DomainSpecification currentSpec)
732                throws DomainSpecException
733        {
734                if (!ele.getTagName().equals(RECORD_TYPE))
735                        throw new DomainSpecException("Not a RecordType element");
736                String name = ele.getAttribute(NAME);
737                if (name.length() == 0)
738                        throw new DomainSpecException(
739                                missing_attr_value + "'name'");
740                RTUtil.debugInfo(
741                        "parseRecordTypeDeclaration() > name = " + name);
742                NodeList childNodes = ele.getElementsByTagName(FIELD);
743                int size = childNodes.getLength();
744                if (size < 1)
745                        throw new DomainSpecException("No fields specified for the RecordType element.");
746                OrderedMap fieldDeclarations =
747                        new OrderedMap(size, String.class, DataType.class);
748                for (int i = 0; i < size; i++)
749                {
750                        Element field = (Element) childNodes.item(i);
751                        String fieldName = field.getAttribute(NAME);
752                        if (fieldName.length() == 0)
753                                throw new DomainSpecException(
754                                        missing_attr_value + "'name'");
755                        Element typeRefEle =
756                                getFirstChildElementByTagName(field, TYPE);
757                        String typeName = typeRefEle.getAttribute(NAME);
758                        if (typeName.length() == 0)
759                                throw new DomainSpecException(
760                                        missing_attr_value + "'name'");
761                        String typeDomain = typeRefEle.getAttribute(DOMAIN);
762                        RTUtil.debugInfo("fieldName = " + fieldName);
763                        //            RTUtil.debugInfo("typeName = " + typeName);
764                        //            RTUtil.debugInfo("typeDomain = " + typeDomain);
765                        DataType fieldType =
766                                currentSpec.lookupType(typeDomain, typeName);
767                        fieldDeclarations.put(fieldName, fieldType);
768                }
769                return new RecordType(name, fieldDeclarations);
770        }
771        /**
772         * Method transformPrincipalTypeDeclaration.<br>
773        Parses PrincipalType declaration in ADSD.
774       
775        Returns a SimpleType object by parsing element:
776        <pre>
777            &lt;PrincialType name="prinType"&gt;
778                &lt;TypeRef domain="" name=""/&gt;
779            &lt;/PrincialType&gt;
780        </pre>
781         * @param ele
782         * @param currentSpec
783         * @return SimpleType
784         * @throws DomainSpecException
785         */
786        private SimpleType transformPrincipalTypeDeclaration(
787                Element ele,
788                DomainSpecification currentSpec)
789                throws DomainSpecException
790        {
791                if (!ele.getTagName().equals(PRINCIPAL_TYPE))
792                        throw new DomainSpecException("Not a PrincipalType element");
793                Element typeRefEle = getFirstChildElementByTagName(ele, TYPE);
794                String domain = typeRefEle.getAttribute(DOMAIN);
795                String name = typeRefEle.getAttribute(NAME);
796                if (name.length() == 0)
797                        throw new DomainSpecException(
798                                missing_attr_value + "'name'");
799                return (SimpleType) currentSpec.lookupType(domain, name);
800        }
801        /**
802         * Method transformRoleDeclaration.<br>
803     * Parses RoleDeclaration in ADSD.
804        Returns a RoleDeclaration object by parsing element:
805        <pre>
806            &lt;RoleDeclaration name="record"&gt;
807                &lt;Parameter name="param1"&gt;
808                    &lt;Type domain="" name=""/&gt;
809                &lt;/Parameter&gt;
810                &lt;Parameter name="param2"&gt;
811                    &lt;Type domain="" name=""/&gt;
812                &lt;/Parameter&gt;
813                ...
814            &lt;/RoleDeclaration&gt;
815        </pre>
816         * @param ele
817         * @param currentSpec
818     *  the DomainSpecification in which the role is declared.
819         * @return RoleDeclaration
820         * @throws DomainSpecException
821         */
822        private RoleDeclaration transformRoleDeclaration(
823                Element ele,
824                DomainSpecification currentSpec)
825                throws DomainSpecException
826        {
827                if (!ele.getTagName().equals(ROLE_DECLARATION))
828                        throw new DomainSpecException("Not a RoleDeclaration element");
829                String roleName = ele.getAttribute(NAME);
830                if (roleName.length() == 0)
831                        throw new DomainSpecException(
832                                missing_attr_value + "'name'");
833                RTUtil.debugInfo(
834                        "parseRoleDeclaration > roleName = " + roleName);
835                boolean isIdentity =
836                        RTUtil.parseBoolean(ele.getAttribute(IS_IDENTITY), false);
837
838                int issuerT = RoleDeclaration.DEFAULT_ISSUER_TYPE;
839                int subjectT = RoleDeclaration.DEFAULT_SUBJECT_TYPE;
840
841                RoleDeclaration roleDeclaration = null;
842
843                Element[] elements = getChildElements(ele);
844                int n = elements.length;
845                OrderedMap parameterDeclarations =
846                        new OrderedMap(n, String.class, DataType.class);
847                for (int i = 0; i < n; i++)
848                {
849                        Element childEle = elements[i];
850                        String childName = childEle.getTagName();
851                        if (childName.equals(RESTRICTION))
852                        {
853                                Element baseRoleEle =
854                                        getFirstChildElementByTagName(
855                                                childEle,
856                                                BASE_ROLE);
857                                String baseRoleName = baseRoleEle.getAttribute(NAME);
858                                if (baseRoleName.length() == 0)
859                                        throw new DomainSpecException(
860                                                missing_attr_value + "'name'");
861                                String baseRoleDomain =
862                                        baseRoleEle.getAttribute(DOMAIN);
863                                // get base role
864                                RoleDeclaration baseRole =
865                                        currentSpec.lookupRoleDeclaration(
866                                                baseRoleDomain,
867                                                baseRoleName);
868                                // get new parameters
869                                NodeList paramNodes =
870                                        childEle.getElementsByTagName(PARAMETER);
871                                OrderedMap newParameters =
872                                        transformParameters(paramNodes, currentSpec);
873                                roleDeclaration =
874                                        RoleDeclaration.createRestrictionRole(
875                                                currentSpec,
876                                                roleName,
877                                                isIdentity,
878                                                baseRole,
879                                                newParameters);
880                        }
881                        else if (childName.equals(EXTENSION))
882                        {
883                                Element baseRoleEle =
884                                        getFirstChildElementByTagName(
885                                                childEle,
886                                                BASE_ROLE);
887                                String baseRoleName = baseRoleEle.getAttribute(NAME);
888                                if (baseRoleName.length() == 0)
889                                        throw new DomainSpecException(
890                                                missing_attr_value + "'name'");
891                                String baseRoleDomain =
892                                        baseRoleEle.getAttribute(DOMAIN);
893                                RoleDeclaration baseRole =
894                                        currentSpec.lookupRoleDeclaration(
895                                                baseRoleDomain,
896                                                baseRoleName);
897                                NodeList paramNodes =
898                                        childEle.getElementsByTagName(PARAMETER);
899                                OrderedMap newParameters =
900                                        transformParameters(paramNodes, currentSpec);
901                                roleDeclaration =
902                                        RoleDeclaration.createExtensionRole(
903                                                currentSpec,
904                                                roleName,
905                                                isIdentity,
906                                                baseRole,
907                                                newParameters);
908                        }
909                        else if (childName.equals(PROJECTION))
910                        {
911                                Element baseRoleEle =
912                                        getFirstChildElementByTagName(
913                                                childEle,
914                                                BASE_ROLE);
915                                String baseRoleName = baseRoleEle.getAttribute(NAME);
916                                if (baseRoleName.length() == 0)
917                                        throw new DomainSpecException(
918                                                missing_attr_value + "'name'");
919                                String baseRoleDomain =
920                                        baseRoleEle.getAttribute(DOMAIN);
921                                RoleDeclaration baseRole =
922                                        currentSpec.lookupRoleDeclaration(
923                                                baseRoleDomain,
924                                                baseRoleName);
925                                NodeList paramNodes =
926                                        childEle.getElementsByTagName(PARAMETER);
927                                int num = paramNodes.getLength();
928                                String[] newParamNames = new String[n];
929                                for (int j = 0; j < num; j++)
930                                {
931                                        Element e = (Element) paramNodes.item(j);
932                                        newParamNames[j] = e.getAttribute(NAME);
933                                }
934
935                                roleDeclaration =
936                                        RoleDeclaration.createProjectionRole(
937                                                currentSpec,
938                                                roleName,
939                                                isIdentity,
940                                                baseRole,
941                                                newParamNames);
942                        }
943                        else if (childName.equals(PLAIN))
944                        {
945                                String issuerTraces =
946                                        childEle.getAttribute(ISSUER_TRACES);
947                                String subjectTraces =
948                                        childEle.getAttribute(SUBJECT_TRACES);
949                                if (issuerTraces.length() != 0)
950                                {
951                                        issuerT =
952                                                ((Integer) (RoleDeclaration
953                                                        .issuerTracesTypes
954                                                        .get(issuerTraces.toLowerCase())))
955                                                        .intValue();
956                                }
957                                if (subjectTraces.length() != 0)
958                                {
959                                        subjectT =
960                                                ((Integer) (RoleDeclaration
961                                                        .subjectTracesTypes
962                                                        .get(subjectTraces.toLowerCase())))
963                                                        .intValue();
964                                }
965                                int dimension =
966                                        RTUtil.parseInt(
967                                                childEle.getAttribute(DIMENSION),
968                                                1);
969                                Element identityRoleEle =
970                                        getFirstChildElementByTagName(childEle, IDENTITY);
971                                /*  TODO
972                                String idBaseRoleName = identityRoleEle.getAttribute(NAME);
973                                if (idBaseRoleName.length() == 0)
974                                        throw new DomainSpecException(
975                                                missing_attr_value + "'name'");
976                                String idBaseRoleDomain = identityRoleEle.getAttribute(DOMAIN);
977                                RoleDeclaration idBaseRole =
978                                        currentSpec.lookupRoleDeclaration(
979                                                idBaseRoleDomain,
980                                                idBaseRoleName);
981                                */
982
983                                NodeList paramNodes =
984                                        childEle.getElementsByTagName(PARAMETER);
985                                OrderedMap newParameters =
986                                        transformParameters(paramNodes, currentSpec);
987                                roleDeclaration =
988                                        RoleDeclaration.createPlainRole(
989                                                currentSpec,
990                                                roleName,
991                                                issuerT,
992                                                subjectT,
993                                                dimension,
994                                                isIdentity,
995                                                newParameters);
996                        }
997
998                } // end of for loop
999                return roleDeclaration;
1000        }
1001        /**
1002         * Method transformParameters.<br>
1003         *  parses <code>Parameter</code> declarations in <code>RoleDeclaration</code> element.
1004         * @param nodes
1005     *  the list of <code>Parameter</code> elements
1006         * @return OrderedMap
1007     *  an OrderedMap of parameter declarations with parameter names as keys.
1008         */
1009        private OrderedMap transformParameters(
1010                NodeList nodes,
1011                DomainSpecification currentSpec)
1012                throws DomainSpecException
1013        {
1014                OrderedMap parameterDeclarations =
1015                        new OrderedMap(String.class, DataType.class);
1016                int n = nodes.getLength();
1017                for (int i = 0; i < n; i++)
1018                {
1019                        Element childEle = (Element) nodes.item(i);
1020                        String paramName = childEle.getAttribute(NAME);
1021                        RTUtil.debugInfo("paramName = " + paramName);
1022                        Element typeRefEle =
1023                                getFirstChildElementByTagName(childEle, TYPE);
1024                        String domain = typeRefEle.getAttribute(DOMAIN);
1025                        //RTUtil.debugInfo("typeDomain = " + domain);
1026                        String name = typeRefEle.getAttribute(NAME);
1027                        //RTUtil.debugInfo("typeName = " + name);
1028                        DataType paramType = currentSpec.lookupType(domain, name);
1029                        parameterDeclarations.put(paramName, paramType);
1030                }
1031                return parameterDeclarations;
1032        }
1033
1034        /*###########################################################
1035            #####
1036            ##### Parsing Credential or AccessRule
1037            #####
1038        #############################################################*/
1039        /**
1040         * Method transformCredential.<br>
1041         *   Returns a <code>CredentialDomain</code> object by parsing element:
1042         *   <pre>
1043         *       &lt;Credential&gt;
1044         *           ...
1045         *       &lt;/Credential&gt;
1046         *   </pre>
1047         * @param ele
1048         * @param rtContext
1049         * @return CredentialDomain
1050         * @throws DomainSpecException
1051         * @throws CredException
1052         */
1053        public synchronized CredentialDomain transformCredential(
1054                Element ele,
1055                RTContext rtContext)
1056                throws DomainSpecException, CredException
1057        {
1058                if (!ele.getTagName().equals(CREDENTIAL))
1059                        throw new DomainSpecException("Not a Credential element.");
1060                String hashID = ele.getAttribute(ID);
1061                if (hashID.length() == 0)
1062                {
1063                        //compute hashID
1064                }
1065                RTUtil.debugInfo("HashID = " + hashID);
1066                CredentialDomain credDomain =
1067                        new CredentialDomain(ele, rtContext);
1068                HashID id = new HashID(HashID.CREDENTIAL_DOMAIN, hashID);
1069                credDomain.setHashID(id);
1070
1071                Principal issuer = null;
1072
1073                Element[] elements = getChildElements(ele);
1074                for (int i = 0; i < elements.length; i++)
1075                {
1076                        Element child = elements[i];
1077                        String tagName = child.getTagName();
1078                        //RTUtil.debugInfo("***** tagName = " + tagName);
1079                        if (tagName.equals(PREAMBLE))
1080                        {
1081                                Element[] preambleElements = getChildElements(child);
1082                                for (int j = 0; j < preambleElements.length; j++)
1083                                {
1084                                        Element preambleChild = preambleElements[j];
1085                                        String preambleChildTag =
1086                                                preambleChild.getTagName();
1087                                        if (preambleChildTag.equals(IMPORT_DOMAIN))
1088                                                // 0 or more
1089                                        {
1090                                                //short name for the imported domain in the scope of this credential.
1091                                                String name =
1092                                                        preambleChild.getAttribute(NAME);
1093                                                if (name.length() == 0)
1094                                                        throw new DomainSpecException(
1095                                                                missing_attr_value + "'name'");
1096                                                RTUtil.debugInfo(
1097                                                        "ImportDomain's name = " + name);
1098                                                String uri = preambleChild.getAttribute(URI);
1099                                                RTUtil.debugInfo(
1100                                                        "ImportDomain's uri = " + uri);
1101                                                String idref =
1102                                                        preambleChild.getAttribute(IDREF);
1103                                                if (idref.length() == 0)
1104                                                        throw new DomainSpecException(
1105                                                                missing_attr_value + "'idref'");
1106                                                ApplicationDomain importedDomain =
1107                                                        locateDomain(idref, uri, rtContext);
1108                                                credDomain.importDomain(name, importedDomain);
1109                                        }
1110                                        else if (preambleChildTag.equals(PRINCIPAL))
1111                                                // 0 or more
1112                                        {
1113                                                // To improve readability, the principal, which could be quite long,
1114                                                // are included in the Preamble so that they can be referred to
1115                                                // in a compact way elsewhere in the credential by using PrincipalRef.
1116                                                RTUtil.debugInfo("parsing principle.....");
1117                                                transformPrincipalValue(
1118                                                        preambleChild,
1119                                                        credDomain);
1120                                        }
1121                                } // end for()..
1122                        }
1123                        else if (tagName.equals(ISSUER))
1124                        {
1125                                Element[] prinEle = getChildElements(child);
1126                                if (prinEle.length != 1)
1127                                        throw new DomainSpecException(
1128                                                improper_sub_element_for + "'Issuer'");
1129                                issuer =
1130                                        transformPrincipalValue(prinEle[0], credDomain)
1131                                                .getValue();
1132                                credDomain.setIssuer(issuer);
1133                        }
1134                        //                      else if (tagName.equals(CREDENTIAL_IDENTIFIER))
1135                        //                      {
1136                        //                              // TODO
1137                        //                              throw new UnsupportedOperationException(
1138                        //                                      no_support_for + "'CredentialIdentifier'");
1139                        //                      }
1140                        else if (tagName.equals(VALIDITY_TIME))
1141                        {
1142                                //TODO
1143                                //ValidityTime validityTime = parseValidityTime(child);
1144                                //credDomain.setValidityTime(validityTime);
1145                        }
1146                        else if (tagName.equals(VALIDITY_RULE))
1147                        {
1148                                // TODO
1149                                throw new UnsupportedOperationException(
1150                                        no_support_for + "'ValidityRule'");
1151                        }
1152                        else // ROLE_DEFINITION
1153                                {
1154                                RoleDefinition roleDef =
1155                                        transformRoleDefinition(
1156                                                issuer,
1157                                                credDomain,
1158                                                child);
1159                                credDomain.addRoleDefinition(roleDef);
1160                        }
1161                } //===== End of for(int i=0; i<numOfchildren; i++)....
1162                credDomain.setComplete();
1163                return credDomain;
1164        } //===== End of parseCredential()
1165        /*
1166            Returns an AccessRule object by parsing element:
1167            <pre>
1168                &lt;AccessRule&gt;
1169                    ...
1170                &lt;/AccessRule&gt;
1171            </pre>
1172        */
1173        public synchronized CredentialDomain transformAccessRule(
1174                Element ele,
1175                RTContext rtContext)
1176                throws DomainSpecException, CredException
1177        {
1178                if (!ele.getTagName().equals(ACCESS_RULE))
1179                        throw new DomainSpecException("Not an AccessRule element.");
1180                String hashID = ele.getAttribute(ID);
1181                if (hashID.length() == 0)
1182                {
1183                        //compute hashID
1184                }
1185
1186                CredentialDomain ruleDomain =
1187                        new CredentialDomain(ele, rtContext);
1188                HashID id = new HashID(HashID.CREDENTIAL_DOMAIN, hashID);
1189                ruleDomain.setHashID(id);
1190
1191                Principal issuer = null;
1192                Element[] elements = getChildElements(ele);
1193                for (int i = 0; i < elements.length; i++)
1194                {
1195                        Element child = elements[i];
1196                        String tagName = child.getTagName();
1197                        if (tagName.equals(PREAMBLE))
1198                        {
1199                                Element[] preambleElements = getChildElements(child);
1200                                for (int j = 0; j < preambleElements.length; j++)
1201                                {
1202                                        Element preambleChild = preambleElements[j];
1203                                        String preambleChildTag =
1204                                                preambleChild.getTagName();
1205                                        if (preambleChildTag.equals(IMPORT_DOMAIN))
1206                                                // 0 or more
1207                                        {
1208                                                String name =
1209                                                        preambleChild.getAttribute(NAME);
1210                                                if (name.length() == 0)
1211                                                        throw new DomainSpecException(
1212                                                                missing_attr_value + "'name'");
1213                                                String uri = preambleChild.getAttribute(URI);
1214                                                String idref =
1215                                                        preambleChild.getAttribute(IDREF);
1216                                                if (idref.length() == 0)
1217                                                        throw new DomainSpecException(
1218                                                                missing_attr_value + "'idref'");
1219                                                ApplicationDomain importedDomain =
1220                                                        locateDomain(idref, uri, rtContext);
1221                                                ruleDomain.importDomain(name, importedDomain);
1222                                        }
1223                                        else if (preambleChildTag.equals(PRINCIPAL))
1224                                                // 0 or more
1225                                        {
1226                                                transformPrincipalValue(
1227                                                        preambleChild,
1228                                                        ruleDomain);
1229                                        }
1230                                } // end for()..
1231                        }
1232                        else if (tagName.equals(RULE_IDENTIFIER))
1233                        {
1234                                // TODO
1235                                throw new UnsupportedOperationException(
1236                                        no_support_for + "'RuleIdentifier'");
1237                        }
1238                        else if (tagName.equals(VALIDITY_TIME))
1239                        {
1240                                //                ValidityTime validityTime = parseValidityTime(child);
1241                                //                accessRule.setValidityTime(validityTime);
1242                        }
1243                        else
1244                        {
1245                                RoleDefinition roleDef =
1246                                        transformRoleDefinition(
1247                                                issuer,
1248                                                ruleDomain,
1249                                                child);
1250                                ruleDomain.addRoleDefinition(roleDef);
1251                        }
1252                } // End of for(int i=0; i<numOfchildren; i++)....
1253                ruleDomain.setComplete();
1254                return ruleDomain;
1255        } // End of parseAccessRule()
1256
1257        //      private parsePrincipalInfo()
1258        //      {
1259        //                              java.security.PublicKey key = null;
1260        //
1261        //                              Element[] principalElements = getChildElements(elements[0]);
1262        //                              if (principalElements.length != 1)
1263        //                                      throw new DomainSpecException(
1264        //                                              improper_sub_element_for + "'ds:KeyValue'");
1265        //                              String tagName = principalElements[0].getTagName();
1266        //                              if (tagName.equals(DSA_KEY_VALUE))
1267        //                              {
1268        //                                      try
1269        //                                      {
1270        //                                              DSAKeyValue keyvalue =
1271        //                                                      new DSAKeyValue(principalElements[0], null);
1272        //                                              key = keyvalue.getPublicKey();
1273        //                                      }
1274        //                                      catch (XMLSecurityException e)
1275        //                                      {
1276        //                                              e.printStackTrace();
1277        //                                              throw new DomainSpecException(e.getMessage());
1278        //                                      }
1279        //
1280        //                                      res =
1281        //                                              new Principal(
1282        //                                                      new KeyValue(new KeyType(KeyType.DSA), key));
1283        //                              }
1284        //                              else if (tagName.equals(RSA_KEY_VALUE))
1285        //                              {
1286        //                                      try
1287        //                                      {
1288        //                                              RSAKeyValue keyvalue =
1289        //                                                      new RSAKeyValue(principalElements[0], null);
1290        //                                              key = keyvalue.getPublicKey();
1291        //                                      }
1292        //                                      catch (XMLSecurityException e)
1293        //                                      {
1294        //                                              e.printStackTrace();
1295        //                                              throw new DomainSpecException(e.getMessage());
1296        //                                      }
1297        //                                      res =
1298        //                                              new Principal(
1299        //                                                      new KeyValue(new KeyType(KeyType.RSA), key));
1300        //                              }
1301        //                              else // any ###other
1302        //                                      {
1303        //                                      // TODO
1304        //                              }
1305        //             
1306        //      }
1307        //      QQ: Currently CredentialDomain is credential-specific. The good part is that
1308        //          the naming needs to be only unique in terms of one credential. However,
1309        //          it might be more efficient to have some sort of global environment to
1310        //          keep the information commonly shared, such as principal keyvalue.
1311        //          For example, in the credential preambel, we could define principals
1312        //          in advance for this credential. But different credentials are very
1313        //          likely to use same principals. If we have a global env to keep it, then
1314        //          we don't need to pass the credDomain around.
1315        /**
1316         * Method transformPrincipalValue.<br>
1317         * @param ele
1318         * @param credDomain
1319         * @return PrincipalValue
1320         * @throws DomainSpecException
1321         */
1322        private PrincipalValue transformPrincipalValue(
1323                Element ele,
1324                CredentialDomain credDomain)
1325                throws DomainSpecException
1326        {
1327                String tag = ele.getTagName();
1328                if (!tag.equals(PRINCIPAL) && !tag.equals(PRINCIPAL_REF))
1329                        throw new DomainSpecException("Not a Principal element");
1330                Principal res = null;
1331                PrincipalValue value = null;
1332                String id = null;
1333                SimpleType principalType = credDomain.getPrincipalType();
1334                if (tag.equals(PRINCIPAL))
1335                {
1336                        id = ele.getAttribute(SHORT_NAME);
1337                        Element[] elements = getChildElements(ele);
1338                        if (elements.length != 1)
1339                                throw new DomainSpecException(
1340                                        improper_sub_element_for + "'Principal'");
1341                        String eleTag = elements[0].getTagName();
1342                        if (eleTag.equals(INTEGER_VALUE))
1343                        {
1344                                res =
1345                                        new DataValuePrincipal(
1346                                                transformIntegerValue(
1347                                                        (IntegerType) principalType,
1348                                                        elements[0]));
1349                        }
1350                        else if (eleTag.equals(STRING_VALUE))
1351                        {
1352                                res =
1353                                        new DataValuePrincipal(
1354                                                transformStringValue(
1355                                                        (StringType) principalType,
1356                                                        elements[0]));
1357                        }
1358                        else if (eleTag.equals(KEY_HASH))
1359                        {
1360                                Node textNode = elements[0].getFirstChild();
1361                                if (textNode.getNodeType()
1362                                        != org.w3c.dom.Node.TEXT_NODE)
1363                                        throw new DomainSpecException("Wrong elements for StringValue");
1364                                String hashValue = textNode.getNodeValue();
1365                                res = new PublicKeyPrincipal(hashValue);
1366                        }
1367                        credDomain.addPrincipal(id, res);
1368                }
1369                else if (tag.equals(PRINCIPAL_REF))
1370                {
1371                        id = ele.getAttribute(SHORT_NAME);
1372                        RTUtil.debugInfo("shortName = " + id);
1373                        res = credDomain.getPrincipal(id);
1374                        if (res == null)
1375                                throw new DomainSpecException("Principal not found");
1376                        value = new PrincipalValue(res);
1377                }
1378                return value;
1379        }
1380        /**
1381         * Method transformRoleDefinition.<br>
1382     * Returns a role definition object.
1383   
1384        <p>
1385        A role definition may be one the the following elements, i.e.
1386        <b>SimpleMember, SimpleContainment, LinkedContainment, IntersectionContainment,
1387        ProductContainment, ExclusiveProductContainment, SimpleDelegation, or
1388        AdvancedDelegation</b>.  Each role definition has a head and a body. The head
1389        part is defined by a HeadRoleTerm. The difference between the above 8
1390        elements lies in the body part.
1391       
1392        <P>
1393        The product role definition represented by <b>ProductContainment</b> and
1394        <b>ExclusiveProductContainment</b> has not been supported yet.
1395         * @param issuer
1396         * @param credDomain
1397         * @param ele
1398         * @return RoleDefinition
1399         * @throws CredException
1400         * @throws DomainSpecException
1401         */
1402        private RoleDefinition transformRoleDefinition(
1403                Principal issuer,
1404                CredentialDomain credDomain,
1405                Element ele)
1406                throws CredException, DomainSpecException
1407        {
1408                RTUtil.debugInfo("parseRoleDefinition().... ");
1409                String tag = ele.getTagName();
1410                RTUtil.debugInfo("parseRoleDefinition()'s tag = " + tag);
1411                if (!tag.equals(SIMPLE_MEMBER)
1412                        && !tag.equals(SIMPLE_CONTAINMENT)
1413                        && !tag.equals(LINKED_CONTAINMENT)
1414                        && !tag.equals(INTERSECTION_CONTAINMENT)
1415                        && !tag.equals(PRODUCT_CONTAINMENT)
1416                        && !tag.equals(EXCLUSIVE_PRODUCT_CONTAINMENT)
1417                        && !tag.equals(SIMPLE_DELEGATION)
1418                        && !tag.equals(ADVANCED_DELEGATION))
1419                        throw new DomainSpecException("Not a proper role definition element");
1420                RoleDefinition roleDefinition = null;
1421                if (tag.equals(SIMPLE_DELEGATION)
1422                        || tag.equals(ADVANCED_DELEGATION))
1423                {
1424                        Element[] elements = getChildElements(ele);
1425                        if (elements.length < 2 || elements.length > 3)
1426                                throw new DomainSpecException(
1427                                        improper_sub_element_for + "delegation role");
1428                        Element headElement = elements[0];
1429                        Element delegateToElement = elements[1];
1430                        Element controlElement = null; // optional
1431                        if (elements.length == 3)
1432                                controlElement = elements[2];
1433                        Role head =
1434                                new Role(
1435                                        issuer,
1436                                        transformRoleTerm(headElement, credDomain));
1437                        DelegationRole body =
1438                                transformDelegation(
1439                                        issuer,
1440                                        credDomain,
1441                                        delegateToElement,
1442                                        controlElement);
1443                        roleDefinition =
1444                                new RoleDefinition(credDomain, head, body);
1445                }
1446                else
1447                {
1448                        Element[] elements = getChildElements(ele);
1449                        if (elements.length != 2)
1450                                throw new DomainSpecException(
1451                                        improper_sub_element_for + "role definition");
1452                        Element headElement = elements[0];
1453                        Element bodyElement = elements[1];
1454                        Role head =
1455                                new Role(
1456                                        issuer,
1457                                        transformRoleTerm(headElement, credDomain));
1458                        PrincipalExpression body =
1459                                transformBody(issuer, credDomain, bodyElement);
1460                        roleDefinition =
1461                                new RoleDefinition(credDomain, head, body);
1462                }
1463                return roleDefinition;
1464        }
1465        /**
1466         * Method transformBody.<br>
1467     * Returns a PrincipalExpression for the body of a role definition.
1468        <P>
1469        The body can be one of the following: PrincipalValue, RoleTerm,
1470        ExternalRole, LinkedRole, Intersection, Product, or a Delegation.
1471   
1472        The method handles all types of the role definition body except the
1473        Delegation role body.
1474         * @param issuer
1475         * @param credDomain
1476         * @param ele
1477         * @return PrincipalExpression
1478         * @throws CredException
1479         * @throws DomainSpecException
1480         */
1481        private PrincipalExpression transformBody(
1482                Principal issuer,
1483                CredentialDomain credDomain,
1484                Element ele)
1485                throws CredException, DomainSpecException
1486        {
1487                String tag = ele.getTagName();
1488                RTUtil.debugInfo("parseBody() tag = " + tag);
1489                PrincipalExpression prinExp = null;
1490                if (tag.startsWith(PRINCIPAL))
1491                {
1492                        prinExp =
1493                                transformPrincipalValue(ele, credDomain).getValue();
1494                }
1495                else if (tag.equals(ROLE_TERM))
1496                {
1497                        RoleTerm roleTerm = transformRoleTerm(ele, credDomain);
1498                        prinExp = new Role(issuer, roleTerm);
1499                }
1500                else if (tag.equals(EXTERNAL_ROLE))
1501                {
1502                        prinExp = transformExternalRole(ele, credDomain);
1503                }
1504                else if (tag.equals(LINKED_ROLE))
1505                {
1506                        NodeList nodes = ele.getElementsByTagName(ROLE_TERM);
1507                        if (nodes.getLength() != 2)
1508                                throw new DomainSpecException(
1509                                        improper_sub_element_for + "'LinkedRole'");
1510                        Element roleTermEle1 = (Element) nodes.item(0);
1511                        Element roleTermEle2 = (Element) nodes.item(1);
1512                        prinExp =
1513                                new LinkedRole(
1514                                        transformRoleTerm(roleTermEle1, credDomain),
1515                                        transformRoleTerm(roleTermEle2, credDomain));
1516                }
1517                else if (tag.equals(INTERSECTION))
1518                {
1519                        prinExp = transformRoleIntersection(ele, credDomain);
1520                }
1521                else if (tag.equals(PRODUCT))
1522                {
1523                        // TODO
1524                        throw new UnsupportedOperationException(
1525                                no_support_for + "'Product'");
1526                }
1527                else if (tag.equals(EXCLUSIVE_PRODUCT))
1528                {
1529                        // TODO
1530                        throw new UnsupportedOperationException(
1531                                no_support_for + "'ExclusiveProduct");
1532                }
1533                return prinExp;
1534        }
1535        /**
1536         * Method transformDelegation.<br>
1537        Returns a DelegationRole object for the body of a role definition
1538        by parsing element: <br>
1539        <pre>
1540            &lt;SimpleDelegation&gt;
1541                ...
1542            &lt;/SimpleDelegation&gt;
1543           
1544            Or
1545           
1546            &lt;AdvancedDelegation&gt;
1547                ...
1548            &lt;/AdvancedDelegation&gt;
1549        </pre>
1550         * @param issuer
1551         * @param credDomain
1552         * @param delegationElement
1553         * @param controlElement
1554         * @return DelegationRole
1555         * @throws DomainSpecException
1556         * @throws CredException
1557         */
1558        private DelegationRole transformDelegation(
1559                Principal issuer,
1560                CredentialDomain credDomain,
1561                Element delegationElement,
1562                Element controlElement)
1563                throws DomainSpecException, CredException
1564        {
1565                if (!delegationElement.getTagName().equals(DELEGATE_TO))
1566                        throw new DomainSpecException("Not a delegateTo element");
1567                if (controlElement != null
1568                        && !controlElement.getTagName().equals(CONTROL))
1569                        throw new DomainSpecException("Not a Control element");
1570                PrincipalExpression delegateTo = null;
1571                Role control = null;
1572                // delegateTo
1573                Element[] elements = getChildElements(delegationElement);
1574                for (int i = 0; i < elements.length; i++)
1575                {
1576                        Element child = elements[i];
1577                        String childName = child.getTagName();
1578                        if (childName.startsWith(PRINCIPAL))
1579                        {
1580                                delegateTo =
1581                                        transformPrincipalValue(child, credDomain)
1582                                                .getValue();
1583                        }
1584                        else if (childName.equals(ROLE_TERM))
1585                        {
1586                                RoleTerm roleTerm =
1587                                        transformRoleTerm(child, credDomain);
1588                                delegateTo = new Role(issuer, roleTerm);
1589                        }
1590                        else
1591                        {
1592                                throw new DomainSpecException(
1593                                        improper_sub_element_for + "'DelegatTo'");
1594                        }
1595                }
1596                // control
1597                if (controlElement != null)
1598                {
1599                        elements = getChildElements(controlElement);
1600                        for (int i = 0; i < elements.length; i++)
1601                        {
1602                                Element child = elements[i];
1603                                String childName = child.getTagName();
1604                                if (childName.equals(ROLE_TERM))
1605                                {
1606                                        RoleTerm roleTerm =
1607                                                transformRoleTerm(child, credDomain);
1608                                        control = new Role(issuer, roleTerm);
1609                                }
1610                                else if (childName.equals(EXTERNAL_ROLE))
1611                                {
1612                                        control =
1613                                                transformExternalRole(child, credDomain);
1614                                }
1615                                else
1616                                {
1617                                        throw new DomainSpecException(
1618                                                improper_sub_element_for + "'Control'");
1619                                }
1620                        }
1621                }
1622                return new DelegationRole(delegateTo, control);
1623        }
1624        /**
1625         * Method transformExternalRole.<br>
1626     * Returns an ExternalRole object by parsing element:
1627        <pre>
1628            &lt;ExternalRole&gt;
1629                ...
1630            &lt;/ExternalRole&gt;
1631        </pre>
1632         * @param ele
1633         * @param credDomain
1634         * @return Role
1635         * @throws CredException
1636         * @throws DomainSpecException
1637         */
1638        private Role transformExternalRole(
1639                Element ele,
1640                CredentialDomain credDomain)
1641                throws CredException, DomainSpecException
1642        {
1643                if (!ele.getTagName().equals(EXTERNAL_ROLE))
1644                        throw new DomainSpecException("Not an ExternalRole element.");
1645                Element[] elements = getChildElements(ele);
1646                if (elements.length != 2)
1647                        throw new DomainSpecException(
1648                                improper_sub_element_for + "'ExternalRole'");
1649                Element prinEle = elements[0];
1650                Element roleTermEle = elements[1];
1651                return new Role(
1652                        transformPrincipalValue(prinEle, credDomain).getValue(),
1653                        transformRoleTerm(roleTermEle, credDomain));
1654        }
1655        /**
1656         * Method transformRoleIntersection.<br>
1657     * Returns an ExternalRole object by parsing element:
1658        <pre>
1659            &lt;Intersection&gt;
1660                ...
1661            &lt;/Intersection&gt;
1662        </pre>
1663         * @param ele
1664         * @param credDomain
1665         * @return RoleIntersection
1666         * @throws CredException
1667         * @throws DomainSpecException
1668         */
1669        private RoleIntersection transformRoleIntersection(
1670                Element ele,
1671                CredentialDomain credDomain)
1672                throws CredException, DomainSpecException
1673        {
1674                if (!ele.getTagName().equals(INTERSECTION))
1675                        throw new DomainSpecException("Not an Intersection element.");
1676                RoleIntersection ri = new RoleIntersection();
1677                Element[] elements = getChildElements(ele);
1678                int n = elements.length;
1679                for (int i = 0; i < n; i++)
1680                {
1681                        Element child = elements[i];
1682                        String childTag = child.getTagName();
1683                        if (childTag.equals(ROLE_TERM))
1684                        {
1685                                ri.and(transformRoleTerm(child, credDomain));
1686                        }
1687                        else if (childTag.equals(EXTERNAL_ROLE))
1688                        {
1689                                ri.and(transformExternalRole(child, credDomain));
1690                        }
1691                        else
1692                        {
1693                                throw new DomainSpecException(
1694                                        improper_sub_element_for + "'Intersection'");
1695                        }
1696                }
1697                return ri;
1698        }
1699        /**
1700         * Method transformRoleTerm.<br>
1701     * Returns a RoleTerm object by parsing element:
1702        <pre>
1703            &lt;RoleTerm&gt;
1704                ...
1705            &lt;/RoleTerm&gt;
1706        </pre>
1707         * @param ele
1708         * @param credDomain
1709         * @return RoleTerm
1710         * @throws CredException
1711         * @throws DomainSpecException
1712         */
1713        private RoleTerm transformRoleTerm(
1714                Element ele,
1715                CredentialDomain credDomain)
1716                throws CredException, DomainSpecException
1717        {
1718                if (!ele.getTagName().equals(HEAD_ROLE_TERM)
1719                        && !ele.getTagName().equals(ROLE_TERM))
1720                        throw new DomainSpecException("Not a RoleTerm element.");
1721                String roleName = ele.getAttribute(NAME);
1722                if (roleName.length() == 0)
1723                        throw new DomainSpecException(
1724                                missing_attr_value + "'name'");
1725                String domain = ele.getAttribute(DOMAIN);
1726                RTUtil.debugInfo("**** roleName = " + roleName);
1727                RTUtil.debugInfo("**** domain = " + domain);
1728                RoleDeclaration roleDecl =
1729                        credDomain.lookupRoleDeclaration(domain, roleName);
1730                RoleTerm roleTerm = new RoleTerm(roleDecl);
1731                NodeList children = ele.getElementsByTagName(PARAMETER);
1732                int n = children.getLength();
1733                for (int i = 0; i < n; i++)
1734                {
1735                        StringBuffer prefix = new StringBuffer(roleName);
1736                        Element paramEle = (Element) children.item(i);
1737                        String paramName = paramEle.getAttribute(NAME);
1738                        //prefix.append(COLON).append(paramName);
1739                        transformParameter(
1740                                paramEle,
1741                                prefix,
1742                                roleTerm,
1743                                credDomain);
1744                }
1745                return roleTerm;
1746        }
1747        /**
1748         * Method transformParameter.<br>
1749        parses element:
1750        <pre>
1751            &lt;Parameter name="aa" id="s"&gt;
1752                ...
1753            &lt;/Parameter&gt;
1754        </pre>
1755         * @param ele
1756         * @param prefix
1757         * @param roleTerm
1758         * @param credDomain
1759         * @throws DomainSpecException
1760         */
1761        //ValueSet
1762        private void transformParameter(
1763                Element ele,
1764                StringBuffer prefix,
1765                RoleTerm roleTerm,
1766                CredentialDomain credDomain)
1767                throws DomainSpecException
1768        {
1769                if (!ele.getTagName().equals(PARAMETER))
1770                        throw new DomainSpecException("Not a Parameter element.");
1771                String parameterName = ele.getAttribute(NAME);
1772                if (parameterName.length() == 0)
1773                        throw new DomainSpecException(
1774                                missing_attr_value + "'name'");
1775                prefix.append(COLON).append(parameterName);
1776                DataType paramType = roleTerm.getParameterType(parameterName);
1777                String id = ele.getAttribute(ID);
1778                RTUtil.debugInfo("transformParameter() id = " + id);
1779                Element[] elements = getChildElements(ele);
1780                if (elements.length > 1)
1781                        throw new DomainSpecException(
1782                                improper_sub_element_for + "'Parameter'");
1783                if (elements.length == 0)
1784                {
1785                        // TODO: Create a null parameter value in this case.
1786                        // It is related to Equals Element.
1787                        ValueSet nullParameterValue = new ValueSet(paramType);
1788                        roleTerm.putParameterValue(
1789                                prefix.toString(),
1790                                nullParameterValue);
1791                }
1792                else
1793                {
1794                        Element valueEle = elements[0];
1795                        String tag = valueEle.getTagName();
1796                        if (tag.equals(EQUALS))
1797                        {
1798                                // TODO
1799                                throw new UnsupportedOperationException(
1800                                        no_support_for + "'Equals'");
1801                        }
1802                        else if (tag.equals(SPECIAL_PRINCIPAL))
1803                        {
1804                                // TODO
1805                                throw new UnsupportedOperationException(
1806                                        no_support_for + "'SpecialPrincipal'");
1807                        }
1808                        // the rest of the elements go to ValueSet
1809                        else
1810                        {
1811                                transformValueSetGroup(
1812                                        paramType,
1813                                        valueEle,
1814                                        prefix,
1815                                        roleTerm,
1816                                        credDomain);
1817                        }
1818                }
1819        }
1820        /**
1821         * Method transformValueSetGroup.<br>
1822        Parses Parameter value elements and stores the prefix and value mapping
1823        in the RoleTerm object.
1824         * @param type
1825         * @param ele
1826         * @param prefix
1827         * @param roleTerm
1828         * @param credDomain
1829         * @throws DomainSpecException
1830         */
1831        //ValueSet
1832        private void transformValueSetGroup(
1833                DataType type,
1834                Element ele,
1835                StringBuffer prefix,
1836                RoleTerm roleTerm,
1837                CredentialDomain credDomain)
1838                throws DomainSpecException
1839        {
1840                ValueSet parameterValue = null;
1841                String tag = ele.getTagName();
1842       
1843                if (tag.equals(TREE_VALUE)) // TreeValueSet
1844                {
1845                        parameterValue = transformTreeValue((TreeType) type, ele);
1846                        roleTerm.putParameterValue(
1847                                prefix.toString(),
1848                                parameterValue);
1849                }
1850                else if (tag.equals(INTERVAL)) // IntervalValueSet
1851                {
1852                        parameterValue =
1853                                transformIntervalValue((OrderedType) type, ele);
1854                        roleTerm.putParameterValue(
1855                                prefix.toString(),
1856                                parameterValue);
1857                }
1858                else if (tag.equals(SET)) // SetValueSet
1859                {
1860                        Element[] elements = getChildElements(ele);
1861                        String memberTag = elements[0].getTagName();
1862                        if (memberTag.equals(INTEGER_VALUE))
1863                        {
1864                                SetValueSet set = new SetValueSet((IntegerType) type);
1865                                IntegerValue v = null;
1866                                for (int i = 0; i < elements.length; i++)
1867                                {
1868                                        Element memberEle = elements[i];
1869                                        v =
1870                                                transformIntegerValue(
1871                                                        (IntegerType) type,
1872                                                        memberEle);
1873                                        set.addValue(v);
1874                                }
1875                                parameterValue = set;
1876                        }
1877                        else if (memberTag.equals(DECIMAL_VALUE))
1878                        {
1879                                SetValueSet set = new SetValueSet((DecimalType) type);
1880                                DecimalValue v = null;
1881                                for (int i = 0; i < elements.length; i++)
1882                                {
1883                                        Element memberEle = elements[i];
1884                                        v =
1885                                                transformDecimalValue(
1886                                                        (DecimalType) type,
1887                                                        memberEle);
1888                                        set.addValue(v);
1889                                }
1890                                parameterValue = set;
1891                        }
1892                        else if (memberTag.equals(STRING_VALUE))
1893                        {
1894                                SetValueSet set = new SetValueSet((StringType) type);
1895                                StringValue v = null;
1896                                for (int i = 0; i < elements.length; i++)
1897                                {
1898                                        Element memberEle = elements[i];
1899                                        v =
1900                                                transformStringValue(
1901                                                        (StringType) type,
1902                                                        memberEle);
1903                                        set.addValue(v);
1904                                }
1905                                parameterValue = set;
1906                        }
1907                        else if (memberTag.equals(ENUM_VALUE))
1908                        {
1909                                SetValueSet set = new SetValueSet((EnumType) type);
1910                                EnumValue v = null;
1911                                for (int i = 0; i < elements.length; i++)
1912                                {
1913                                        Element memberEle = elements[i];
1914                                        v =
1915                                                transformEnumValue(
1916                                                        (EnumType) type,
1917                                                        memberEle);
1918                                        set.addValue(v);
1919                                }
1920                                parameterValue = set;
1921                        }
1922                        else if (memberTag.startsWith(PRINCIPAL))
1923                        {
1924                                SetValueSet set = new SetValueSet((KeyType) type);
1925                                PrincipalValue v = null;
1926                                for (int i = 0; i < elements.length; i++)
1927                                {
1928                                        Element memberEle = elements[i];
1929                                        v =
1930                                                transformPrincipalValue(
1931                                                        memberEle,
1932                                                        credDomain);
1933                                        set.addValue(v);
1934                                }
1935                                parameterValue = set;
1936                        }
1937                        roleTerm.putParameterValue(
1938                                prefix.toString(),
1939                                parameterValue);
1940                }
1941                else if (tag.equals(RECORD)) // Record
1942                {
1943                        Element[] elements = getChildElements(ele);
1944                        // Fields
1945                        for (int i = 0; i < elements.length; i++)
1946                        {
1947                                StringBuffer newPrefix =
1948                                        new StringBuffer(prefix.toString());
1949                                String fieldName = elements[i].getAttribute(NAME);
1950                                if (fieldName.length() == 0)
1951                                        throw new DomainSpecException(
1952                                                missing_attr_value + "'name'");
1953                                RTUtil.debugInfo(
1954                                        "transformValueSetGroup.field name = "
1955                                                + fieldName);
1956                                newPrefix.append(COLON).append(fieldName);
1957                                DataType fieldType =
1958                                        ((RecordType) type).getFieldType(fieldName);
1959                                if (fieldType != null)
1960                                        RTUtil.debugInfo(
1961                                                "transformValueSetGroup.type name = "
1962                                                        + fieldType.getName());
1963                                else
1964                                        RTUtil.debugInfo(
1965                                                "transformValueSetGroup.type  =  null");
1966                                Element valueEle = getChildElements(elements[i])[0];
1967                                // recurrsion
1968                                transformValueSetGroup(
1969                                        fieldType,
1970                                        valueEle,
1971                                        newPrefix,
1972                                        roleTerm,
1973                                        credDomain);
1974                        }
1975                }
1976                else // SingletonValueSet
1977                        {
1978                        transformValueGroup(
1979                                (SimpleType) type,
1980                                ele,
1981                                prefix,
1982                                roleTerm,
1983                                credDomain);
1984                }
1985        } // end of transformValueSetGroup()
1986        /**
1987         * Method transformValueGroup.<br>
1988        Parses Value choice group, which can be one of the following:
1989        IntegerValue, DecimalValue, StringValue, EnumValue, TimeValue,
1990        or PrincipalValue.
1991         * @param type
1992         * @param ele
1993         * @param prefix
1994         * @param roleTerm
1995         * @param credDomain
1996         * @throws DomainSpecException
1997         */
1998        //SingletonValueSet
1999        private void transformValueGroup(
2000                SimpleType type,
2001                Element ele,
2002                StringBuffer prefix,
2003                RoleTerm roleTerm,
2004                CredentialDomain credDomain)
2005                throws DomainSpecException
2006        {
2007                SingletonValueSet value = null;
2008                String tag = ele.getTagName();
2009                if (tag.equals(INTEGER_VALUE))
2010                {
2011                        value =
2012                                new SingletonValueSet(
2013                                        (IntegerType) type,
2014                                        transformIntegerValue((IntegerType) type, ele));
2015                        roleTerm.putParameterValue(prefix.toString(), value);
2016                }
2017                else if (tag.equals(DECIMAL_VALUE))
2018                {
2019                        value =
2020                                new SingletonValueSet(
2021                                        (DecimalType) type,
2022                                        transformDecimalValue((DecimalType) type, ele));
2023                        roleTerm.putParameterValue(prefix.toString(), value);
2024                }
2025                else if (tag.equals(STRING_VALUE))
2026                {
2027                        value =
2028                                new SingletonValueSet(
2029                                        (StringType) type,
2030                                        transformStringValue((StringType) type, ele));
2031                        roleTerm.putParameterValue(prefix.toString(), value);
2032                }
2033                else if (tag.equals(ENUM_VALUE))
2034                {
2035                        value =
2036                                new SingletonValueSet(
2037                                        (EnumType) type,
2038                                        transformEnumValue((EnumType) type, ele));
2039                        roleTerm.putParameterValue(prefix.toString(), value);
2040                }
2041                else if (tag.equals(TIME_VALUE))
2042                {
2043                        // TODO
2044                        //throw new UnsupportedOperationException(no_support_for+"'TimeValue'");
2045                        //roleTerm.putParameterValue(prefix.toString(), valu
2046                }
2047                else if (tag.startsWith(PRINCIPAL))
2048                {
2049                        value =
2050                                new SingletonValueSet(
2051                                        credDomain.getPrincipalType(),
2052                                        transformPrincipalValue(ele, credDomain));
2053                        roleTerm.putParameterValue(prefix.toString(), value);
2054                }
2055        } // end of transformValueGroup()
2056        /**
2057         * Method transformIntervalValue.<br>
2058        Returns an IntervalValue object by parsing element:
2059        <pre>
2060            &lt;Interval includeMin="true" includeMax="true"&gt;
2061                &lt;min&gt; ... &lt;/min&gt;
2062                &lt;max&gt; ... &lt;/max&gt;
2063            &lt;/Interval&gt;
2064        </pre>
2065         * @param type
2066         * @param ele
2067         * @return IntervalValueSet
2068         * @throws DomainSpecException
2069         */
2070        private IntervalValueSet transformIntervalValue(
2071                OrderedType type,
2072                Element ele)
2073                throws DomainSpecException
2074        {
2075                if (!ele.getTagName().equals(INTERVAL))
2076                        throw new DomainSpecException("Not an Interval element");
2077                Element fromEle = getFirstChildElementByTagName(ele, FROM);
2078                Element toEle = getFirstChildElementByTagName(ele, TO);
2079                Element e1 = null, e2 = null;
2080                if (fromEle != null)
2081                        e1 = getFirstChildElement(fromEle);
2082                if (toEle != null)
2083                        e2 = getFirstChildElement(toEle);
2084                if (e1 != null && e2 != null)
2085                {
2086                        if (!e1.getTagName().equals(e2.getTagName()))
2087                                throw new DomainSpecException(
2088                                        improper_sub_element_for + "'Interval'");
2089                }
2090                DataValue min = null, max = null;
2091                if (e1 != null)
2092                        min = transformOrderedValue(type, e1);
2093                if (e2 != null)
2094                        max = transformOrderedValue(type, e2);
2095                return new IntervalValueSet(type, min, max);
2096        }
2097        /**
2098         * Method transformOrderedValue.<br>
2099        Returns DataValue by parsing elements:
2100        <pre>
2101            &lt;IntegerValue&gt; ... &lt;/IntegerValue&gt; or
2102   
2103            &lt;DecimalValue&gt; ... &lt;/DecimalValue&gt; or
2104   
2105            &lt;StirngValue&gt; ... &lt;/StringValue&gt; or
2106   
2107            &lt;EnumValue&gt; ... &lt;/EnumValue&gt; or
2108   
2109            &lt;TimeValue&gt; ... &lt;/timeValue&gt;
2110        </pre>
2111         * @param type
2112         * @param ele
2113         * @return DataValue
2114         * @throws DomainSpecException
2115         */
2116        private DataValue transformOrderedValue(
2117                OrderedType type,
2118                Element ele)
2119                throws DomainSpecException
2120        {
2121                DataValue value = null;
2122                String tag = ele.getTagName();
2123                if (tag.equals(INTEGER_VALUE))
2124                {
2125                        value = transformIntegerValue((IntegerType) type, ele);
2126                }
2127                if (tag.equals(DECIMAL_VALUE))
2128                {
2129                        value = transformDecimalValue((DecimalType) type, ele);
2130                }
2131                if (tag.equals(STRING_VALUE))
2132                {
2133                        value = transformStringValue((StringType) type, ele);
2134                }
2135                if (tag.equals(ENUM_VALUE))
2136                {
2137                        value = transformEnumValue((EnumType) type, ele);
2138                }
2139                if (tag.equals(TIME_VALUE))
2140                {
2141                        //value = transformTimeValue((TimeType)type, ele);
2142                }
2143                return value;
2144        }
2145        /**
2146         * Method transformIntegerValue.<br>
2147        Returns an IntegerValue object by parsing element:
2148        <pre>
2149            &lt;IntegerValue&gt;
2150                123
2151            &lt;/IntegerValue&gt;
2152        </pre>
2153         * @param type
2154         * @param ele
2155         * @return IntegerValue
2156         * @throws DomainSpecException
2157         */
2158        private IntegerValue transformIntegerValue(
2159                IntegerType type,
2160                Element ele)
2161                throws DomainSpecException
2162        {
2163                if (!ele.getTagName().equals(INTEGER_VALUE))
2164                        throw new DomainSpecException("Not an IntegerValue element.");
2165                Node textNode = ele.getFirstChild();
2166                if (textNode.getNodeType() != org.w3c.dom.Node.TEXT_NODE)
2167                        throw new DomainSpecException("Wrong elements for StringValue");
2168                IntegerValue value =
2169                        new IntegerValue(textNode.getNodeValue());
2170                // Type checking.
2171                if (type != null && !type.isValidValue(value))
2172                        throw new DomainSpecException(
2173                                illegal_value_for + "IntegerType :" + type.getName());
2174                return value;
2175        }
2176        /**
2177         * Method transformDecimalValue.<br>
2178        Returns a DecimalValue object by parsing element:
2179        <pre>
2180            &lt;DecimalValue&gt;
2181                123
2182            &lt;/DecimalValue&gt;
2183        </pre>
2184         * @param type
2185         * @param ele
2186         * @return DecimalValue
2187         * @throws DomainSpecException
2188         */
2189        private DecimalValue transformDecimalValue(
2190                DecimalType type,
2191                Element ele)
2192                throws DomainSpecException
2193        {
2194                if (!ele.getTagName().equals(DECIMAL_VALUE))
2195                        throw new DomainSpecException("Wrong element");
2196                Node textNode = ele.getFirstChild();
2197                if (textNode.getNodeType() != org.w3c.dom.Node.TEXT_NODE)
2198                        throw new DomainSpecException("Wrong elements for StringValue");
2199                DecimalValue value =
2200                        new DecimalValue(textNode.getNodeValue());
2201                // Type checking.
2202                if (type != null && !type.isValidValue(value))
2203                        throw new DomainSpecException(
2204                                illegal_value_for + "DecimalType :" + type.getName());
2205                return value;
2206        }
2207        /**
2208         * Method transformStringValue.<br>
2209        Returns a StringValue object by parsing element:
2210        <pre>
2211            &lt;StringValue&gt;
2212                123
2213            &lt;/StringValue&gt;
2214        </pre>
2215         * @param type
2216         * @param ele
2217         * @return StringValue
2218         * @throws DomainSpecException
2219         */
2220        private StringValue transformStringValue(
2221                StringType type,
2222                Element ele)
2223                throws DomainSpecException
2224        {
2225                if (!ele.getTagName().equals(STRING_VALUE))
2226                        throw new DomainSpecException("Not a StringValue element");
2227                Node textNode = ele.getFirstChild();
2228                if (textNode.getNodeType() != org.w3c.dom.Node.TEXT_NODE)
2229                        throw new DomainSpecException(
2230                                improper_sub_element_for + "'StringValue'");
2231                StringValue value = new StringValue(textNode.getNodeValue());
2232                // Type checking.
2233                if (type != null && !type.isValidValue(value))
2234                        throw new DomainSpecException(
2235                                illegal_value_for + "StringType :" + type.getName());
2236                return value;
2237        }
2238        /**
2239         * Method transformEnumValue.<br>
2240        Returns an EnumValue object by parsing element:
2241        <pre>
2242            &lt;EnumValue&gt;
2243                123
2244            &lt;/EnumValue&gt;
2245        </pre>
2246         * @param type
2247         * @param ele
2248         * @return EnumValue
2249         * @throws DomainSpecException
2250         */
2251        private EnumValue transformEnumValue(EnumType type, Element ele)
2252                throws DomainSpecException
2253        {
2254                if (!ele.getTagName().equals(ENUM_VALUE))
2255                        throw new DomainSpecException("Not an EnumValue element");
2256                Node textNode = ele.getFirstChild();
2257                if (textNode.getNodeType() != org.w3c.dom.Node.TEXT_NODE)
2258                        throw new DomainSpecException(
2259                                improper_sub_element_for + "'EnumValue'");
2260                EnumValue value =
2261                        new EnumValue(textNode.getNodeValue());
2262                // Type checking.
2263                if (type != null && !type.isValidValue(value))
2264                {
2265                        RTUtil.debugInfo(
2266                                "transformEnumValue() type name = " + type.getName());
2267                        RTUtil.debugInfo(
2268                                "transformEnumValue() value = " + value.toString());
2269                        throw new DomainSpecException(
2270                                illegal_value_for + "EnumType :" + type.getName());
2271                }
2272                return value;
2273        }
2274        /**
2275         * Method transformTreeValue.<br>
2276        Returns a TreeValueSet object by parsing element:
2277        <pre>
2278            &lt;TreeValue includeCurrent="true"
2279                       includeChildren="true"
2280                       includeDescendents="false"&gt;   
2281                cs.stanford.edu
2282            &lt;/TreeValue&gt;
2283        </pre>
2284         * @param type
2285         * @param ele
2286         * @return TreeValueSet
2287         * @throws DomainSpecException
2288         */
2289        private TreeValueSet transformTreeValue(
2290                TreeType type,
2291                Element ele)
2292                throws DomainSpecException
2293        {
2294                if (!ele.getTagName().equals(TREE_VALUE))
2295                        throw new DomainSpecException("Not a TreeValue element");
2296                Node textNode = ele.getFirstChild();
2297                if (textNode.getNodeType() != org.w3c.dom.Node.TEXT_NODE)
2298                        throw new DomainSpecException(
2299                                improper_sub_element_for + "'TreeValue'");
2300                boolean includeCurrent =
2301                        RTUtil.parseBoolean(
2302                                ele.getAttribute(INCLUDE_CURRENT),
2303                                true);
2304                boolean includeChildren =
2305                        RTUtil.parseBoolean(
2306                                ele.getAttribute(INCLUDE_CHILDREN),
2307                                false);
2308                boolean includeDescendents =
2309                        RTUtil.parseBoolean(
2310                                ele.getAttribute(INCLUDE_DESCENDENTS),
2311                                false);
2312                String value = textNode.getNodeValue();
2313                String separator = type.getSeparator();
2314                StringTokenizer st = new StringTokenizer(value, separator);
2315                int size = st.countTokens();
2316                ArrayList values = new ArrayList(size);
2317                while (st.hasMoreTokens())
2318                {
2319                        values.add((String) st.nextToken());
2320                }
2321                TreeValue res = new TreeValue(type, values);
2322                // Type checking.
2323                if (type != null && !type.isValidValue(res))
2324                        throw new DomainSpecException(
2325                                illegal_value_for + "TreeType :" + type.getName());
2326                return new TreeValueSet(
2327                        (TreeType) type,
2328                        res,
2329                        includeCurrent,
2330                        includeChildren,
2331                        includeDescendents);
2332        }
2333        /**
2334         * Method transformTimeValue.<br>
2335        Returns a TimeValue object by parsing element:
2336        <pre>
2337            &lt;TimeValue&gt;
2338                2002-08-29T09:31:32
2339            &lt;/TimeValue&gt;
2340        </pre>
2341         * @param type
2342         * @param ele
2343         * @return TimeValue
2344         * @throws DomainSpecException
2345         */
2346        private TimeValue transformTimeValue(TimeType type, Element ele)
2347                throws DomainSpecException
2348        {
2349                if (!ele.getTagName().equals(TIME_VALUE))
2350                        throw new DomainSpecException("Not a TimeValue element");
2351                Node textNode = ele.getFirstChild();
2352                if (textNode.getNodeType() != org.w3c.dom.Node.TEXT_NODE)
2353                        throw new DomainSpecException(
2354                                improper_sub_element_for + "'TimeValue'");
2355                String dateString = textNode.getNodeValue();
2356                String typeName = type.getName();
2357                TimeValue value =
2358                        new TimeValue(
2359                                typeName,
2360                                transformDateTime(typeName, dateString));
2361                return value;
2362        }
2363        /**
2364         * Method transformDateTime.<br>
2365        Returns a java.uti.Date object from the given XML Schema datetype name
2366        and the dateTime stirng.
2367       
2368        By using Sun XML Datetype Library, we validate dateTime against
2369        the corresonding datetype specified by typeName. DomainSpecExeption is
2370        thrown when validation fails.
2371         * @param typeName
2372         * @param dateTime
2373         * @return Date
2374         * @throws DomainSpecException
2375         */
2376        private Date transformDateTime(String typeName, String dateTime)
2377                throws DomainSpecException
2378        {
2379                com.sun.msv.datatype.xsd.XSDatatype type = null;
2380                try
2381                {
2382                        com.sun.msv.datatype.xsd.DatatypeFactory.getTypeByName(
2383                                typeName);
2384                }
2385                catch (org.relaxng.datatype.DatatypeException e)
2386                {
2387                        throw new DomainSpecException(
2388                                "Cannot recognize type " + typeName);
2389                }
2390                if (!type.isValid(dateTime, null))
2391                        throw new DomainSpecException(
2392                                illegal_value_for + type.getName());
2393                // for datetype, the corresponding Java object is Calendar.
2394                Calendar dtObject =
2395                        (Calendar) type.createJavaObject(dateTime, null);
2396                Class dtClass = type.getJavaObjectType();
2397                RTUtil.debugInfo(
2398                        "paseDateTime()> dtClass = " + dtClass.getName());
2399                return dtObject.getTime();
2400        }
2401        /*
2402        TreeValue parseDateTimeOld(String typeName, String dateTime)
2403            throws DomainSpecException
2404        {
2405       
2406                XSDatatype = DatatypeFactory.getTypeByName(tu   
2407       
2408            string regExp;
2409            String s = dateTime.trim();
2410       
2411            if(typeName.equals("dateTime"))
2412            {
2413                regExp = "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}";
2414                int tIndex = s.indexOf('T');
2415               
2416            }
2417            else if (typeName.equals("date"))
2418            {
2419            }
2420            else if (typeName.equals("time"))
2421            {
2422            }
2423            else if(typeName.equals("gYear"))
2424            {
2425            }
2426            else if(typeName.equals("gYearMonth"))
2427            {
2428            }
2429            else if(typeName.equals("gMonth"))
2430            {
2431            }
2432            else if(typeName.equals("gMonthDay"))
2433            {
2434            }
2435            else if(typeName.equals("gDay"))
2436            {
2437            }
2438           
2439        // Issue: Not sure how to handle the optional time zone part, so ignore it for now.
2440        // The following is an attempt to match the time zone.
2441        // "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(Z|((+|\\0x2D)[0-2][0-9]:[0-2][0-9]))?
2442       
2443            if(! s.matches(regExp))
2444                throw new DomainSpecException("'"+dateTime+
2445                    "' is no in the format of 'CCYY-MM-DDThh:mm:ss'");           
2446            String year = s.substring(0, 4);
2447            if(! RTUtil.validYear(year))
2448                throw new DomainSecException("Invalid year '" + year + "'");
2449            String month = s.substring(5, 8);
2450            if(! RTUtil.validMonth(month))
2451                throw new DomainSpecException("Invalid month '" + month+"'");
2452            String day = s.substring(9, 11);
2453            if(! RTUtil.validDay(year, month, day))
2454                throw new DomainSpecException("Invalid day '" + day +
2455                    "' in month '"+month+"'");
2456            String hour = s.substring(12, 14);
2457            if(! RTUtil.validHour(hour))
2458                throw new DomainSpecException("Invalid hour '"+hour+"'");
2459            String minute = s.substring(15, 17);
2460            if(! RTUtil.validMinSec(minute))
2461                throw new DomainSpecException("Invalid minute '"+minute+"'");
2462            String second = s.substring(18, 20);
2463            if(! RTUtil.validMinSec(second))
2464                throw new DomainSpecException("Invalid second '"+second+"'");
2465            //String timeZone = s.substring(22);
2466           
2467       
2468            Date date = null;
2469            try
2470            {
2471                Calendar calendar = Calendar.getInstance();
2472                calendar.set(Integer.parseInt(year), Integer.parseInt(month),
2473                             Integer.parseInt(day), Integer.parseInt(hour),
2474                             Integer.parseInt(minute), Integer.parseInt(second));
2475                date = calendar.getTime();
2476            }
2477            catch(NumberFormatException e)
2478            {
2479                // Because month, day, hour, minute, second has been validated
2480                // it should never reach here.
2481            }
2482           
2483            return ;
2484        }
2485        */
2486        //        DatatypeValidator validator = getDatatypeValidator("dateTime");
2487        //        try
2488        //        {
2489        //            validator.validate(dateTime, null);
2490        //        }
2491        //        catch(InvalidDatatypeValueException e)
2492        //        {
2493        //           
2494        //        }
2495        //       
2496        //       
2497        //        int year = date[0], month = date[1], day = date[2],
2498        //            hour = date[3], minute = date[4], second = date[5];
2499        //        // Issue: what to do with the time zone?
2500        //       
2501        //        Calendar calendar = Calendar.getInstance();
2502        //        calendar.set(year, month, day, hour, minute, second);
2503        //        return calendar.getTime();
2504
2505        //    private ValidityTime parseValidityTime(Element ele)
2506        //        throws DomainSpecException
2507        //    {
2508        //        if(! ele.getTagName().equals(VALIDITY_TIME))
2509        //            throw new DomainSpecException("Not a ValidityTime element");
2510        //       
2511        //        Element[] elements = getChildElements(ele);
2512        //        if(elements.length != 3)
2513        //            throw new DomainSpecException(
2514        //                improper_sub_element_for + "'ValidityTime'");
2515        //               
2516        //        String issueAt = getFirstChildElementByTagName(ele, ISSUE_AT).getNodeValue();
2517        //        String validFrom = getFirstChildElementByTagName(ele, VALID_FROM).getNodeValue();
2518        //        String validTo = getFirstChildElementByTagName(ele, VALID_TO).getNodeValue();
2519        //       
2520        //        return new ValidityTime(RTUtil.parseDateTime(issueAt),
2521        //                                RTUtil.parseDateTime(validFrom),
2522        //                                RTUtil.parseDateTime(validTo));
2523        //    }
2524
2525} // End of Class RTParser.
Note: See TracBrowser for help on using the repository browser.