Java Code Examples for com.thoughtworks.xstream.converters.UnmarshallingContext#convertAnother()

The following examples show how to use com.thoughtworks.xstream.converters.UnmarshallingContext#convertAnother() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChannelConverter.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    // read attributes
    Map<String, String> attributes = this.attributeMapValidator.readValidatedAttributes(reader);

    // read values
    List<?> nodes = (List<?>) context.convertAnother(context, List.class);
    NodeIterator nodeIterator = new NodeIterator(nodes);

    // create object
    Object object = unmarshalType(reader, context, attributes, nodeIterator);

    nodeIterator.assertEndOfType();

    return object;
}
 
Example 2
Source File: ConverterValueMap.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Reads-in {@code N} children in a key-value map and returns it.
 *
 * @param reader the reader to be used to read-in the children (must not be null)
 * @param numberOfValues the number of children to be read in (< 0 = until end of section)
 * @param context
 * @return the key-value map containing the read-in children (not null, could be empty)
 * @throws ConversionException if not all children could be read-in
 */
public static Map<String, Object> readValueMap(HierarchicalStreamReader reader, int numberOfValues,
        UnmarshallingContext context) throws ConversionException {
    Map<String, Object> valueMap = new HashMap<>((numberOfValues >= 0) ? numberOfValues : 10);
    int counter = 0;

    while (reader.hasMoreChildren() && ((counter < numberOfValues) || (numberOfValues == -1))) {
        reader.moveDown();
        if (reader.hasMoreChildren()) {
            List<?> list = (List<?>) context.convertAnother(context, List.class);
            valueMap.put(reader.getNodeName(), list);
        } else {
            valueMap.put(reader.getNodeName(), reader.getValue());
        }
        reader.moveUp();
        counter++;
    }

    if ((counter < numberOfValues) && (numberOfValues > 0)) {
        throw new ConversionException("Not all children could be read-in!");
    }

    return valueMap;
}
 
Example 3
Source File: NodeListConverter.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    Map<String, String> attributes = ConverterAttributeMapValidator.readValidatedAttributes(reader, null);

    String nodeName = reader.getNodeName();
    List<?> values = (List<?>) context.convertAnother(context, List.class);

    return new NodeList(nodeName, attributes, values);
}
 
Example 4
Source File: AbstractMappingConverter.java    From depan with Apache License 2.0 5 votes vote down vote up
protected Object unmarshalObject(
    HierarchicalStreamReader reader, UnmarshallingContext context) {
  reader.moveDown();
  try {
    String childName = reader.getNodeName();
    Class<?> childClass = mapper.realClass(childName);

    return context.convertAnother(null, childClass);
  } finally {
    reader.moveUp();
  }
}
 
Example 5
Source File: ComponentStyleConverter.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Reads nested {@link ComponentStyle}s.
 *
 * @param style   parent {@link ComponentStyle}
 * @param styles  {@link List} to read nested {@link ComponentStyle}s into
 * @param context {@link UnmarshallingContext}
 */
private void readNestedStyles ( @NotNull final ComponentStyle style, @NotNull final List<ComponentStyle> styles,
                                @NotNull final UnmarshallingContext context )
{
    // Reading nested component style
    final ComponentStyle childStyle = ( ComponentStyle ) context.convertAnother ( style, ComponentStyle.class );

    // Saving parent style reference
    childStyle.setParent ( style );

    // Adding single child style
    styles.add ( childStyle );
}
 
Example 6
Source File: EdgeReferenceConverter.java    From depan with Apache License 2.0 5 votes vote down vote up
private Relation unmarshallRelation(
    HierarchicalStreamReader reader, UnmarshallingContext context) {
  String classAttribute =
      reader.getAttribute(mapper.aliasForAttribute("class"));
  Class<?> resultClass = mapper.realClass(classAttribute);
  Relation relation = (Relation) context.convertAnother(null, resultClass);
  return relation;
}
 
Example 7
Source File: AnnotationReflectionConverter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected Object unmarshallField(
                                 final UnmarshallingContext context, final Object result,
                                 Class type, Field field) {
    XStreamConverter annotation = annotationProvider.getAnnotation(
        field, XStreamConverter.class);
    if (annotation != null) {
        Class<? extends Converter> converterType = (Class<? extends Converter>)annotation.value();
        ensureCache(converterType);
        return context.convertAnother(result, type, cachedConverters.get(converterType));
    } else {
        return context.convertAnother(result, type);
    }
}
 
Example 8
Source File: GamaListConverterNetwork.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext arg1) {
	// reader.moveDown();
	final GamaListReducerNetwork rmt =
			(GamaListReducerNetwork) arg1.convertAnother(null, GamaListReducerNetwork.class);
	// reader.moveUp();
	return rmt.constructObject(convertScope.getScope());
}
 
Example 9
Source File: GamaPopulationConverter.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext context) {

	reader.moveDown();
	final IList<IAgent> listAgetFromPopulation = (IList<IAgent>) context.convertAnother(null, IList.class);
	reader.moveUp();

	return listAgetFromPopulation;
}
 
