Java Code Examples for com.thoughtworks.xstream.io.HierarchicalStreamReader#moveDown()

The following examples show how to use com.thoughtworks.xstream.io.HierarchicalStreamReader#moveDown() . 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: XmlXstreamTest.java    From amforeas with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Object unmarshal (HierarchicalStreamReader reader, UnmarshallingContext uc) {
    Map<String, Object> map = new HashMap<String, Object>();
    MultivaluedMap<String, String> mv = new MultivaluedHashMap<>();
    while (reader.hasMoreChildren()) {
        reader.moveDown();
        mv.add(reader.getNodeName(), reader.getValue());
        map.put(reader.getNodeName(), reader.getValue());
        reader.moveUp();
    }

    if (uc.getRequiredType().equals(MultivaluedMap.class)) {
        return mv;
    } else {
        return map;
    }

}
 
Example 2
Source File: MapConverter.java    From yes-cart with Apache License 2.0 6 votes vote down vote up
protected void populateMap(HierarchicalStreamReader reader, UnmarshallingContext context, Map map) {
    while (reader.hasMoreChildren()) {
        reader.moveDown();

        reader.moveDown();
        Object key = readItem(reader, context, map);
        reader.moveUp();

        reader.moveDown();
        Object value = readItem(reader, context, map);
        reader.moveUp();

        map.put(key, value);

        reader.moveUp();
    }
}
 
Example 3
Source File: NamedArrayConverter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext context) {
    final List list = new ArrayList();
    while (reader.hasMoreChildren()) {
        reader.moveDown();
        final Object item;
        final String className = HierarchicalStreams.readClassAttribute(reader, mapper);
        final Class itemType = className == null ? arrayType.getComponentType() : mapper.realClass(className);
        if (Mapper.Null.class.equals(itemType)) {
            item = null;
        } else {
            item = context.convertAnother(null, itemType);
        }
        list.add(item);
        reader.moveUp();
    }
    final Object array = Array.newInstance(arrayType.getComponentType(), list.size());
    for (int i = 0; i < list.size(); ++i) {
        Array.set(array, i, list.get(i));
    }
    return array;
}
 
Example 4
Source File: JavaFieldConverter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    String methodName = null;
    String declaringClassName = null;
    
    while((methodName == null || declaringClassName == null) && reader.hasMoreChildren()) {
        reader.moveDown();
        
        if (reader.getNodeName().equals("name")) {
            methodName = reader.getValue();
        } else if (reader.getNodeName().equals("clazz")) {
            declaringClassName = reader.getValue();
        }
        reader.moveUp();
    }
    
    Class declaringClass = (Class)javaClassConverter.fromString(declaringClassName);
    try {
        return declaringClass.getDeclaredField(mapper.realMember(declaringClass, methodName));
    } catch (NoSuchFieldException e) {
        throw new ConversionException(e);
    }
}
 
Example 5
Source File: EdgeReferenceConverter.java    From depan with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * This implementation assumes that a {@code GraphModel}, used to find nodes,
 * can be retrieved from the {@code UnmarshallingContext}.
 */
@Override
public Object unmarshal(HierarchicalStreamReader reader,
    UnmarshallingContext context) {
  try {
    GraphModel graph = refConverter.getGraphModel(context);

    reader.moveDown();
    Relation relation = unmarshallRelation(reader, context);
    reader.moveUp();

    reader.moveDown();
    GraphNode head = unmarshallGraphNode(reader, context, graph);
    reader.moveUp();

    reader.moveDown();
    GraphNode tail = unmarshallGraphNode(reader, context, graph);
    reader.moveUp();

    GraphEdge result = (GraphEdge) graph.findEdge(relation, head, tail);
    return result;
  } catch (RuntimeException err) {
    // TODO(leeca): Add some error diagnostics, or eliminate as dead code.
    throw err;
  }
}
 
Example 6
Source File: GamaAgentConverter.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext arg1) {
	// TODO manage MinimalAgent and MinimalGridAgent
	reader.moveDown();
	// SimulationAgent simAgt = convertScope.getSimulationAgent();
	// List<IAgent> lagt;
	// if(simAgt == null) {
	// lagt = (convertScope.getScope()).getSimulation().getAgents(convertScope.getScope());
	// } else {
	// lagt = simAgt.getAgents(convertScope.getScope());
	// }
	final ReferenceAgent agt = (ReferenceAgent) arg1.convertAnother(null, ReferenceAgent.class);

	// boolean found = false;
	// int i = 0;
	// IAgent agt = null;
	// while(!found && (i < lagt.size())) {
	// if(lagt.get(i).getName().equals(reader.getValue())) {
	// found = true;
	// agt = lagt.get(i);
	// }
	// i++;
	// }
	reader.moveUp();
	return agt;
}
 
