Examples of ScalarEvent


Examples of com.esotericsoftware.yamlbeans.parser.ScalarEvent

    IOException, YamlException {
    boolean isRoot = this.isRoot;
    this.isRoot = false;

    if (object == null) {
      emitter.emit(new ScalarEvent(null, null, new boolean[] {true, true}, null, (char)0));
      return;
    }

    Class valueClass = object.getClass();
    boolean unknownType = fieldClass == null;
    if (unknownType) fieldClass = valueClass;

    if (Beans.isScalar(valueClass)) {
      emitter.emit(new ScalarEvent(null, null, new boolean[] {true, true}, String.valueOf(object), (char)0));
      return;
    }

    if (object instanceof Enum) {
      emitter.emit(new ScalarEvent(null, null, new boolean[] {true, true}, ((Enum)object).name(), (char)0));
      return;
    }

    String anchor = null;
    if (config.writeConfig.autoAnchor) {
      Integer count = referenceCount.get(object);
      if (count == null) {
        emitter.emit(new AliasEvent(anchoredObjects.get(object)));
        return;
      }
      if (count > 1) {
        referenceCount.remove(object);
        anchor = String.valueOf(nextAnchor++);
        anchoredObjects.put(object, anchor);
      }
    }

    String tag = null;
    boolean showTag = false;
    if (unknownType || valueClass != fieldClass || config.writeConfig.alwaysWriteClassName) {
      showTag = true;
      if ((unknownType || fieldClass == List.class) && valueClass == ArrayList.class) showTag = false;
      if ((unknownType || fieldClass == Map.class) && valueClass == HashMap.class) showTag = false;
      if (fieldClass == Set.class && valueClass == HashSet.class) showTag = false;
      if (valueClass == defaultType) showTag = false;
      if (showTag) {
        tag = config.classNameToTag.get(valueClass.getName());
        if (tag == null) tag = valueClass.getName();
      }
    }

    for (Entry<Class, ScalarSerializer> entry : config.scalarSerializers.entrySet()) {
      if (entry.getKey().isAssignableFrom(valueClass)) {
        ScalarSerializer serializer = entry.getValue();
        emitter.emit(new ScalarEvent(null, tag, new boolean[] {tag == null, tag == null}, serializer.write(object), (char)0));
        return;
      }
    }

    if (object instanceof Collection) {
      emitter.emit(new SequenceStartEvent(anchor, tag, !showTag, false));
      for (Object item : (Collection)object) {
        if (isRoot && !config.writeConfig.writeRootElementTags) elementType = item.getClass();
        writeValue(item, elementType, null, null);
      }
      emitter.emit(Event.SEQUENCE_END);
      return;
    }

    if (object instanceof Map) {
      emitter.emit(new MappingStartEvent(anchor, tag, !showTag, false));
      for (Object item : ((Map)object).entrySet()) {
        Entry entry = (Entry)item;
        writeValue(entry.getKey(), null, null, null);
        if (isRoot && !config.writeConfig.writeRootElementTags) elementType = entry.getValue().getClass();
        writeValue(entry.getValue(), elementType, null, null);
      }
      emitter.emit(Event.MAPPING_END);
      return;
    }

    if (fieldClass.isArray()) {
      elementType = fieldClass.getComponentType();
      emitter.emit(new SequenceStartEvent(anchor, null, true, false));
      for (int i = 0, n = Array.getLength(object); i < n; i++)
        writeValue(Array.get(object, i), elementType, null, null);
      emitter.emit(Event.SEQUENCE_END);
      return;
    }

    // Value must be a bean.

    Object prototype = null;
    if (!config.writeConfig.writeDefaultValues && valueClass != Class.class) {
      prototype = defaultValuePrototypes.get(valueClass);
      if (prototype == null && Beans.getDeferredConstruction(valueClass, config) == null) {
        try {
          prototype = Beans.createObject(valueClass, config.privateConstructors);
        } catch (InvocationTargetException ex) {
          throw new YamlException("Error creating object prototype to determine default values.", ex);
        }
        defaultValuePrototypes.put(valueClass, prototype);
      }
    }

    Set<Property> properties;
    try {
      properties = Beans.getProperties(valueClass, config.beanProperties, config.privateFields, config);
    } catch (IntrospectionException ex) {
      throw new YamlException("Error inspecting class: " + valueClass.getName(), ex);
    }
    emitter.emit(new MappingStartEvent(anchor, tag, !showTag, false));
    for (Property property : properties) {
      try {
        Object propertyValue = property.get(object);
        if (prototype != null) {
          // Don't output properties that have the default value for the prototype.
          Object prototypeValue = property.get(prototype);
          if (propertyValue == null && prototypeValue == null) continue;
          if (propertyValue != null && prototypeValue != null && prototypeValue.equals(propertyValue)) continue;
        }
        emitter.emit(new ScalarEvent(null, null, new boolean[] {true, true}, property.getName(), (char)0));
        Class propertyElementType = config.propertyToElementType.get(property);
        Class propertyDefaultType = config.propertyToDefaultType.get(property);
        writeValue(propertyValue, property.getType(), propertyElementType, propertyDefaultType);
      } catch (Exception ex) {
        throw new YamlException("Error getting property '" + property + "' on class: " + valueClass.getName(), ex);
View Full Code Here

Examples of com.esotericsoftware.yamlbeans.parser.ScalarEvent

  }

  private void processTag () throws IOException {
    String tag = null;
    if (event.type == SCALAR) {
      ScalarEvent ev = (ScalarEvent)event;
      tag = ev.tag;
      if (style == 0) style = chooseScalarStyle();
      if ((!config.canonical || tag == null) && ((0 == style && ev.implicit[0]) || (0 != style && ev.implicit[1]))) {
        preparedTag = null;
        return;
View Full Code Here

Examples of com.esotericsoftware.yamlbeans.parser.ScalarEvent

    if (preparedTag != null && !"".equals(preparedTag)) writer.writeIndicator(preparedTag, true, false, false);
    preparedTag = null;
  }

  private char chooseScalarStyle () {
    ScalarEvent ev = (ScalarEvent)event;
    if (analysis == null) analysis = ScalarAnalysis.analyze(ev.value, config.escapeUnicode);
    if (ev.style == '"' || config.canonical) return '"';
    if (ev.style == 0 && !(simpleKeyContext && (analysis.empty || analysis.multiline))
      && (flowLevel != 0 && analysis.allowFlowPlain || flowLevel == 0 && analysis.allowBlockPlain)) return 0;
    if (ev.style == 0 && ev.implicit[0] && !(simpleKeyContext && (analysis.empty || analysis.multiline))
View Full Code Here

Examples of com.esotericsoftware.yamlbeans.parser.ScalarEvent

    if (ev.style == 0 && analysis.multiline && flowLevel == 0 && analysis.allowBlock) return '|';
    return '"';
  }

  private void processScalar () throws IOException {
    ScalarEvent ev = (ScalarEvent)event;
    if (analysis == null) analysis = ScalarAnalysis.analyze(ev.value, config.escapeUnicode);
    if (style == 0) style = chooseScalarStyle();
    boolean split = !simpleKeyContext;
    if (style == '"')
      writer.writeDoubleQuoted(analysis.scalar, split, indent, config.wrapColumn, config.escapeUnicode);
View Full Code Here

Examples of com.esotericsoftware.yamlbeans.parser.ScalarEvent

    IOException, YamlException {
    boolean isRoot = this.isRoot;
    this.isRoot = false;

    if (object == null) {
      emitter.emit(new ScalarEvent(null, null, new boolean[] {true, true}, null, (char)0));
      return;
    }

    Class valueClass = object.getClass();
    boolean unknownType = fieldClass == null;
    if (unknownType) fieldClass = valueClass;

    if (object instanceof Enum) {
      emitter.emit(new ScalarEvent(null, null, new boolean[] {true, true}, ((Enum)object).name(), (char)0));
      return;
    }

    String anchor = null;
    if (!Beans.isScalar(valueClass)) {
      anchor = anchoredObjects.get(object);
      if (config.writeConfig.autoAnchor) {
        Integer count = referenceCount.get(object);
        if (count == null) {
          emitter.emit(new AliasEvent(anchoredObjects.get(object)));
          return;
        }
        if (count > 1) {
          referenceCount.remove(object);
          if (anchor == null) {
            anchor = String.valueOf(nextAnchor++);
            anchoredObjects.put(object, anchor);
          }
        }
      }
    }

    String tag = null;
    boolean showTag = false;
    if (unknownType || valueClass != fieldClass || config.writeConfig.alwaysWriteClassName) {
      showTag = true;
      if ((unknownType || fieldClass == List.class) && valueClass == ArrayList.class) showTag = false;
      if ((unknownType || fieldClass == Map.class) && valueClass == HashMap.class) showTag = false;
      if (fieldClass == Set.class && valueClass == HashSet.class) showTag = false;
      if (valueClass == defaultType) showTag = false;
      if (showTag) {
        tag = config.classNameToTag.get(valueClass.getName());
        if (tag == null) tag = valueClass.getName();
      }
    }

    for (Entry<Class, ScalarSerializer> entry : config.scalarSerializers.entrySet()) {
      if (entry.getKey().isAssignableFrom(valueClass)) {
        ScalarSerializer serializer = entry.getValue();
        emitter.emit(new ScalarEvent(null, tag, new boolean[] {tag == null, tag == null}, serializer.write(object), (char)0));
        return;
      }
    }

    if (Beans.isScalar(valueClass)) {
      emitter.emit(new ScalarEvent(null, null, new boolean[] {true, true}, String.valueOf(object), (char)0));
      return;
    }

    if (object instanceof Collection) {
      emitter.emit(new SequenceStartEvent(anchor, tag, !showTag, false));
      for (Object item : (Collection)object) {
        if (isRoot && !config.writeConfig.writeRootElementTags) elementType = item.getClass();
        writeValue(item, elementType, null, null);
      }
      emitter.emit(Event.SEQUENCE_END);
      return;
    }

    if (object instanceof Map) {
      emitter.emit(new MappingStartEvent(anchor, tag, !showTag, false));
      for (Object item : ((Map)object).entrySet()) {
        Entry entry = (Entry)item;
        writeValue(entry.getKey(), null, null, null);
        if (isRoot && !config.writeConfig.writeRootElementTags) elementType = entry.getValue().getClass();
        writeValue(entry.getValue(), elementType, null, null);
      }
      emitter.emit(Event.MAPPING_END);
      return;
    }

    if (fieldClass.isArray()) {
      elementType = fieldClass.getComponentType();
      emitter.emit(new SequenceStartEvent(anchor, null, true, false));
      for (int i = 0, n = Array.getLength(object); i < n; i++)
        writeValue(Array.get(object, i), elementType, null, null);
      emitter.emit(Event.SEQUENCE_END);
      return;
    }

    // Value must be a bean.

    Object prototype = null;
    if (!config.writeConfig.writeDefaultValues && valueClass != Class.class) {
      prototype = defaultValuePrototypes.get(valueClass);
      if (prototype == null && Beans.getDeferredConstruction(valueClass, config) == null) {
        try {
          prototype = Beans.createObject(valueClass, config.privateConstructors);
        } catch (InvocationTargetException ex) {
          throw new YamlException("Error creating object prototype to determine default values.", ex);
        }
        defaultValuePrototypes.put(valueClass, prototype);
      }
    }

    Set<Property> properties;
    try {
      properties = Beans.getProperties(valueClass, config.beanProperties, config.privateFields, config);
    } catch (IntrospectionException ex) {
      throw new YamlException("Error inspecting class: " + valueClass.getName(), ex);
    }
    emitter.emit(new MappingStartEvent(anchor, tag, !showTag, false));
    for (Property property : properties) {
      try {
        Object propertyValue = property.get(object);
        if (prototype != null) {
          // Don't output properties that have the default value for the prototype.
          Object prototypeValue = property.get(prototype);
          if (propertyValue == null && prototypeValue == null) continue;
          if (propertyValue != null && prototypeValue != null && prototypeValue.equals(propertyValue)) continue;
        }
        emitter.emit(new ScalarEvent(null, null, new boolean[] {true, true}, property.getName(), (char)0));
        Class propertyElementType = config.propertyToElementType.get(property);
        Class propertyDefaultType = config.propertyToDefaultType.get(property);
        writeValue(propertyValue, property.getType(), propertyElementType, propertyDefaultType);
      } catch (Exception ex) {
        throw new YamlException("Error getting property '" + property + "' on class: " + valueClass.getName(), ex);
View Full Code Here

Examples of com.esotericsoftware.yamlbeans.parser.ScalarEvent

  }

  private void processTag () throws IOException {
    String tag = null;
    if (event.type == SCALAR) {
      ScalarEvent ev = (ScalarEvent)event;
      tag = ev.tag;
      if (style == 0) style = chooseScalarStyle();
      if ((!config.canonical || tag == null) && ((0 == style && ev.implicit[0]) || (0 != style && ev.implicit[1]))) {
        preparedTag = null;
        return;
View Full Code Here

Examples of com.esotericsoftware.yamlbeans.parser.ScalarEvent

    if (preparedTag != null && !"".equals(preparedTag)) writer.writeIndicator(preparedTag, true, false, false);
    preparedTag = null;
  }

  private char chooseScalarStyle () {
    ScalarEvent ev = (ScalarEvent)event;
    if (analysis == null) analysis = ScalarAnalysis.analyze(ev.value, config.escapeUnicode);
    if (ev.style == '"' || config.canonical) return '"';
    if (ev.style == 0 && !(simpleKeyContext && (analysis.empty || analysis.multiline))
      && (flowLevel != 0 && analysis.allowFlowPlain || flowLevel == 0 && analysis.allowBlockPlain)) return 0;
    if (ev.style == 0 && ev.implicit[0] && !(simpleKeyContext && (analysis.empty || analysis.multiline))
View Full Code Here

Examples of com.esotericsoftware.yamlbeans.parser.ScalarEvent

    if (ev.style == 0 && analysis.multiline && flowLevel == 0 && analysis.allowBlock) return '|';
    return '"';
  }

  private void processScalar () throws IOException {
    ScalarEvent ev = (ScalarEvent)event;
    if (analysis == null) analysis = ScalarAnalysis.analyze(ev.value, config.escapeUnicode);
    if (style == 0) style = chooseScalarStyle();
    boolean split = !simpleKeyContext;
    if (style == '"')
      writer.writeDoubleQuoted(analysis.scalar, split, indent, config.wrapColumn, config.escapeUnicode);
View Full Code Here

Examples of org.yaml.snakeyaml.events.ScalarEvent

        IRubyObject tag = args[2];
        IRubyObject plain = args[3];
        IRubyObject quoted = args[4];
        IRubyObject style = args[5];

        ScalarEvent event = new ScalarEvent(
                anchor.isNil() ? null : anchor.asJavaString(),
                tag.isNil() ? null : tag.asJavaString(),
                new ImplicitTuple(plain.isTrue(),
                quoted.isTrue()),
                value.asJavaString(),
View Full Code Here

Examples of org.yaml.snakeyaml.events.ScalarEvent

                            context,
                            handler,
                            "alias",
                            alias);
                } else if (event.is(ID.Scalar)) {
                    ScalarEvent se = (ScalarEvent)event;
                    IRubyObject anchor = se.getAnchor() == null ?
                        runtime.getNil() :
                        RubyString.newString(runtime, se.getAnchor());
                    IRubyObject tag = se.getTag() == null ?
                        runtime.getNil() :
                        RubyString.newString(runtime, se.getTag());
                    IRubyObject plain_implicit = runtime.newBoolean(se.getImplicit().isFirst());
                    IRubyObject quoted_implicit = runtime.newBoolean(se.getImplicit().isSecond());
                    IRubyObject style = runtime.newFixnum(se.getStyle());
                    IRubyObject val = RubyString.newString(runtime, se.getValue());

                    invoke(
                            context,
                            handler,
                            "scalar",
                            val,
                            anchor,
                            tag,
                            plain_implicit,
                            quoted_implicit,
                            style);
                } else if (event.is(ID.SequenceStart)) {
                    SequenceStartEvent sse = (SequenceStartEvent)event;
                    IRubyObject anchor = sse.getAnchor() == null ?
                        runtime.getNil() :
                        RubyString.newString(runtime, sse.getAnchor());
                    IRubyObject tag = sse.getTag() == null ?
                        runtime.getNil() :
                        RubyString.newString(runtime, sse.getTag());
                    IRubyObject implicit = runtime.newBoolean(sse.getImplicit());
                    IRubyObject style = runtime.newFixnum(sse.getFlowStyle() ? 1 : 0);

                    invoke(
                            context,
                            handler,
                            "start_sequence",
                            anchor,
                            tag,
                            implicit,
                            style);
                } else if (event.is(ID.SequenceEnd)) {
                    invoke(
                            context,
                            handler,
                            "end_sequence");
                } else if (event.is(ID.MappingStart)) {
                    MappingStartEvent mse = (MappingStartEvent)event;
                    IRubyObject anchor = mse.getAnchor() == null ?
                        runtime.getNil() :
                        RubyString.newString(runtime, mse.getAnchor());
                    IRubyObject tag = mse.getTag() == null ?
                        runtime.getNil() :
                        RubyString.newString(runtime, mse.getTag());
                    IRubyObject implicit = runtime.newBoolean(mse.getImplicit());
                    IRubyObject style = runtime.newFixnum(mse.getFlowStyle() ? 1 : 0);

                    invoke(
                            context,
                            handler,
                            "start_mapping",
                            anchor,
                            tag,
                            implicit,
                            style);
                } else if (event.is(ID.MappingEnd)) {
                    invoke(
                            context,
                            handler,
                            "end_mapping");
                } else if (event.is(ID.StreamEnd)) {
                    invoke(
                            context,
                            handler,
                            "end_stream");
                    break;
                }
            } catch (ParserException pe) {
                parser = null;
                RubyKernel.raise(context, runtime.getKernel(),
                    new IRubyObject[] {runtime.getModule("Psych").getConstant("SyntaxError"), runtime.newString(pe.getLocalizedMessage())},
                    Block.NULL_BLOCK);
            } catch (ScannerException se) {
                parser = null;
                StringBuilder message = new StringBuilder("syntax error");
                if (se.getProblemMark() != null) {
                    message.append(se.getProblemMark().toString());
                }
                throw runtime.newArgumentError(message.toString());
            }
        }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.