Example 10
Source File: GamaMapConverter.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext arg1) {
	// reader.moveDown();
	final GamaMapReducer rmt = (GamaMapReducer) arg1.convertAnother(null, GamaMapReducer.class);
	// reader.moveUp();
	return rmt.constructObject(convertScope.getScope());
}
 
Example 11
Source File: AggListConverter.java    From pentaho-aggdesigner with GNU General Public License v2.0 5 votes vote down vote up
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
  List<UIAggregate> list = new ArrayList<UIAggregate>();
  while (reader.hasMoreChildren()) {
    reader.moveDown();
    if ("aggregation".equals(reader.getNodeName())) {
            UIAggregateImpl agg = (UIAggregateImpl)context.convertAnother(null, UIAggregateImpl.class);
            list.add(agg);
    }
    reader.moveUp();
  }
  aggList.clearAggs();
  aggList.addAggs(list);
  return aggList; 
}
 
Example 12
Source File: SystemClockConverter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext context) {
    reader.moveDown();
    final ZoneId zone = (ZoneId)context.convertAnother(null, ZoneId.class);
    reader.moveUp();
    return Clock.system(zone);
}
 
Example 13
Source File: CGLIBEnhancedConverter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void readCallback(HierarchicalStreamReader reader, UnmarshallingContext context,
    List callbacksToEnhance, List callbacks) {
    Callback callback = (Callback)context.convertAnother(null, mapper.realClass(reader
        .getNodeName()));
    callbacks.add(callback);
    if (callback == null) {
        callbacksToEnhance.add(NoOp.INSTANCE);
    } else {
        callbacksToEnhance.add(callback);
    }
}
 
Example 14
Source File: GraphModelReferenceConverter.java    From depan with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * Obtain the {@link GraphModelReference}, including loading the saved
 * {@link GraphModel} from a project-based or file-system relative location.
 * 
 * @see EdgeConverter#unmarshal(HierarchicalStreamReader, UnmarshallingContext)
 */
@Override
public Object unmarshal(
    HierarchicalStreamReader reader, UnmarshallingContext context) {

  String graphPath = (String) context.convertAnother(null, String.class);
  if (null == graphPath) {
    throw new RuntimeException("Missing location for dependencies");
  }
  if (graphPath.startsWith("/")) {
    return unmarshalProjectGraphFile(graphPath, context);
  }
  return unmarshalRelativeGraphFile(graphPath, context);
}
 
Example 15
Source File: BindingInfoConverter.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    BindingInfoXmlResult bindingInfoXmlResult = null;
    BindingInfo bindingInfo = null;

    // read attributes
    Map<String, String> attributes = this.attributeMapValidator.readValidatedAttributes(reader);
    String id = attributes.get("id");

    // set automatically extracted URI for a possible 'config-description' section
    context.put("config-description.uri", "binding:" + id);

    // read values
    List<?> nodes = (List<?>) context.convertAnother(context, List.class);
    NodeIterator nodeIterator = new NodeIterator(nodes);

    String name = (String) nodeIterator.nextValue("name", true);
    String description = (String) nodeIterator.nextValue("description", false);
    String author = (String) nodeIterator.nextValue("author", false);
    String serviceId = (String) nodeIterator.nextValue("service-id", false);

    URI configDescriptionURI = readConfigDescriptionURI(nodeIterator);
    ConfigDescription configDescription = null;
    if (configDescriptionURI == null) {
        configDescription = readConfigDescription(nodeIterator);
        if (configDescription != null) {
            configDescriptionURI = configDescription.getUID();
        }
    }

    nodeIterator.assertEndOfType();

    // create object
    bindingInfo = new BindingInfo(id, name, description, author, serviceId, configDescriptionURI);
    bindingInfoXmlResult = new BindingInfoXmlResult(bindingInfo, configDescription);

    return bindingInfoXmlResult;
}
 
Example 16
Source File: GamaMatrixConverter.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext arg1) {
	final GamaMatrixReducer rmt = (GamaMatrixReducer) arg1.convertAnother(null, GamaMatrixReducer.class);
	return rmt.constructObject(convertScope.getScope());
}
 
Example 17
Source File: GamaGraphConverter.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext arg1) {
	final GamaGraphReducer rmt = (GamaGraphReducer) arg1.convertAnother(null, GamaGraphReducer.class);
	return rmt.constructObject(convertScope.getScope());
}
 
Example 18
Source File: GraphModelConverter.java    From depan with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * This implementation injects a newly synthesized {@code GraphBuilder}
 * instance into the {@code UnmarshallingContext} with the key
 * {@code GraphBuilder.class}.  This allows the {@link EdgeConverter} to
 * translate node ids directly into node references.
 * 
 * @see EdgeConverter#unmarshal(HierarchicalStreamReader, UnmarshallingContext)
 */