Example 7
Source File: ValueConverter.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Object unmarshal ( final HierarchicalStreamReader reader, final UnmarshallingContext context )
{
    final Value value = new Value ();

    // Reading language
    final String locale = reader.getAttribute ( LANGUAGE );
    value.setLocale ( LanguageUtils.fromString ( locale ) );

    // Reading possible single-value case attributes
    final String state = reader.getAttribute ( STATE );
    final String character = reader.getAttribute ( MNEMONIC );
    final int mnemonic = character != null ? character.charAt ( 0 ) : -1;

    // Reading texts and tooltips
    final String text = reader.getValue ();
    final List<Text> texts = new ArrayList<Text> ();
    while ( reader.hasMoreChildren () )
    {
        reader.moveDown ();
        texts.add ( ( Text ) context.convertAnother ( value, Text.class ) );
        reader.moveUp ();
    }

    // Determining what should we save
    if ( texts.size () == 0 )
    {
        // Saving single text
        value.addText ( new Text ( text, state, mnemonic ) );
    }
    else
    {
        // Saving multiple texts
        value.setTexts ( texts );
    }

    return value;
}
 
Example 8
Source File: SESARSampleMetadataXMLConverter.java    From ET_Redux with Apache License 2.0 5 votes vote down vote up
/**
 * reads a <code>Tracer</code> from the XML file specified through <code>reader</code>
 * 
 * @pre     <code>reader</code> leads to a valid <code>Tracer</code>
 * @post    the <code>Tracer</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>Object</code> - <code>Tracer</code> read from file
 *          specified by <code>reader</code>
 */
public Object unmarshal ( HierarchicalStreamReader reader,
        UnmarshallingContext context ) {

    SESARSampleMetadata SESARSampleMetadata = new SESARSampleMetadata();

    reader.moveDown();
    SESARSampleMetadata.setStratigraphicFormationName( reader.getValue() );
    reader.moveUp();

    reader.moveDown();
    SESARSampleMetadata.setStratigraphicGeologicAgeMa( reader.getValue() );
    reader.moveUp();

    reader.moveDown();
    SESARSampleMetadata.setStratigraphicMinAbsoluteAgeMa( Double.valueOf( reader.getValue() ) );
    reader.moveUp();

     reader.moveDown();
    SESARSampleMetadata.setStratigraphicMaxAbsoluteAgeMa( Double.valueOf( reader.getValue() ) );
    reader.moveUp();

    reader.moveDown();
    SESARSampleMetadata.setDetritalType( reader.getValue() );
    reader.moveUp();


    return SESARSampleMetadata;
}
 
Example 9
Source File: GamaShapeFileConverter.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 GamaShapeFile shp = new GamaShapeFile(scope.getScope(), reader.getValue());
	reader.moveUp();
	return shp;
}
 
Example 10
Source File: MeasuredRatioModelXMLConverter.java    From ET_Redux with Apache License 2.0 5 votes vote down vote up
/**
 * reads a <code>MeasuredRatioModel</code> from the XML file specified
 * through <code>reader</code>
 * 
 * @pre     <code>reader</code> leads to a valid <code>MeasuredRatioModel</code>
 * @post    returns the <code>MeasuredRatioModel</code> read from the XML file
 * @param   reader  stream to read through
 * @param   context <code>UnmarshallingContext</code> used to store generic data
 * @return  <code>MeasuredRatioModel</code> - <code>MeasuredRatioModel</code>
 *          read from file specified by <code>reader</code>
 */
