using System; using System.Text; using com.itac.mes.imsapi; using com.itac.mes.imsapi.domain.container; namespace MesTools.ImsApi { public class ImsApiTools { public static String[] getKeys(params ImsApiKey[] configKeys) { if (configKeys == null) { return null; } String[] retArray = new String[configKeys.Length]; for (int i = 0; i < configKeys.Length; i++) { retArray[i] = configKeys[i].Key; } return retArray; } /** * @param imsapiKeys * @param values * @return null, wenn einer der Parameter null ist oder wenn die Arrays unterschiedliche Längen haben */ public static KeyValue[] getKeyValue(ImsApiKey[] imsapiKeys, String[] values) { if (imsapiKeys == null || values == null) { return null; } if (imsapiKeys.Length != values.Length) { return null; } KeyValue[] result = new KeyValue[imsapiKeys.Length]; for (int i = 0; i < imsapiKeys.Length; i++) { result[i] = new KeyValue(imsapiKeys[i].Key, values[i]); } return result; } public static String toString(String fieldName, ImsApiKey[] imsApiKeys, String[] values) { // geordnete Ausgabe der Values (lesbar) try { StringBuilder stb = new StringBuilder(); // sind die Daten ein Array?? if (imsApiKeys == null) { return "IMSAPITools print problem: imsapiKeys=null"; } if (values == null) { return "IMSAPITools print problem: values array=null"; } if (imsApiKeys.Length == 0) { return "IMSAPITools print problem: imsapiKeys.length=0"; } bool isArray = imsApiKeys.Length < values.Length; if (isArray) { int arrayCount = values.Length / imsApiKeys.Length; // arrayCount == 0 kann nicht vorkommen // if (arrayCount == 0) { // printKeyValues(stb, fieldName, imsApiKeys, values, 0, arrayCount); // } for (int i = 0; i < arrayCount; i++) { printKeyValues(stb, fieldName, imsApiKeys, values, i, arrayCount); } } else { stb.Append(fieldName); if (!fieldName.Equals("")) { stb.Append('\n'); } printKeyValues(stb, fieldName, imsApiKeys, values, -1, -1); } return stb.ToString(); } catch (Exception) { return "IMSAPITools print problem"; } } private static void printKeyValues(StringBuilder stb, String fieldName, ImsApiKey[] imsApiKeys, String[] values, int arrayIndex, int arrayCount) { int baseIndex = 0; if (arrayIndex >= 0) { baseIndex = arrayIndex * imsApiKeys.Length; } stb.Append(fieldName); if (arrayIndex >= 0) { stb.Append('[').Append(arrayIndex).Append("]/[").Append(arrayCount).Append(']'); } if (!fieldName.Equals("") && arrayIndex >= 0) { stb.Append('\n'); } if (arrayCount <= 0) { return; } for (int k = 0; k < imsApiKeys.Length; k++) { stb.Append(" ").Append(imsApiKeys[k].Key); // Array-index ausgeben stb.Append(':'); stb.Append(values[baseIndex + k]).Append('\n'); } } /** * print keys and values in a formatted way for column-oriented logging *
*
* key1: value1, key2: value2, key3: value3, key4: value4,
*
* @param baseName
* may be null, no printing, no line feed
* @param imsApiKeys
* @param values
* @return
*/
public static void printKeyValues(StringBuilder stb, String baseName, String[] imsApiKeys, String[] values)
{
if (imsApiKeys == null || values == null)
{
stb.Append("keys/values = null");
return;
}
if (values.Length == 0)
{
stb.Append("");
return;
}
if ((values.Length % imsApiKeys.Length) != 0)
{
stb.Append("keys/values unequal size");
return;
}
int maxColumns = 132; // print-width
// find key-length and value length over all...
int keyLength = 0;
foreach (String item in imsApiKeys)
{
keyLength = Math.Max(keyLength, item.Length);
}
int valueLength = 0;
foreach (String value in values)
{
valueLength = Math.Max(valueLength, value.Length);
}
bool isArray = values.Length > imsApiKeys.Length;
int arrayIndex = 0;
int column = 0;
int valueStartIndex = 0;
if (baseName != null)
{
stb.Append(baseName);
if (baseName.Length > 0)
{
stb.Append(':');
}
stb.Append('\n');
}
while (valueStartIndex < values.Length)
{
if (isArray)
{
// print index-key
if (arrayIndex > 0)
{
stb.Append('\n');
column = 0;
}
stb.Append('[').Append(arrayIndex).Append("]\n");
}
for (int k = 0; k < imsApiKeys.Length; k++)
{
if (column == 0)
{
stb.Append(" ");
column += 3;
}
stb.Append(fillString(imsApiKeys[k], keyLength)).Append(": ");
column += (keyLength + 2 + valueLength);
if (k < (imsApiKeys.Length - 1))
{
stb.Append(fillString(values[valueStartIndex + k], valueLength, ','));
column += 2;
}
else
{
stb.Append(fillString(values[valueStartIndex + k], valueLength));
}
if (column > maxColumns)
{
stb.Append('\n');
column = 0;
}
}
arrayIndex++;
valueStartIndex += imsApiKeys.Length;
}
}
/**
* Fills a string to the given length with spaces, optionally Append a closig character.
*
* @param name
* the string to be filled
* @param keyLength
* the resulting string should have this length
* @param closingChar
* optional; if given this is Appended to the string
* @return the filled string with the given length
*/
private static String fillString(String name, int keyLength, params char[] closingChar)
{
StringBuilder stb = new StringBuilder(name);
if (closingChar.Length > 0)
{
stb.Append(closingChar[0]);
}
while (stb.Length <= keyLength)
{
stb.Append(' ');
}
return stb.ToString();
}
}
}
* key5: value5
*