Beispiel #1
0
    /*
     * (non-Javadoc)
     *
     * @see
     * com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object
     * , com.thoughtworks.xstream.io.HierarchicalStreamWriter,
     * com.thoughtworks.xstream.converters.MarshallingContext)
     */
    @Override
    public void marshal(
        Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
      DataCenterInfo info = (DataCenterInfo) source;

      writer.startNode(ELEM_NAME);
      // For backward compat. for now
      writer.setValue(info.getName().name());
      writer.endNode();

      if (info.getName() == Name.Amazon) {
        AmazonInfo aInfo = (AmazonInfo) info;
        writer.startNode(NODE_METADATA);
        // for backward compat. for now
        if (aInfo.getMetadata().size() == 0) {
          writer.addAttribute("class", "java.util.Collections$EmptyMap");
        }
        context.convertAnother(aInfo.getMetadata());
        writer.endNode();
      }
    }
Beispiel #2
0
    /*
     * (non-Javadoc)
     *
     * @see
     * com.thoughtworks.xstream.converters.Converter#unmarshal(com.thoughtworks
     * .xstream.io.HierarchicalStreamReader,
     * com.thoughtworks.xstream.converters.UnmarshallingContext)
     */
    @Override
    @SuppressWarnings("unchecked")
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
      DataCenterInfo info = null;
      while (reader.hasMoreChildren()) {
        reader.moveDown();

        if (ELEM_NAME.equals(reader.getNodeName())) {
          final String dataCenterName = reader.getValue();
          if (DataCenterInfo.Name.Amazon.name().equalsIgnoreCase(dataCenterName)) {
            info = new AmazonInfo();
          } else {
            final DataCenterInfo.Name name = DataCenterInfo.Name.valueOf(dataCenterName);
            info =
                new DataCenterInfo() {

                  @Override
                  public Name getName() {
                    return name;
                  }
                };
          }
        } else if (NODE_METADATA.equals(reader.getNodeName())) {
          if (info.getName() == Name.Amazon) {
            Map<String, String> metadataMap =
                (Map<String, String>) context.convertAnother(info, Map.class);
            Map<String, String> metadataMapInter = new HashMap<String, String>(metadataMap.size());
            for (Map.Entry<String, String> entry : metadataMap.entrySet()) {
              metadataMapInter.put(
                  StringCache.intern(entry.getKey()), StringCache.intern(entry.getValue()));
            }
            ((AmazonInfo) info).setMetadata(metadataMapInter);
          }
        }

        reader.moveUp();
      }
      return info;
    }