public Object unmarshal(HierarchicalStreamReader reader,
        UnmarshallingContext context) {

    ValueModel measuredRatio = new MeasuredRatioModel();
    
    reader.moveDown();
    measuredRatio.setName(reader.getValue());
    reader.moveUp();
    
    reader.moveDown();
    measuredRatio.setValue(new BigDecimal(reader.getValue()));
    reader.moveUp();

    // temp hack dec 2007 during transition to new data format
    reader.moveDown();
    if ("uncertaintyType".equals(reader.getNodeName())) {
        measuredRatio.setUncertaintyType(reader.getValue());
        reader.moveUp();

        reader.moveDown();
    }
    measuredRatio.setOneSigma(new BigDecimal(reader.getValue()));
    reader.moveUp();
    
    reader.moveDown();
    ((MeasuredRatioModel)measuredRatio).setFracCorr( (reader.getValue().equalsIgnoreCase("true")) ? true : false);
    reader.moveUp();

    reader.moveDown();
    ((MeasuredRatioModel)measuredRatio).setOxideCorr( (reader.getValue().equalsIgnoreCase("true")) ? true : false);
    reader.moveUp();
    
    return measuredRatio;
}
 
Example 11
Source File: SingletonMapConverter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    reader.moveDown();
    final Object key = readCompleteItem(reader, context, null);
    final Object value = readCompleteItem(reader, context, null);
    reader.moveUp();
    
    return Collections.singletonMap(key, value); 
}
 
Example 12
Source File: GamaSpeciesConverter.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 13
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 14
Source File: RegexPatternConverter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    reader.moveDown();
    String pattern = reader.getValue();
    reader.moveUp();
    reader.moveDown();
    int flags = Integer.parseInt(reader.getValue());
    reader.moveUp();
    return Pattern.compile(pattern, flags);
}
 
Example 15
Source File: MapEntryConverter.java    From ZenQuery with Apache License 2.0 5 votes vote down vote up
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    Map<String, String> map = new HashMap<String, String>();

    while(reader.hasMoreChildren()) {
        reader.moveDown();

        String key = reader.getNodeName();
        String value = reader.getValue();
        map.put(key, value);

        reader.moveUp();
    }

    return map;
}
 
Example 16
Source File: AbstractXStreamConverter.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
protected Map<String, String> readMap(HierarchicalStreamReader reader, UnmarshallingContext context, String key, String value) {
    Map<String, String> map = new HashMap<String, String>();
    while (reader.hasMoreChildren()) {
        reader.moveDown();
        map.put(reader.getAttribute(key), reader.getAttribute(value));
        reader.moveUp();
    }
    return map;
}
 
Example 17
Source File: XmlMementoSerializer.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    String catalogItemId = null;
    instantiateNewInstanceSettingCache(reader, context);
    
    if (reader instanceof PathTrackingReader) {
        // have to assume this is first; there is no mark/reset support on these readers
        // (if there were then it would be easier, we could just look for that child anywhere,
        // and not need a custom writer!)
        if ("catalogItemId".equals( ((PathTrackingReader)reader).peekNextChild() )) {
            // cache the instance
            
            reader.moveDown();
            catalogItemId = reader.getValue();
            reader.moveUp();
        }
    }
    boolean customLoaderSet = false;
    try {
        if (Strings.isNonBlank(catalogItemId)) {
            if (lookupContext==null) throw new NullPointerException("lookupContext required to load catalog item "+catalogItemId);
            RegisteredType cat = lookupContext.lookupManagementContext().getTypeRegistry().get(catalogItemId);
            if (cat==null) {
                String upgradedItemId = CatalogUpgrades.getTypeUpgradedIfNecessary(lookupContext.lookupManagementContext(), catalogItemId);
                if (!Objects.equal(catalogItemId, upgradedItemId)) {
                    LOG.warn("Upgrading spec catalog item id from "+catalogItemId+" to "+upgradedItemId+" on rebind "+getContextDescription(context));
                    cat = lookupContext.lookupManagementContext().getTypeRegistry().get(upgradedItemId);
                    catalogItemId = upgradedItemId;
                }
            }
            if (cat==null) throw new NoSuchElementException("catalog item: "+catalogItemId);
            BrooklynClassLoadingContext clcNew = CatalogUtils.newClassLoadingContext(lookupContext.lookupManagementContext(), cat);
            delegatingClassLoader.pushClassLoadingContext(clcNew);
            customLoaderSet = true;
            
            CatalogUpgrades.markerForCodeThatLoadsJavaTypesButShouldLoadRegisteredType();
        }
        
        AbstractBrooklynObjectSpec<?, ?> result = (AbstractBrooklynObjectSpec<?, ?>) super.unmarshal(reader, context);
        // we wrote it twice so this shouldn't be necessary; but if we fix it so we only write once, we'd need this
        result.catalogItemId(catalogItemId);
        return result;
    } finally {
        context.put("SpecConverter.instance", null);
        if (customLoaderSet) {
            delegatingClassLoader.popClassLoadingContext();
        }
    }
}
 
