View Javadoc

1   /**
2    * Copyright (c) 2008-2012, http://www.snakeyaml.org
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.yaml.snakeyaml.emitter;
17  
18  import java.util.HashMap;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.yaml.snakeyaml.DumperOptions.Version;
23  import org.yaml.snakeyaml.constructor.AbstractConstruct;
24  import org.yaml.snakeyaml.constructor.Constructor;
25  import org.yaml.snakeyaml.error.YAMLException;
26  import org.yaml.snakeyaml.events.AliasEvent;
27  import org.yaml.snakeyaml.events.DocumentEndEvent;
28  import org.yaml.snakeyaml.events.DocumentStartEvent;
29  import org.yaml.snakeyaml.events.Event;
30  import org.yaml.snakeyaml.events.ImplicitTuple;
31  import org.yaml.snakeyaml.events.MappingEndEvent;
32  import org.yaml.snakeyaml.events.MappingStartEvent;
33  import org.yaml.snakeyaml.events.ScalarEvent;
34  import org.yaml.snakeyaml.events.SequenceEndEvent;
35  import org.yaml.snakeyaml.events.SequenceStartEvent;
36  import org.yaml.snakeyaml.events.StreamEndEvent;
37  import org.yaml.snakeyaml.events.StreamStartEvent;
38  import org.yaml.snakeyaml.nodes.MappingNode;
39  import org.yaml.snakeyaml.nodes.Node;
40  import org.yaml.snakeyaml.nodes.ScalarNode;
41  
42  public class EventConstructor extends Constructor {
43  
44      public EventConstructor() {
45          this.yamlConstructors.put(null, new ConstructEvent());
46      }
47  
48      private class ConstructEvent extends AbstractConstruct {
49  
50          @SuppressWarnings("unchecked")
51          public Object construct(Node node) {
52              Map<Object, Object> mapping;
53              if (node instanceof ScalarNode) {
54                  mapping = new HashMap<Object, Object>();
55              } else {
56                  mapping = constructMapping((MappingNode) node);
57              }
58              String className = node.getTag().getValue().substring(1) + "Event";
59              Event value;
60              if (className.equals("AliasEvent")) {
61                  value = new AliasEvent((String) mapping.get("anchor"), null, null);
62              } else if (className.equals("ScalarEvent")) {
63                  String tag = (String) mapping.get("tag");
64                  String v = (String) mapping.get("value");
65                  if (v == null) {
66                      v = "";
67                  }
68                  List<Boolean> implicitList = (List<Boolean>) mapping.get("implicit");
69                  ImplicitTuple implicit;
70                  if (implicitList == null) {
71                      implicit = new ImplicitTuple(false, true);
72                  } else {
73                      implicit = new ImplicitTuple((Boolean) implicitList.get(0),
74                              (Boolean) implicitList.get(1));
75                  }
76                  value = new ScalarEvent((String) mapping.get("anchor"), tag, implicit, v, null,
77                          null, null);
78              } else if (className.equals("SequenceStartEvent")) {
79                  String tag = (String) mapping.get("tag");
80                  Boolean implicit = (Boolean) mapping.get("implicit");
81                  if (implicit == null) {
82                      implicit = true;
83                  }
84                  value = new SequenceStartEvent((String) mapping.get("anchor"), tag, implicit, null,
85                          null, false);
86              } else if (className.equals("MappingStartEvent")) {
87                  String tag = (String) mapping.get("tag");
88                  Boolean implicit = (Boolean) mapping.get("implicit");
89                  if (implicit == null) {
90                      implicit = true;
91                  }
92                  value = new MappingStartEvent((String) mapping.get("anchor"), tag, implicit, null,
93                          null, false);
94              } else if (className.equals("DocumentEndEvent")) {
95                  value = new DocumentEndEvent(null, null, false);
96              } else if (className.equals("DocumentStartEvent")) {
97                  Map<String, String> tags = (Map<String, String>) mapping.get("tags");
98                  List<Integer> versionList = (List<Integer>) mapping.get("version");
99                  Version version = null;
100                 // TODO ???
101                 if (versionList != null) {
102                     Integer major = versionList.get(0).intValue();
103                     if (major != 1) {
104                         throw new YAMLException("Unsupported version.");
105                     }
106                     Integer minor = versionList.get(1).intValue();
107                     if (minor == 0) {
108                         version = Version.V1_0;
109                     } else {
110                         version = Version.V1_1;
111                     }
112                 }
113                 value = new DocumentStartEvent(null, null, false, version, tags);
114             } else if (className.equals("MappingEndEvent")) {
115                 value = new MappingEndEvent(null, null);
116             } else if (className.equals("SequenceEndEvent")) {
117                 value = new SequenceEndEvent(null, null);
118             } else if (className.equals("StreamEndEvent")) {
119                 value = new StreamEndEvent(null, null);
120             } else if (className.equals("StreamStartEvent")) {
121                 value = new StreamStartEvent(null, null);
122             } else {
123                 throw new UnsupportedOperationException();
124             }
125             return value;
126         }
127     }
128 }