📄 tcufieldvalue.java
字号:
package com.jr81.source.dataset;
import java.io.ByteArrayInputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.jdom.Element;
import com.jr81.common.JrUtility;
import com.jr81.source.common.TcuDataType;
import com.jr81.source.stream.TcuBaseClassSTR;
import com.jr81.source.stream.TcuBaseItemsSTR;
public class TcuFieldValue extends TcuBaseClassSTR {
private String FieldName="";
private byte[] FieldValue=null;
private int DataType=TcuDataType.String;
public TcuFieldValue() {
super("FV");
// TODO 自动生成构造函数存根
}
public TcuFieldValue(TcuBaseItemsSTR stritems) {
super("FV",stritems);
// TODO 自动生成构造函数存根
}
public boolean Clear() {
// TODO 自动生成方法存根
setFieldName("");
setFieldValue("");
DataType=TcuDataType.String;
return true;
}
public boolean CreateStrItems() {
// TODO 自动生成方法存根
StrItems.Clear();
StrItems.AddItem("N",getFieldName());
StrItems.AddItem("V",getFieldValue(),0);
return true;
}
public boolean ParseStrItems() {
// TODO 自动生成方法存根
setFieldName(StrItems.GetItem("N"));
setFieldValue(StrItems.GetItemByteArray("V"));
return true;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
try{
TcuFieldValue FV=new TcuFieldValue();
FV.Clear();
FV.setFieldName("msg");
FV.setFieldValue("My name is Sanlen!".getBytes());
FV.SaveToFile("C:\\FV.Txt");
String xml= FV.SaveToXMLWithHead();
System.out.println(xml);
//TcuFieldData FD2=new TcuFieldData();
//FD2.LoadFromFile("C:\\FD.XML");
//System.out.println(FD2.getDescription());
TcuFieldValue FV2=new TcuFieldValue();
//FD2.Assign(FD);
//FV2.LoadFromFile("C:\\FV.Txt");
ByteArrayInputStream bi=new ByteArrayInputStream(xml.getBytes());
FV2.LoadFromXMLWithHead(bi);
System.out.println(new String(FV2.getFieldValue()));
}catch(Exception e){
e.printStackTrace();
}
}
/**
* @return 返回 fieldName。
*/
public String getFieldName() {
return FieldName;
}
/**
* @param fieldName 要设置的 fieldName。
*/
public void setFieldName(String fieldName) {
FieldName = fieldName;
}
/**
* @return 返回 fieldValue。
*/
public byte[] getFieldValue() {
return FieldValue;
}
/**
* @param fieldValue 要设置的 fieldValue。
*/
public void setFieldValue(byte[] fieldValue) {
if (fieldValue!=null){
FieldValue = fieldValue;
}
}
public void setFieldValue(String fieldValue) {
if (fieldValue!=null){
FieldValue = fieldValue.getBytes();
}
}
public String AsString(){
return new String(FieldValue);
}
public boolean AsBoolean(){
if (getFieldValue().equals("1")){
return true;
}
else
return false;
}
public Byte AsByte(){
if (getFieldValue().equals("1")){
return Byte.valueOf("1");
}
else
return Byte.valueOf("0");
}
public byte Asbyte(){
if (getFieldValue().equals("1")){
return 1;
}
else
return 0;
}
public Integer AsInteger(){
try{
return Integer.valueOf(new String(getFieldValue()));
}
catch(Exception e){
return null ;
}
}
public int Asinteger(){
try{
return Integer.parseInt(new String(getFieldValue()));
}
catch(Exception e){
return 0 ;
}
}
public Float AsFloat(){
try{
return Float.valueOf(new String(getFieldValue()));
}
catch(Exception e){
return null ;
}
}
public float Asfloat(){
try{
return Float.parseFloat(new String(getFieldValue()));
}
catch(Exception e){
return 0 ;
}
}
public Double AsDouble(){
try{
return Double.valueOf(new String(getFieldValue()));
}
catch(Exception e){
return null ;
}
}
public double Asdouble(){
try{
return Double.parseDouble(new String(getFieldValue()));
}
catch(Exception e){
return 0 ;
}
}
public Long AsLong(){
try{
return Long.valueOf(new String(getFieldValue()));
}
catch(Exception e){
return null ;
}
}
public long Aslong(){
try{
return Long.parseLong(new String(getFieldValue()));
}
catch(Exception e){
return 0 ;
}
}
public byte[] AsByteArray(){
try{
return getFieldValue();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
public Date AsDateTime() {
SimpleDateFormat dateFormatter =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
if (getFieldValue().equals("")){
return null;
}
return dateFormatter.parse(new String(getFieldValue()));
} catch (ParseException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
return null;
}
}
public Date AsDate() {
SimpleDateFormat dateFormatter =new SimpleDateFormat("yyyy-MM-dd");
try {
if (getFieldValue().equals("")){
return null;
}
return dateFormatter.parse(new String(getFieldValue()));
} catch (ParseException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
return null;
}
}
public Date AsTime(){
SimpleDateFormat dateFormatter =new SimpleDateFormat("HH:mm:ss");
try {
if (getFieldValue().equals("")){
return null;
}
return dateFormatter.parse(new String(getFieldValue()));
} catch (ParseException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
return null;
}
}
public String SaveToXML(){
return GetXMLItems();
}
public String GetXMLItems(){
String xml=null;
if (DataType==TcuDataType.ByteArray||DataType==TcuDataType.Text){
xml="<"+FieldName+" B64='1'>"+JrUtility.getBASE64(new String(FieldValue))+"</"+FieldName+">";
}else{
xml="<"+FieldName+" B64='0'>"+(new String(FieldValue))+"</"+FieldName+">";
}
return xml;
}
public boolean LoadFromXML(Element xml) {
Element root=(Element)xml.getChildren().get(0);
return ParseXMLItems(root);
}
public boolean ParseXMLItems(Element xml){
if (xml==null) return false;
FieldName=xml.getName();
if (xml.getAttributeValue("B64").equals("1")){
FieldValue=JrUtility.getFromBASE64(xml.getTextTrim()).getBytes();
}
else {
FieldValue=xml.getTextTrim().getBytes();
}
return true;
}
/**
* @return the dataType
*/
public int getDataType() {
return DataType;
}
/**
* @param dataType the dataType to set
*/
public void setDataType(int dataType) {
DataType = dataType;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -