/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2015 Ausenco Engineering Canada Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jaamsim.StringProviders;

import com.jaamsim.basicsim.ErrorException;
import com.jaamsim.basicsim.ObjectType;
import com.jaamsim.input.OutputChain;
import com.jaamsim.input.OutputHandle;
import com.jaamsim.units.Unit;

public class StringProvOutput implements StringProvider {

	private final OutputChain chain;
	private final Class<? extends Unit> unitType;

	public StringProvOutput(OutputChain ch, Class<? extends Unit> ut) {
		chain = ch;
		unitType = ut;
	}

	@Override
	public String getNextString(double simTime, String fmt, double siFactor) {

		OutputHandle out = chain.getOutputHandle(simTime);

		if (out == null)
			throw new ErrorException("Expression cannot be evaluated: %s.", chain.toString());

		if (out.isNumericValue()) {
			if (out.getUnitType() != unitType && unitType != null)
				throw new ErrorException("Unit mismatch. Expected a %s, received a %s",
						ObjectType.getObjectTypeForClass(unitType),
						ObjectType.getObjectTypeForClass(out.getUnitType()));
			double d = out.getValueAsDouble(simTime, 0.0d);
			return String.format(fmt, d/siFactor);
		}
		else {
			Object obj = out.getValue(simTime, out.getReturnType());
			return String.format(fmt, obj);
		}
	}

	@Override
	public String toString() {
		return chain.toString();
	}

}

@@ -771,16 +771,53 @@
 ctType;%0A
+import com.jaamsim.input.InputAgent;%0A
 import c
@@ -1237,19 +1237,17 @@
  double 
-siF
+f
 actor) %7B
@@ -1438,19 +1438,12 @@
 ue()
-) %7B%0A%09%09%09if (
+ && 
 out.
@@ -1488,21 +1488,22 @@
 != null)
+ %7B
 %0A%09%09%09
-%09
 throw ne
@@ -1566,17 +1566,16 @@
  a %25s%22,%0A
-%09
 %09%09%09%09%09Obj
@@ -1616,25 +1616,24 @@
 Type),%0A%09%09%09%09%09
-%09
 ObjectType.g
@@ -1680,210 +1680,81 @@
 ;%0A%09%09
-%09double d = out.getValueAsDouble(simTime, 0.0d);%0A%09%09%09return String.format(fmt, d/siFactor);%0A%09%09%7D%0A%09%09else %7B%0A%09%09%09Object obj = out.getValue(simTime, out.getReturnType());%0A%09%09%09return String.format(fmt, obj);%0A%09%09%7D
+%7D%0A%0A%09%09return InputAgent.getValueAsString(out, simTime, fmt, %22%25s%22, factor);
 %0A%09%7D%0A

use output formating helper in StringProvOutput