Java服务,它在运行时提取字段的值

时间:2012-08-01 04:59:23

标签: java web-services xpath webmethods

我需要构建一个java服务,它在运行时提取字段的值。规范文档中的字段路径和规范文档本身应作为输入。

例如: fromDoc由层次结构中的子文档组成,即。 fromDoc /数据/参数/过时

 •fromDoc

     •Data

        •Parameters

           •outDate(string)

对于inStringValues,我将输入作为'fromDoc / Data / Parameters / outDate'

输出应该在运行时返回变量'fromDoc / Data / Parameters / outDate'的值。

我有一个使用键值对逻辑实现此功能的代码。

IDataCursor pipelineCursor = pipeline.getCursor();

      // fromDoc
IData     fromDoc = IDataUtil.getIData( pipelineCursor, "fromDoc" );
String[] inStringValues = IDataUtil.getStringArray( pipelineCursor, "inStringValues" );
if ( fromDoc == null)
{
    return;
}
pipelineCursor.destroy();
int len = inStringValues.length;
String[] outStrings = new String[len];
IDataCursor fromCursor = fromDoc.getCursor();
boolean hasData = false;

while( fromCursor.next() )
{
    for(int i=0;i<len;i++)
    {
        String key = fromCursor.getKey();
        String val = fromCursor.getValue().toString();
        if(key.equals(inStringValues[i]))
        {
             outStrings[i]=key + "," + val;
        }
    }
}

fromCursor.destroy();

IDataCursor pipelineCursor_1 = pipeline.getCursor();
IDataUtil.put( pipelineCursor_1, "outStrings", outStrings );

pipelineCursor_1.destroy();

请告诉我如何修改此代码以实现上述逻辑? 或者让我知道是否有人有这样的现有服务。

2 个答案:

答案 0 :(得分:1)

Tundra提供的服务可以满足您的需求:tundra.document:get($document, $key)

  • $document是您要从
  • 中提取值的com.wm.data.IData文档
  • $key是您要提取的密钥的名称,可以是完全限定的并包含数组索引,例如"path/to/my[1]/key"

相关的Java代码如下:

public static final void get(IData pipeline) throws ServiceException {
  IDataCursor cursor = pipeline.getCursor();

  try {
    IData document = IDataUtil.getIData(cursor, "$document");
    String key = IDataUtil.getString(cursor, "$key");
    IDataUtil.put(cursor, "$value", get(document, key));
  } finally {
    cursor.destroy();
  }          
}

// wrapper class for fully qualified IData keys
public static class Key {
  public static final String SEPARATOR = "/";
  public static final java.util.regex.Pattern INDEX_PATTERN = java.util.regex.Pattern.compile("\\[(-?\\d+?)\\]$");

  protected boolean hasIndex = false;
  protected int index = 0;
  protected String key = null;

  public Key(String key) {
    java.util.regex.Matcher matcher = INDEX_PATTERN.matcher(key);
    StringBuffer buffer = new StringBuffer();
    while(matcher.find()) {
      hasIndex = true;
      index = Integer.parseInt(matcher.group(1));
      matcher.appendReplacement(buffer, "");
    }
    matcher.appendTail(buffer);
    this.key = buffer.toString();
  }

  public boolean hasIndex() {
    return hasIndex;
  }

  public int getIndex() {
    return index;
  }

  public String toString() {
    return key;
  }

  public static java.util.Queue<Key> parse(String key) {
    String[] parts = key.split(SEPARATOR);
    java.util.Queue<Key> queue = new java.util.ArrayDeque<Key>(parts.length);

    for (int i = 0; i < parts.length; i++) {
      queue.add(new Key(parts[i]));
    }
    return queue;
  }

  public static boolean isFullyQualified(String key) {
    return key != null && (key.contains(SEPARATOR) || INDEX_PATTERN.matcher(key).find());
  }
}

// returns the value associated with the given key from the given IData document
public static Object get(IData input, String key) {
  Object value = null;
  if (input != null && key != null) {
    // try finding a value that matches the literal key
    IDataCursor cursor = input.getCursor();
    try {
      value = IDataUtil.get(cursor, key);
    } finally {
      cursor.destroy();
    }

    // if value wasn't found using the literal key, the key could be fully qualified
    if (value == null && Key.isFullyQualified(key)) value = get(input, Key.parse(key));
  }

  return value;
}

// gets a value from an IData document with a fully qualified key
protected static Object get(IData input, java.util.Queue<Key> keys) {
  Object value = null;

  if (input != null && keys != null && keys.size() > 0) {
    IDataCursor cursor = input.getCursor();
    Key key = keys.remove();

    if (keys.size() > 0) {
      if (key.hasIndex()) {
        IData[] array = IDataUtil.getIDataArray(cursor, key.toString());
        value = get(get(array, key.getIndex()), keys);
      } else {
        value = get(IDataUtil.getIData(cursor, key.toString()), keys);
      }
    } else {
      if (key.hasIndex()) {
        Object[] array = IDataUtil.getObjectArray(cursor, key.toString());
        value = get(array, key.getIndex());
      } else {
        value = IDataUtil.get(cursor, key.toString());
      }
    }
    cursor.destroy();
  }

  return value;
}

public static <T> T get(T[] array, int index) {
  T item = null;

  if (array != null) {
    // support reverse/tail indexing
    if (index < 0) index += array.length;

    item = array[index];
  }

  return item;
}

注意:Tundra还提供了一项补充服务,用于在com.wm.data.IData文档中设置值:tundra.document.put($document, $key, $value)

答案 1 :(得分:0)

这个怎么样?

    // fromDoc
    IDataCursor cursor = pipeline.getCursor();
    IData fromDoc = IDataUtil.getIData(cursor, "fromDoc");

    if (fromDoc == null) {
        return;
    }

    // Data
    cursor = fromDoc.getCursor();
    IData Data = IDataUtil.getIData(cursor, "Data");

    if (Data == null) {
        return;
    }

    // Parameters
    cursor = Data.getCursor();
    IData Parameters = IDataUtil.getIData(cursor, "Parameters");

    if (Parameters == null) {
        return;
    }

    // outDate
    cursor = Parameters.getCursor();
    String outDate = IDataUtil.getString(cursor, "outDate");

    // Here I have the outDate value