@Override
public Object unmarshal(
    HierarchicalStreamReader reader, UnmarshallingContext context) {
  // There should not be two graphs in the same serialization,
  // but just in case ....
  GraphBuilder prior = contextGraphBuilder(context);

  try {
    GraphBuilder builder = GraphBuilders.createGraphModelBuilder();
    context.put(GraphBuilder.class, builder);

    while (reader.hasMoreChildren()) {
      reader.moveDown();
      String childName = reader.getNodeName();
      Class<?> childClass = mapper.realClass(childName);

      if (GraphNode.class.isAssignableFrom(childClass)) {
        GraphNode node = (GraphNode) context.convertAnother(null, childClass);
        builder.newNode(node);
      }
      else if (GraphEdge.class.isAssignableFrom(childClass)) {
        GraphEdge edge =
            (GraphEdge) context.convertAnother(null, childClass);
        builder.addEdge(edge);
      } else {
        LOG.info("Skipped object with tag {}", childName);
      }

      reader.moveUp();
    }

    return builder.createGraphModel();
  } catch (RuntimeException err) {
    // TODO Auto-generated catch block
    err.printStackTrace();
    throw err;
  } finally {
    context.put(GraphBuilder.class, prior);
  }
}
 
Example 19
Source File: PhysicalConstantsModelXMLConverter.java    From ET_Redux with Apache License 2.0 4 votes vote down vote up
/**
 * reads a
 * <code>PbBlank</code> from the XML file specified through
 * <code>reader</code>
 *
 * @pre
 * <code>reader</code> leads to a valid
 * <code>PbBlank</code> @post the
 * <code>PbBlank</code> is read from the XML file and returned
 *
 * @param reader stream to read through
 * @param context
 * <code>UnmarshallingContext</code> used to store generic data
 * @return
 * <code>PbBlank</code> -
 * <code>PbBlank</code> read from file specified by
 * <code>reader</code>
 */
public Object unmarshal ( HierarchicalStreamReader reader,
        UnmarshallingContext context ) {

    AbstractRatiosDataModel physicalConstantsModel = PhysicalConstantsModel.createNewInstance();

    reader.moveDown();
    physicalConstantsModel.setModelName( reader.getValue() );
    reader.moveUp();

    reader.moveDown();
    physicalConstantsModel.setVersionNumber( Integer.parseInt( reader.getValue() ) );
    reader.moveUp();

    reader.moveDown();
    if ( "minorVersionNumber".equals( reader.getNodeName() ) ) {
        physicalConstantsModel.setMinorVersionNumber( Integer.valueOf( reader.getValue() ) );
        reader.moveUp();
        reader.moveDown();
    } else {
        physicalConstantsModel.setMinorVersionNumber( 0 );
    }

    physicalConstantsModel.setLabName( reader.getValue() );
    reader.moveUp();

    reader.moveDown();
    physicalConstantsModel.setDateCertified( reader.getValue() );
    reader.moveUp();

    reader.moveDown();
    physicalConstantsModel.setReference( reader.getValue() );
    reader.moveUp();

    reader.moveDown();
    physicalConstantsModel.setComment( reader.getValue() );
    reader.moveUp();

    reader.moveDown();
    if ( "ratios".equals( reader.getNodeName() ) ) {
        ArrayList<ValueModel> ratios = new ArrayList<ValueModel>();
        while (reader.hasMoreChildren()) {
            reader.moveDown();
            ValueModel item = new ValueModelReferenced();
            item = (ValueModel) context.convertAnother( item, ValueModelReferenced.class );
            ratios.add( item );
            reader.moveUp();
        }
        // Convert to array
        ValueModel[] arrayRatios = new ValueModel[ratios.size()];
        for (int i = 0; i < ratios.size(); i ++) {
            arrayRatios[i] = ratios.get( i );
        }

        physicalConstantsModel.setRatios( arrayRatios );
    }
    reader.moveUp();

    reader.moveDown();
    if ( "rhos".equals( reader.getNodeName() ) ) {
        Map<String, BigDecimal> rhos = new HashMap<>();

        rhos = (Map<String, BigDecimal>) context.convertAnother( rhos, Map.class );

        physicalConstantsModel.setRhosVarUnct( rhos );
    }
    reader.moveUp();

    reader.moveDown();
    if ( "atomicMolarMasses".equals( reader.getNodeName() ) ) {
        Map<String, BigDecimal> atomicMolarMasses = new TreeMap<>();

        atomicMolarMasses = (Map<String, BigDecimal>) context.convertAnother( atomicMolarMasses, Map.class );

        ((PhysicalConstantsModel) physicalConstantsModel).setAtomicMolarMasses( atomicMolarMasses );
    }
    reader.moveUp();

    return physicalConstantsModel;
}
 
Example 20
Source File: AbstractXStreamConverter.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
protected <T> T readObject(HierarchicalStreamReader reader, UnmarshallingContext context, Class<? extends T> clazz) {
    return (T) context.convertAnother(reader.getValue(), clazz);
}