View Javadoc

    1   /**
    2    * Copyright (c) 2008-2011, 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
    17  package org.yaml.snakeyaml.emitter;
    18
    19  import java.util.HashMap;
    20  import java.util.List;
    21  import java.util.Map;
    22
    23  import org.yaml.snakeyaml.constructor.AbstractConstruct;
    24  import org.yaml.snakeyaml.constructor.Constructor;
    25  import org.yaml.snakeyaml.events.AliasEvent;
    26  import org.yaml.snakeyaml.events.DocumentEndEvent;
    27  import org.yaml.snakeyaml.events.DocumentStartEvent;
    28  import org.yaml.snakeyaml.events.Event;
    29  import org.yaml.snakeyaml.events.ImplicitTuple;
    30  import org.yaml.snakeyaml.events.MappingEndEvent;
    31  import org.yaml.snakeyaml.events.MappingStartEvent;
    32  import org.yaml.snakeyaml.events.ScalarEvent;
    33  import org.yaml.snakeyaml.events.SequenceEndEvent;
    34  import org.yaml.snakeyaml.events.SequenceStartEvent;
    35  import org.yaml.snakeyaml.events.StreamEndEvent;
    36  import org.yaml.snakeyaml.events.StreamStartEvent;
    37  import org.yaml.snakeyaml.nodes.MappingNode;
    38  import org.yaml.snakeyaml.nodes.Node;
    39  import org.yaml.snakeyaml.nodes.ScalarNode;
    40
    41  public class EventConstructor extends Constructor {
    42
    43      public EventConstructor() {
    44          this.yamlConstructors.put(null, new ConstructEvent());
    45      }
    46
    47      private class ConstructEvent extends AbstractConstruct {
    48
    49          @SuppressWarnings("unchecked")
    50          public Object construct(Node node) {
    51              Map<Object, Object> mapping;
    52              if (node instanceof ScalarNode) {
    53                  mapping = new HashMap<Object, Object>();
    54              } else {
    55                  mapping = constructMapping((MappingNode) node);
    56              }
    57              String className = node.getTag().getValue().substring(1) + "Event";
    58              Event value;
    59              if (className.equals("AliasEvent")) {
    60                  value = new AliasEvent((String) mapping.get("anchor"), null, null);
    61              } else if (className.equals("ScalarEvent")) {
    62                  String tag = (String) mapping.get("tag");
    63                  String v = (String) mapping.get("value");
    64                  if (v == null) {
    65                      v = "";
    66                  }
    67                  List<Boolean> implicitList = (List<Boolean>) mapping.get("implicit");
    68                  ImplicitTuple implicit;
    69                  if (implicitList == null) {
    70                      implicit = new ImplicitTuple(false, true);
    71                  } else {
    72                      implicit = new ImplicitTuple((Boolean) implicitList.get(0),
    73                              (Boolean) implicitList.get(1));
    74                  }
    75                  value = new ScalarEvent((String) mapping.get("anchor"), tag, implicit, v, null,
    76                          null, null);
    77              } else if (className.equals("SequenceStartEvent")) {
    78                  String tag = (String) mapping.get("tag");
    79                  Boolean implicit = (Boolean) mapping.get("implicit");
    80                  if (implicit == null) {
    81                      implicit = true;
    82                  }
    83                  value = new SequenceStartEvent((String) mapping.get("anchor"), tag, implicit, null,
    84                          null, false);
    85              } else if (className.equals("MappingStartEvent")) {
    86                  String tag = (String) mapping.get("tag");
    87                  Boolean implicit = (Boolean) mapping.get("implicit");
    88                  if (implicit == null) {
    89                      implicit = true;
    90                  }
    91                  value = new MappingStartEvent((String) mapping.get("anchor"), tag, implicit, null,
    92                          null, false);
    93              } else if (className.equals("DocumentEndEvent")) {
    94                  value = new DocumentEndEvent(null, null, false);
    95              } else if (className.equals("DocumentStartEvent")) {
    96                  Map<String, String> tags = (Map<String, String>) mapping.get("tags");
    97                  List<Integer> versionList = (List<Integer>) mapping.get("version");
    98                  Integer[] version = null;
    99                  if (versionList != null) {
    100                     version = new Integer[2];
    101                     version[0] = versionList.get(0).intValue();
    102                     version[1] = versionList.get(1).intValue();
    103                 }
    104                 value = new DocumentStartEvent(null, null, false, version, tags);
    105             } else if (className.equals("MappingEndEvent")) {
    106                 value = new MappingEndEvent(null, null);
    107             } else if (className.equals("SequenceEndEvent")) {
    108                 value = new SequenceEndEvent(null, null);
    109             } else if (className.equals("StreamEndEvent")) {
    110                 value = new StreamEndEvent(null, null);
    111             } else if (className.equals("StreamStartEvent")) {
    112                 value = new StreamStartEvent(null, null);
    113             } else {
    114                 throw new UnsupportedOperationException();
    115             }
    116             return value;
    117         }
    118     }
    119 }