Skip to content

Commit 6f97c3b

Browse files
committed
Update UILoader.
1 parent 1663257 commit 6f97c3b

2 files changed

Lines changed: 21 additions & 68 deletions

File tree

sierra-tools/dtd-encoder/src/main/java/org/httprpc/sierra/tools/DTDEncoder.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,12 @@ private String getAttributeType(ConstantAdapter[] values) {
179179

180180
attributeTypeBuilder.append('(');
181181

182-
var i = 0;
183-
184-
for (var value : values) {
182+
for (var i = 0; i < values.length; i++) {
185183
if (i > 0) {
186184
attributeTypeBuilder.append('|');
187185
}
188186

189-
attributeTypeBuilder.append(value.getKey());
190-
191-
i++;
187+
attributeTypeBuilder.append(values[i].getKey());
192188
}
193189

194190
attributeTypeBuilder.append(')');

sierra/src/main/java/org/httprpc/sierra/UILoader.java

Lines changed: 19 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -773,42 +773,14 @@ private void processStartElement(XMLStreamReader xmlStreamReader) {
773773

774774
var propertyType = mutator.getParameterTypes()[0];
775775

776-
Object argument = null;
777-
776+
Object argument;
778777
if (propertyType == Integer.TYPE || propertyType == Integer.class) {
779778
if (name.equals(Attribute.HORIZONTAL_ALIGNMENT.getName())) {
780-
for (var horizontalAlignment : HorizontalAlignment.values()) {
781-
if (value.equals(horizontalAlignment.getKey())) {
782-
argument = horizontalAlignment.getValue();
783-
break;
784-
}
785-
}
786-
787-
if (argument == null) {
788-
throw new IllegalArgumentException("Invalid horizontal alignment.");
789-
}
779+
argument = getValue(value, HorizontalAlignment.values());
790780
} else if (name.equals(Attribute.VERTICAL_ALIGNMENT.getName())) {
791-
for (var verticalAlignment : VerticalAlignment.values()) {
792-
if (value.equals(verticalAlignment.getKey())) {
793-
argument = verticalAlignment.getValue();
794-
break;
795-
}
796-
}
797-
798-
if (argument == null) {
799-
throw new IllegalArgumentException("Invalid vertical alignment.");
800-
}
781+
argument = getValue(value, VerticalAlignment.values());
801782
} else if (name.equals(Attribute.ORIENTATION.getName())) {
802-
for (var orientation : Orientation.values()) {
803-
if (value.equals(orientation.getKey())) {
804-
argument = orientation.getValue();
805-
break;
806-
}
807-
}
808-
809-
if (argument == null) {
810-
throw new IllegalArgumentException("Invalid orientation.");
811-
}
783+
argument = getValue(value, Orientation.values());
812784

813785
if (component instanceof JSplitPane) {
814786
argument = switch ((int)argument) {
@@ -818,38 +790,11 @@ private void processStartElement(XMLStreamReader xmlStreamReader) {
818790
};
819791
}
820792
} else if (name.equals(Attribute.FOCUS_LOST_BEHAVIOR.getName())) {
821-
for (var focusLostBehavior : FocusLostBehavior.values()) {
822-
if (value.equals(focusLostBehavior.getKey())) {
823-
argument = focusLostBehavior.getValue();
824-
break;
825-
}
826-
}
827-
828-
if (argument == null) {
829-
throw new IllegalArgumentException("Invalid focus lost behavior.");
830-
}
793+
argument = getValue(value, FocusLostBehavior.values());
831794
} else if (name.equals(Attribute.TAB_PLACEMENT.getName())) {
832-
for (var tabPlacement : TabPlacement.values()) {
833-
if (value.equals(tabPlacement.getKey())) {
834-
argument = tabPlacement.getValue();
835-
break;
836-
}
837-
}
838-
839-
if (argument == null) {
840-
throw new IllegalArgumentException("Invalid tab placement.");
841-
}
795+
argument = getValue(value, TabPlacement.values());
842796
} else if (name.equals(Attribute.TAB_LAYOUT_POLICY.getName())) {
843-
for (var tabLayoutPolicy : TabLayoutPolicy.values()) {
844-
if (value.equals(tabLayoutPolicy.getKey())) {
845-
argument = tabLayoutPolicy.getValue();
846-
break;
847-
}
848-
}
849-
850-
if (argument == null) {
851-
throw new IllegalArgumentException("Invalid tab layout policy.");
852-
}
797+
argument = getValue(value, TabLayoutPolicy.values());
853798
} else {
854799
argument = Integer.valueOf(value);
855800
}
@@ -913,6 +858,18 @@ private void processStartElement(XMLStreamReader xmlStreamReader) {
913858
components.push(component);
914859
}
915860

861+
private int getValue(String key, ConstantAdapter[] values) {
862+
for (var i = 0; i < values.length; i++) {
863+
var value = values[i];
864+
865+
if (key.equals(value.getKey())) {
866+
return value.getValue();
867+
}
868+
}
869+
870+
throw new IllegalArgumentException("Invalid key.");
871+
}
872+
916873
private String getText(String value) {
917874
if (resourceBundle == null) {
918875
return value;

0 commit comments

Comments
 (0)