@@ -118,11 +118,11 @@ private KMIPMessage parseMessage()
118118 {
119119 case "RequestHeader" :
120120 isRequest = true ;
121- header = parseHeader (isRequest );
121+ header = parseHeader (true );
122122 break ;
123123 case "ResponseHeader" :
124124 isRequest = false ;
125- header = parseHeader (isRequest );
125+ header = parseHeader (false );
126126 break ;
127127 case "BatchItem" :
128128 KMIPBatchItem batchItem = parseBatchItem (isRequest );
@@ -176,7 +176,13 @@ private KMIPHeader parseHeader(boolean isRequest)
176176 switch (name )
177177 {
178178 case "ProtocolVersion" :
179- protocolVersion = parseProtocolVersion ();
179+ int marjorVersion , minorVersion ;
180+ startElement = assertStartElement ("ProtocolVersionMajor" );
181+ marjorVersion = parseInteger (startElement , "ProtocolVersionMajor" );
182+ startElement = assertStartElement ("ProtocolVersionMinor" );
183+ minorVersion = parseInteger (startElement , "ProtocolVersionMinor" );
184+ assertEndElement (eventReader .nextEvent (), "ProtocolVersion" );
185+ protocolVersion = new KMIPProtocolVersion (marjorVersion , minorVersion );
180186 break ;
181187 case "ClientCorrelationValue" :
182188 clientCorrelationValue = parseTextString (startElement , "ClientCorrelationValue" );
@@ -339,7 +345,7 @@ private KMIPPayload parsePayload(KMIPOperation operation, boolean isRequest)
339345 attributes = parseAttributes ();
340346 break ;
341347 case "UniqueIdentifier" :
342- uniqueIdentifier = parseUniqueIdentifier (startElement , "Error in parsing Unique Identifier " );
348+ uniqueIdentifier = parseUniqueIdentifier (startElement , "UniqueIdentifier " );
343349 uniqueIdentifiers .add (uniqueIdentifier );
344350 break ;
345351 case "SplitKeyParts" :
@@ -486,9 +492,8 @@ private Map<String, Object> parseAttributes()
486492 attributes .put ("CryptographicLength" , cryptographicLength );
487493 break ;
488494 case "CryptographicUsageMask" :
489- int cryptographicUsageMask = parseInteger (startElement , KMIPCryptographicUsageMask .class , "Error in parsing CryptographicUsageMask: " );
495+ int cryptographicUsageMask = parseInteger (startElement , KMIPCryptographicUsageMask .class , "CryptographicUsageMask" );
490496 attributes .put ("CryptographicUsageMask" , cryptographicUsageMask );
491- assertEndElement (event , "CryptographicUsageMask" );
492497 break ;
493498 case "Name" :
494499 startElement = assertStartElement ("NameValue" );
@@ -553,8 +558,7 @@ private KMIPKeyBlock parseKeyBlock()
553558 break ;
554559 case "KeyValue" :
555560 startElement = assertStartElement ("KeyMaterial" );
556- keyValue = parseByteString (startElement , "Error in parsing KeyMaterial: " );
557- assertEndElement (event , "KeyMaterial" );
561+ keyValue = parseByteString (startElement , "KeyMaterial" );
558562 assertEndElement (event , "KeyValue" );
559563 break ;
560564 case "CryptographicAlgorithm" :
@@ -582,10 +586,10 @@ private KMIPKeyBlock parseKeyBlock()
582586 return null ;
583587 }
584588
585- private static void assertException (Attribute attribute , String name , String value , String errorMessage )
589+ private static void assertException (Attribute attribute , String value , String errorMessage )
586590 throws KMIPInputException
587591 {
588- if (!attribute .getName ().getLocalPart ().equals (name ) || !attribute .getValue ().equals (value ))
592+ if (!attribute .getName ().getLocalPart ().equals ("type" ) || !attribute .getValue ().equals (value ))
589593 {
590594 //parse error
591595 throw new KMIPInputException (errorMessage );
@@ -640,109 +644,57 @@ private void assertEndElement(XMLEvent event, String name)
640644 throw new KMIPInputException ("Error in parsing" + name + ": Expected End Element for " + name );
641645 }
642646
643- private int parseInteger (StartElement startElement , String className )
647+ private String parseElementAttribute (StartElement startElement , String className , String type )
644648 throws KMIPInputException , XMLStreamException
645649 {
646- int value ;
650+ String value ;
647651 if (startElement .getAttributes () != null )
648652 {
649653 Iterator it = startElement .getAttributes ();
650654 Attribute attribute = (Attribute )it .next ();
651- assertException (attribute , " type" , "Integer" , " Error in parsing " + className + ": type should be integer" );
655+ assertException (attribute , type , "Error in parsing " + className + ": type should be " + type );
652656 attribute = (Attribute )it .next ();
653- value = Integer . parseInt ( attribute .getValue () );
657+ value = attribute .getValue ();
654658 assertException (it .hasNext (), "Error in parsing " + className + ": There should be 2 attributes" );
655659 assertEndElement (eventReader .nextEvent (), className );
656660 }
657661 else
658662 {
659663 throw new KMIPInputException ("Error in parsing " + className + ": there should be 2 attributes" );
660664 }
661-
662665 return value ;
663666 }
664667
668+ private int parseInteger (StartElement startElement , String className )
669+ throws KMIPInputException , XMLStreamException
670+ {
671+ return Integer .parseInt (parseElementAttribute (startElement , className , "Integer" ));
672+ }
673+
665674 private String parseTextString (StartElement startElement , String className )
666675 throws KMIPInputException , XMLStreamException
667676 {
668- String value ;
669- if (startElement .getAttributes () != null )
670- {
671- Iterator it = startElement .getAttributes ();
672- Attribute attribute = (Attribute )it .next ();
673- assertException (attribute , "type" , "TextString" , "Error in parsing " + className + ": type should be text string" );
674- attribute = (Attribute )it .next ();
675- value = attribute .getValue ();
676- assertException (it .hasNext (), "Error in parsing " + className + ": There should be 2 attributes" );
677- assertEndElement (eventReader .nextEvent (), className );
678- }
679- else
680- {
681- throw new KMIPInputException ("Error in parsing " + className + ": there should be 2 attributes" );
682- }
683- return value ;
677+ return parseElementAttribute (startElement , className , "TextString" );
684678 }
685679
686- private byte [] parseByteString (StartElement startElement , String errorMessage )
687- throws KMIPInputException
680+ private byte [] parseByteString (StartElement startElement , String className )
681+ throws KMIPInputException , XMLStreamException
688682 {
689- String value ;
690- if (startElement .getAttributes () != null )
691- {
692- Iterator it = startElement .getAttributes ();
693- Attribute attribute = (Attribute )it .next ();
694- assertException (attribute , "type" , "ByteString" , errorMessage + " type should be text string" );
695- attribute = (Attribute )it .next ();
696- value = attribute .getValue ();
697- assertException (it .hasNext (), errorMessage + "There should be 2 attributes" );
698- }
699- else
700- {
701- throw new KMIPInputException (errorMessage + " there should be 2 attributes" );
702- }
703- return Hex .decode (value );
683+ return Hex .decode (parseElementAttribute (startElement , className , "ByteString" ));
704684 }
705685
706- private KMIPUniqueIdentifier parseUniqueIdentifier (StartElement startElement , String errorMessage )
686+ private KMIPUniqueIdentifier parseUniqueIdentifier (StartElement startElement , String className )
707687 throws KMIPInputException , XMLStreamException
708688 {
709689 //TODO: parse integer, enumeration
710- String value ;
711- if (startElement .getAttributes () != null )
712- {
713- Iterator it = startElement .getAttributes ();
714- Attribute attribute = (Attribute )it .next ();
715- assertException (attribute , "type" , "TextString" , errorMessage + " type should be text string" );
716- attribute = (Attribute )it .next ();
717- value = attribute .getValue ();
718- assertException (it .hasNext (), errorMessage + "There should be 2 attributes" );
719- }
720- else
721- {
722- throw new KMIPInputException (errorMessage + " there should be 2 attributes" );
723- }
724- assertEndElement (eventReader .nextEvent (), "UniqueIdentifier" );
690+ String value = parseElementAttribute (startElement , className , "TextString" );
725691 return new KMIPUniqueIdentifier (value );
726692 }
727693
728694 private Date parseDateTime (StartElement startElement , String className )
729695 throws KMIPInputException , XMLStreamException
730696 {
731- String value ;
732- if (startElement .getAttributes () != null )
733- {
734- Iterator it = startElement .getAttributes ();
735- Attribute attribute = (Attribute )it .next ();
736- assertException (attribute , "type" , "DateTime" , "Error in parsing " + className + ": type should be DateTime" );
737- attribute = (Attribute )it .next ();
738- value = attribute .getValue ();
739- assertException (it .hasNext (), "Error in parsing " + className + ":There should be 2 attributes" );
740- assertEndElement (eventReader .nextEvent (), className );
741- }
742- else
743- {
744- throw new KMIPInputException ("Error in parsing " + className + ": there should be 2 attributes" );
745- }
697+ String value = parseElementAttribute (startElement , className , "DateTime" );
746698 if (value .equals ("$NOW" ))
747699 {
748700 return new Date ();
@@ -753,21 +705,7 @@ private Date parseDateTime(StartElement startElement, String className)
753705 public <T extends Enum <T > & KMIPEnumeration > T parseEnum (StartElement startElement , Class <T > enumClass , String className )
754706 throws KMIPInputException , XMLStreamException
755707 {
756- String value ;
757- if (startElement .getAttributes () != null )
758- {
759- Iterator it = startElement .getAttributes ();
760- Attribute attribute = (Attribute )it .next ();
761- assertException (attribute , "type" , "Enumeration" , "Error in parsing " + className + ": type should be enumeration" );
762- attribute = (Attribute )it .next ();
763- value = attribute .getValue ();
764- assertException (it .hasNext (), "Error in parsing " + className + ":There should be 2 attributes" );
765- assertEndElement (eventReader .nextEvent (), className );
766- }
767- else
768- {
769- throw new KMIPInputException ("Error in parsing " + className + ": there should be 2 attributes" );
770- }
708+ String value = parseElementAttribute (startElement , className , "Enumeration" );
771709 // Convert value string to corresponding enum constant
772710 try
773711 {
@@ -779,57 +717,23 @@ public <T extends Enum<T> & KMIPEnumeration> T parseEnum(StartElement startEleme
779717 }
780718 }
781719
782- private static <T extends Enum <T > & KMIPEnumeration > int parseInteger (StartElement startElement , Class <T > enumClass , String errorMessage )
783- throws KMIPInputException
720+ private <T extends Enum <T > & KMIPEnumeration > int parseInteger (StartElement startElement , Class <T > enumClass , String className )
721+ throws KMIPInputException , XMLStreamException
784722 {
785723 int result = 0 ;
786- if (startElement .getAttributes () != null )
724+ String value = parseElementAttribute (startElement , className , "Integer" );
725+ try
787726 {
788- Iterator it = startElement .getAttributes ();
789- Attribute attribute = (Attribute )it .next ();
790- assertException (attribute , "type" , "Integer" , errorMessage + " type should be Integer" );
791- attribute = (Attribute )it .next ();
792- String [] values = attribute .getValue ().split (" " );
793- try
727+ for (String v : value .split (" " ))
794728 {
795- for (String value : values )
796- {
797- T enumConstant = Enum .valueOf (enumClass , value .replace ("-" , "_" ));
798- result |= enumConstant .getValue ();
799- }
729+ T enumConstant = Enum .valueOf (enumClass , v .replace ("-" , "_" ));
730+ result |= enumConstant .getValue ();
800731 }
801- catch (IllegalArgumentException e )
802- {
803- throw new KMIPInputException (errorMessage + " Invalid value for enum: " + attribute .getValue ());
804- }
805- assertException (it .hasNext (), errorMessage + "There should be 2 attributes" );
806732 }
807- else
733+ catch ( IllegalArgumentException e )
808734 {
809- throw new KMIPInputException (errorMessage + " there should be 2 attributes" );
735+ throw new KMIPInputException (" Invalid value for enum: " + value );
810736 }
811737 return result ;
812738 }
813-
814- public KMIPProtocolVersion parseProtocolVersion ()
815- throws KMIPInputException
816- {
817- int marjorVersion , minorVersion ;
818- try
819- {
820- XMLEvent event = eventReader .nextEvent ();
821- StartElement startElement = assertStartElement ("ProtocolVersionMajor" );
822- marjorVersion = parseInteger (startElement , "ProtocolVersionMajor" );
823-
824- startElement = assertStartElement ("ProtocolVersionMinor" );
825- minorVersion = parseInteger (startElement , "ProtocolVersionMinor" );
826-
827- assertEndElement (event , "ProtocolVersion" );
828- return new KMIPProtocolVersion (marjorVersion , minorVersion );
829- }
830- catch (XMLStreamException e )
831- {
832- throw new KMIPInputException ("Error processing XML: " + e .getMessage ());
833- }
834- }
835739}
0 commit comments