private Object renderIdentifierInner()

in taverna-reference-impl/src/main/java/org/apache/taverna/reference/impl/ReferenceServiceImpl.java [464:533]


	private Object renderIdentifierInner(T2Reference id, Class<?> leafClass,
			ReferenceContext context, StreamToValueConverterSPI<?> converter)
			throws ReferenceServiceException {
		checkServices();

		switch (id.getReferenceType()) {
		case IdentifiedList:
			try {
				IdentifiedList<T2Reference> idList = listService.getList(id);
				if (idList == null)
					throw new ReferenceServiceException(
							"Could not find IdentifiedList " + id);
				List<Object> result = new ArrayList<>();
				for (T2Reference child : idList)
					result.add(renderIdentifierInner(child, leafClass, context,
							converter));
				return result;
			} catch (ListServiceException lse) {
				throw new ReferenceServiceException(lse);
			}

		case ReferenceSet:
			try {
				ReferenceSet rs = referenceSetService.getReferenceSet(id);
				if (rs == null)
					throw new ReferenceServiceException(
							"Could not find ReferenceSet " + id);
				// Check that there are references in the set
				if (rs.getExternalReferences().isEmpty())
					throw new ReferenceServiceException(
							"Can't render an empty reference set to a POJO");
				/*
				 * If we can't directly map to an appropriate value keep track
				 * of the cheapest reference from which to try to build the pojo
				 * from a stream
				 */
				ExternalReferenceSPI cheapestReference = null;
				float cheapestReferenceCost = MAX_VALUE;
				for (ExternalReferenceSPI ers : rs.getExternalReferences()) {
					if (ers instanceof ValueCarryingExternalReference<?>) {
						ValueCarryingExternalReference<?> vcer = (ValueCarryingExternalReference<?>) ers;
						if (leafClass.isAssignableFrom(vcer.getValueType()))
							return vcer.getValue();
					}
					// Got here so this wasn't an appropriate value type
					if (cheapestReference == null
							|| ers.getResolutionCost() < cheapestReferenceCost) {
						cheapestReference = ers;
						cheapestReferenceCost = ers.getResolutionCost();
					}
				}
				if (converter != null && cheapestReference != null)
					try (InputStream stream = cheapestReference
							.openStream(context)) {
						return converter.renderFrom(stream,
								cheapestReference.getDataNature(),
								cheapestReference.getCharset());
					}
			} catch (Exception e) {
				throw new ReferenceServiceException(e);
			}
			throw new ReferenceServiceException(
					"No converter found, and reference set didn't contain"
							+ " an appropriate value carrying reference, cannot render to POJO");

		default:
			throw new ReferenceServiceException("Unsupported ID type : "
					+ id.getReferenceType());
		}
	}