Example 18
Source File: SampleDateModelXMLConverter.java    From ET_Redux with Apache License 2.0 4 votes vote down vote up
/**
 * reads a <code>SampleDateModel</code> from the XML file specified through <code>reader</code>
 * 
 * @pre     <code>reader</code> leads to a valid <code>SampleDateModel</code>
 * @post    the <code>SampleDateModel</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>SampleDateModel</code> - <code>SampleDateModel</code> read from file
 *          specified by <code>reader</code>
 */
public Object unmarshal ( HierarchicalStreamReader reader,
        UnmarshallingContext context ) {

    ValueModel dateModel = new SampleDateModel();

    reader.moveDown();
    dateModel.setName( reader.getValue() );
    reader.moveUp();

    // use name to look up dateName and methodname
    for (int i = 0; i < SampleDateTypes.getSampleDateModelTypes().length; i ++) {
        if ( dateModel.getName().equalsIgnoreCase( SampleDateTypes.getSampleDateModelTypes()[i][0] ) ) {
            ((SampleDateModel) dateModel).setMethodName( SampleDateTypes.getSampleDateModelTypes()[i][1] );
            ((SampleDateModel) dateModel).setDateName( SampleDateTypes.getSampleDateModelTypes()[i][2] );
            break;
        }
    }

    reader.moveDown();
    dateModel.setValue( new BigDecimal( reader.getValue() ) );
    reader.moveUp();

    reader.moveDown();
    dateModel.setUncertaintyType( reader.getValue() );
    reader.moveUp();

    reader.moveDown();
    dateModel.setOneSigma( new BigDecimal( reader.getValue() ) );
    reader.moveUp();

    reader.moveDown();
    ((SampleDateModel) dateModel).setMeanSquaredWeightedDeviation( new BigDecimal( reader.getValue() ) );
    reader.moveUp();

    reader.moveDown();
    ((SampleDateModel) dateModel).setInternalTwoSigmaUnct( new BigDecimal( reader.getValue() ) );
    reader.moveUp();

    reader.moveDown();
    ((SampleDateModel) dateModel).setInternalTwoSigmaUnctWithTracerCalibrationUnct( new BigDecimal( reader.getValue() ) );
    reader.moveUp();

    reader.moveDown();
    ((SampleDateModel) dateModel).setInternalTwoSigmaUnctWithTracerCalibrationAndDecayConstantUnct( new BigDecimal( reader.getValue() ) );
    reader.moveUp();

    // reader.moveDown();
    // ((SampleDateModel) dateModel).setIncludedFractionIDsVector(reader.getValue());
    // reader.moveUp();

    reader.moveDown();
    if ( "includedFractionsVector".equals( reader.getNodeName() ) ) {
        Vector<String> includedFractions = new Vector<String>();
        while (reader.hasMoreChildren()) {
            reader.moveDown();
            String item = new String();
            item = (String) context.convertAnother( item, String.class );
            includedFractions.add( item );
            reader.moveUp();
        }

        ((SampleDateModel) dateModel).setIncludedFractionIDsVector( includedFractions );
    }
    reader.moveUp();

    reader.moveDown();
    ((SampleDateModel) dateModel).setExplanation( reader.getValue() );
    reader.moveUp();

    reader.moveDown();
    ((SampleDateModel) dateModel).setComment( reader.getValue() );
    reader.moveUp();

    reader.moveDown();
    ((SampleDateModel) dateModel).setPreferred( (reader.getValue().equalsIgnoreCase( "true" )) ? true : false );
    reader.moveUp();


    return dateModel;
}
 
Example 19
Source File: PbBlankICModelXMLConverter.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>
 */
@Override
public Object unmarshal ( HierarchicalStreamReader reader,
        UnmarshallingContext context ) {

    AbstractRatiosDataModel pbBlankICModel = PbBlankICModel.createNewInstance();

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

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

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

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

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

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

    reader.moveDown();
    pbBlankICModel.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 ValueModel();
            item = (ValueModel) context.convertAnother( item, ValueModel.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 );
        }

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

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

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

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

    return pbBlankICModel;
}
 
Example 20
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);
  }
}