use of jp.ossc.nimbus.beans.dataset.PropertySchemaDefineException in project nimbus by nimbus-org.
the class HttpResponseImplTest method testSetObjectByServiceName.
/**
* HttpResponseのストリームを設定、取得するテスト。
* <p>
* 条件:
* <ul>
* <li>XMLストリームデータをデータセットに変換して取得する</li>
* <li>次の内容をDataSetXMLConverterで変換する<BR>
* <PRE>
* <?xml version="1.0" encoding="UTF-8"?>
* <dataSet>
* <schema>
* <header name="TestHeader">
* :A,java.util.Date,
* "jp.ossc.nimbus.util.converter.DateFormatConverter{ConvertType=2;Format="yyyy-MM-DD"}",
* "jp.ossc.nimbus.util.converter.DateFormatConverter{ConvertType=1;Format="yyyy-MM-DD"}",
* "@value@ != null"
* :B,java.lang.String,,,
* </header>
* </schema>
* <header name="TestHeader"><A>2008-01-28</A><B>TestValue</B></header></dataSet>
* <PRE></li>
* <li>上記のストリームデータとコンバータをプロパティに設定して、<BR>
* HttpResponse#getObjectを実行</li>
* </ul>
* 確認:
* <ul>
* <li>正しい値が返ってくることを確認</li>
* </ul>
*/
public void testSetObjectByServiceName() {
try {
// コンバータサービスの定義ファイルをロード
if (!ServiceManagerFactory.loadManager("jp/ossc/nimbus/service/http/httpclient/service-conv.xml")) {
System.exit(-1);
}
HttpResponseImpl res = new HttpResponseImpl();
// CharacterEncodingを設定しておく
res.headerMap = new HashMap();
String[] vals = new String[] { "application/xml;charset=Shift_JIS" };
res.headerMap.put("Content-Type", vals);
String inxml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<dataSet><schema><header name=\"TestHeader\">" + ":A,java.util.Date," + "\"jp.ossc.nimbus.util.converter.DateFormatConverter{ConvertType=2;Format=\"yyyy-MM-DD\"}\"," + "\"jp.ossc.nimbus.util.converter.DateFormatConverter{ConvertType=1;Format=\"yyyy-MM-DD\"}\"," + "\"@value@ != null\"\n:B,java.lang.String,,," + "</header></schema><header name=\"TestHeader\">" + "<A>2008-01-28</A><B>TestValue</B></header></dataSet>";
// 入力ストリームとコンバーターサービス名をプロパティにセット
InputStream is = new ByteArrayInputStream(inxml.getBytes());
res.setInputStream(is);
ServiceName name = new ServiceName("DataSetXMLConverter");
res.setStreamConverterServiceName(name);
//
DataSet dataset = (DataSet) res.getObject();
assertEquals("TestHeader", dataset.getHeader("TestHeader").getName());
assertEquals(":A,java.util.Date," + "\"jp.ossc.nimbus.util.converter.DateFormatConverter{ConvertType=2;Format=\"yyyy-MM-DD\"}\"," + "\"jp.ossc.nimbus.util.converter.DateFormatConverter{ConvertType=1;Format=\"yyyy-MM-DD\"}\"," + "\"@value@ != null\"\n:B,java.lang.String,,,", dataset.getHeader("TestHeader").getSchema());
assertEquals("2008-01-28", dataset.getHeader("TestHeader").getFormatProperty("A"));
assertEquals("TestValue", dataset.getHeader("TestHeader").getProperty("B"));
} catch (PropertySchemaDefineException e) {
e.printStackTrace();
fail("例外発生");
} catch (ConvertException e) {
e.printStackTrace();
fail("例外発生");
} finally {
ServiceManagerFactory.unloadManager("jp/ossc/nimbus/service/http/httpclient/service-conv.xml");
}
}
use of jp.ossc.nimbus.beans.dataset.PropertySchemaDefineException in project nimbus by nimbus-org.
the class BeanExchangeConverter method convert.
private Object convert(Object input, Object output, boolean isClone) throws ConvertException {
if (output == null) {
throw new ConvertException("Output is null.");
}
if (isClone) {
if (!(output instanceof Cloneable)) {
throw new ConvertException("Output is not cloneable.");
}
if (output instanceof DataSet) {
output = ((DataSet) output).cloneSchema();
} else if (output instanceof RecordList) {
output = ((RecordList) output).cloneSchema();
} else if (output instanceof Record) {
output = ((Record) output).cloneSchema();
} else {
try {
output = output.getClass().getMethod("clone", (Class[]) null).invoke(output, (Object[]) null);
} catch (NoSuchMethodException e) {
throw new ConvertException(e);
} catch (IllegalAccessException e) {
throw new ConvertException(e);
} catch (InvocationTargetException e) {
throw new ConvertException(e);
}
}
}
if (input == null) {
return output;
}
Object[] inputs = null;
if (input instanceof Collection) {
inputs = ((Collection) input).toArray();
} else if (input.getClass().isArray()) {
inputs = (Object[]) input;
}
if (inputs != null) {
if (inputs.length == 0) {
return output;
}
if (output instanceof RecordList) {
RecordList list = (RecordList) output;
for (int i = 0; i < inputs.length; i++) {
Record record = list.createRecord();
list.add(convert(inputs[i], record, false));
}
return list;
} else if (output instanceof Collection) {
Object[] outputs = ((Collection) output).toArray();
if (outputs.length == 0) {
throw new ConvertException("Size of collection is 0.");
}
for (int i = 0, imax = Math.min(inputs.length, outputs.length); i < imax; i++) {
outputs[i] = convert(inputs[i], outputs[i], false);
}
return outputs;
} else if (output.getClass().isArray()) {
Object[] outputs = (Object[]) output;
final Class componentType = output.getClass().getComponentType();
if (outputs.length == 0) {
if (componentType.isInterface() || componentType.isPrimitive()) {
throw new ConvertException("Length of array is 0.");
}
outputs = (Object[]) Array.newInstance(componentType, inputs.length);
try {
for (int i = 0; i < outputs.length; i++) {
outputs[i] = componentType.newInstance();
}
} catch (IllegalAccessException e) {
throw new ConvertException("Length of array is 0.", e);
} catch (InstantiationException e) {
throw new ConvertException("Length of array is 0.", e);
}
}
for (int i = 0, imax = Math.min(inputs.length, outputs.length); i < imax; i++) {
if (outputs[i] == null) {
if (componentType.isInterface() || componentType.isPrimitive()) {
throw new ConvertException("Element of array is null.");
}
try {
outputs[i] = componentType.newInstance();
} catch (IllegalAccessException e) {
throw new ConvertException("Element of array is null.", e);
} catch (InstantiationException e) {
throw new ConvertException("Element of array is null.", e);
}
}
outputs[i] = convert(inputs[i], outputs[i], false);
}
return outputs;
}
}
Map propMapping = propertyMapping;
boolean isOutputAutoMapping = false;
boolean isInputAutoMapping = false;
if (propMapping == null || propMapping.size() == 0) {
if (propMapping == null) {
propMapping = new HashMap();
}
if (output instanceof Record) {
Record record = (Record) output;
RecordSchema schema = record.getRecordSchema();
if (schema != null) {
for (int i = 0, imax = schema.getPropertySize(); i < imax; i++) {
propMapping.put(schema.getPropertyName(i), schema.getPropertyName(i));
}
}
if (propMapping.size() != 0) {
isOutputAutoMapping = true;
}
} else {
final SimpleProperty[] props = isFieldOnly(output.getClass()) ? SimpleProperty.getFieldProperties(output) : SimpleProperty.getProperties(output, !isAccessorOnly(output.getClass()));
for (int i = 0; i < props.length; i++) {
if (isEnabledPropertyName(output.getClass(), props[i].getPropertyName()) && props[i].isWritable(output.getClass())) {
propMapping.put(props[i].getPropertyName(), props[i].getPropertyName());
}
}
if (propMapping.size() != 0) {
isOutputAutoMapping = true;
}
}
if (!isOutputAutoMapping) {
if (input instanceof Record) {
Record record = (Record) input;
RecordSchema schema = record.getRecordSchema();
if (schema != null) {
for (int i = 0, imax = schema.getPropertySize(); i < imax; i++) {
propMapping.put(schema.getPropertyName(i), schema.getPropertyName(i));
}
}
if (propMapping.size() != 0) {
isInputAutoMapping = true;
}
} else {
final SimpleProperty[] props = isFieldOnly(output.getClass()) ? SimpleProperty.getFieldProperties(input) : SimpleProperty.getProperties(input, !isAccessorOnly(output.getClass()));
for (int i = 0; i < props.length; i++) {
if (isEnabledPropertyName(output.getClass(), props[i].getPropertyName()) && props[i].isReadable(input.getClass())) {
propMapping.put(props[i].getPropertyName(), props[i].getPropertyName());
}
}
if (propMapping.size() != 0) {
isInputAutoMapping = true;
}
}
}
if (propMapping.size() == 0) {
throw new ConvertException("PropertyMapping is null.");
}
}
if (isMakeSchema && (output instanceof Record) && ((Record) output).getRecordSchema() == null) {
StringBuilder buf = new StringBuilder();
final Iterator entries = propMapping.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
String inputProp = (String) entry.getKey();
Object outputProp = entry.getValue();
try {
Property prop = propertyAccess.getProperty(inputProp);
Class propType = prop.getPropertyType(input);
if (outputProp instanceof String) {
buf.append(':').append(outputProp);
buf.append(',').append(propType.getName()).append('\n');
} else {
List outputProps = (List) outputProp;
for (int i = 0, imax = outputProps.size(); i < imax; i++) {
buf.append(':').append((String) outputProps.get(i));
buf.append(',').append(propType.getName()).append('\n');
}
}
} catch (IllegalArgumentException e) {
throw new ConvertException("Input property parse error. property=" + inputProp, e);
} catch (NoSuchPropertyException e) {
if (isOutputAutoMapping) {
continue;
}
throw new ConvertException("Input property get error. input=" + input + ", property=" + inputProp, e);
} catch (InvocationTargetException e) {
throw new ConvertException("Input property get error. input=" + input + ", property=" + inputProp, e);
}
}
if (buf.length() != 0) {
try {
((Record) output).setSchema(buf.toString());
} catch (PropertySchemaDefineException e) {
// 起こらない
}
}
}
final Iterator entries = propMapping.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
String inputProp = (String) entry.getKey();
Object value = null;
try {
value = propertyAccess.get(input, inputProp);
} catch (IllegalArgumentException e) {
throw new ConvertException("Input property get error. input=" + input + ", property=" + inputProp, e);
} catch (NoSuchPropertyException e) {
if (isOutputAutoMapping) {
continue;
}
throw new ConvertException("Input property get error. input=" + input + ", property=" + inputProp, e);
} catch (InvocationTargetException e) {
throw new ConvertException("Input property get error. input=" + input + ", property=" + inputProp, e);
}
Object outputProp = entry.getValue();
if (outputProp instanceof String) {
try {
propertyAccess.set(output, (String) outputProp, value);
} catch (IllegalArgumentException e) {
throw new ConvertException("Output property set error. output=" + output + ", property=" + outputProp + ", value=" + value, e);
} catch (NoSuchPropertyException e) {
if (isInputAutoMapping) {
continue;
}
throw new ConvertException("Output property set error. output=" + output + ", property=" + outputProp + ", value=" + value, e);
} catch (InvocationTargetException e) {
throw new ConvertException("Output property set error. output=" + output + ", property=" + outputProp + ", value=" + value, e);
}
} else {
List outputProps = (List) outputProp;
try {
for (int i = 0, imax = outputProps.size(); i < imax; i++) {
propertyAccess.set(output, (String) outputProps.get(i), value);
}
} catch (IllegalArgumentException e) {
throw new ConvertException("Output property set error. output=" + output + ", property=" + outputProp + ", value=" + value, e);
} catch (NoSuchPropertyException e) {
if (isInputAutoMapping) {
continue;
}
throw new ConvertException("Output property set error. output=" + output + ", property=" + outputProp + ", value=" + value, e);
} catch (InvocationTargetException e) {
throw new ConvertException("Output property set error. output=" + output + ", property=" + outputProp + ", value=" + value, e);
}
}
}
return output;
}
use of jp.ossc.nimbus.beans.dataset.PropertySchemaDefineException in project nimbus by nimbus-org.
the class HttpResponseImplTest method testSetObject.
/**
* HttpResponseのストリームを設定、取得するテスト。
* <p>
* 条件:
* <ul>
* <li>XMLストリームデータをデータセットに変換して取得する</li>
* <li>次の内容をDataSetXMLConverterで変換する<BR>
* <PRE>
* <?xml version="1.0" encoding="UTF-8"?>
* <dataSet>
* <schema>
* <header name="TestHeader">
* :A,java.util.Date,
* "jp.ossc.nimbus.util.converter.DateFormatConverter{ConvertType=2;Format="yyyy-MM-DD"}",
* "jp.ossc.nimbus.util.converter.DateFormatConverter{ConvertType=1;Format="yyyy-MM-DD"}",
* "@value@ != null"
* :B,java.lang.String,,,
* </header>
* </schema>
* <header name="TestHeader"><A>2008-01-28</A><B>TestValue</B></header></dataSet>
* <PRE></li>
* <li>上記のストリームデータとコンバータをプロパティに設定して、<BR>
* HttpResponse#getObjectを実行</li>
* </ul>
* 確認:
* <ul>
* <li>正しい値が返ってくることを確認</li>
* </ul>
*/
public void testSetObject() {
try {
HttpResponseImpl res = new HttpResponseImpl();
// CharacterEncodingを設定しておく
res.headerMap = new HashMap();
String[] vals = new String[] { "application/xml;charset=Shift_JIS" };
res.headerMap.put("Content-Type", vals);
String inxml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<dataSet><schema><header name=\"TestHeader\">" + ":A,java.util.Date," + "\"jp.ossc.nimbus.util.converter.DateFormatConverter{ConvertType=2;Format=\"yyyy-MM-DD\"}\"," + "\"jp.ossc.nimbus.util.converter.DateFormatConverter{ConvertType=1;Format=\"yyyy-MM-DD\"}\"," + "\"@value@ != null\"\n:B,java.lang.String,,," + "</header></schema><header name=\"TestHeader\">" + "<A>2008-01-28</A><B>TestValue</B></header></dataSet>";
// 入力ストリームとコンバーターをプロパティにセット
InputStream is = new ByteArrayInputStream(inxml.getBytes());
res.setInputStream(is);
DataSetXMLConverter conv = new DataSetXMLConverter(DataSetXMLConverter.XML_TO_DATASET);
res.setStreamConverter(conv);
//
DataSet dataset = (DataSet) res.getObject();
assertEquals("TestHeader", dataset.getHeader("TestHeader").getName());
assertEquals(":A,java.util.Date," + "\"jp.ossc.nimbus.util.converter.DateFormatConverter{ConvertType=2;Format=\"yyyy-MM-DD\"}\"," + "\"jp.ossc.nimbus.util.converter.DateFormatConverter{ConvertType=1;Format=\"yyyy-MM-DD\"}\"," + "\"@value@ != null\"\n:B,java.lang.String,,,", dataset.getHeader("TestHeader").getSchema());
assertEquals("2008-01-28", dataset.getHeader("TestHeader").getFormatProperty("A"));
assertEquals("TestValue", dataset.getHeader("TestHeader").getProperty("B"));
} catch (PropertySchemaDefineException e) {
e.printStackTrace();
fail("例外発生");
} catch (ConvertException e) {
e.printStackTrace();
fail("例外発生");
}
}
Aggregations