关闭

XMLEncoder生成的xml文档的schema分析

1840人阅读 评论(0) 收藏 举报

以下文为基础,进行分析

Long Term Persistence of JavaBeans Components: XML Schema

http://java.sun.com/products/jfc/tsc/articles/persistence3/

 

1Basic Elements

每个xml以一个可选的

<?xml version="1.0" encoding="UTF-8" ?>

开头,接着是

<java version="1.4.0" class="java.beans.XMLDecoder">
  ...objects go here...
</java>

其中,version,class这个两个属性似乎已经不使用了。

<java>..</java>中间填充的是组成xml文档的objects,以被解码器readObject的顺序出现。

2Objects

每个object由创建它的一系列方法,按序排列来代表。

每个xml元素都代表一个方法调用。这些方法:一,创建了object(对应一个表达式expression);二,影响了object(对应一个语句statement)。

strings被看作特殊的expressions,标识符(identifiers)用来在object创建之后再引用object时使用的。

3Strings

xml中的原子表达式,

<string>Hello, World</string>

The '<' and '&' characters are represented by the &lt; and &amp; escape sequences.

4Expressions and Statements

expression是有返回值的方法调用,表示为:<object>

statement是没有返回值的方法调用。表示为:<void>

两个元素都有一个method属性,表示调用的方法。

 

<object>的class 属性指定作为一个静态方法的目标的class,构造函数表示为:一个名为new的静态方法。

当expression,statement进一步还包含expressions,则这些expressions作为前两者所代表的方法的参数。

所以,

<object class="javax.swing.JButton" method="new">
  <string>Press me</string>
</object>

代表的java 语句是

new JButton("Press me");

还可以表示为:

<object class="javax.swing.JButton">
  <string>Press me</string>
</object>

因为,new是默认的方法。

将影响一个object(由expression产生)的方法(一个statement)放到这个object里去,如下:

<object class="javax.swing.JButton">
  <void method="setText">
    <string>Hello, world</string>
  </void>
</object>

即为:

JButton b = new JButton();
b.setText("Hello, world");

 

If an expression should not be used as an argument to the enclosing method, it should be represented with the <void> tag. The result of an expression in a <void> tag is still evaluated and used by any objects it encloses.(待考)

嵌套expression,statement的能力减少了描述图形时使用的identifier的数目。

5标识符

有时,用<object>声明了一个对象之后,可能会在其他地方再次用到该对象,所以要定义identifier:

<object id="button1" class="javax.swing.JButton"/>

id是全局的,从声明起。

例子:

<object class="javax.swing.JPanel">
  <void method="add">
    <object id="button1" class="javax.swing.JButton"/>
  </void>
  <void method="add">
    <object class="javax.swing.JLabel">
      <void method="setLabelFor">
        <object idref="button1"/>
      </void>
    </object>
  </void>
</object>

即为:

JPanel panel1 = new JPanel();
JButton button1 = new JButton();
JLabel label1 = new JLabel();
panel1.add(button1);
panel1.add(label1);
label1.setLabelFor(button1);

id还可以这样用:

<object class="java.util.Date">
  <void id="now" method="getTime"/>
</object>

It allows an expression to be evaluated in the context of the enclosing instance, in this case defining the variable now as the value of the expression. It corresponds to the following Java code: (待考)

long now = new Date().getTime();

6缩写词Abbreviation

上述知识,足够使用XMLEncoder,下面是高级知识,可以让xml更简单:

primitives

The following tags represent both the primitive types and their corresponding wrapper classes:
<boolean>
<byte>
<char>
<short>
<int>
<long>
<float>
<double>

例如:

<object class="java.lang.Integer">
  <string>123</string>
</object>

可简写为:

<int>123</int>

null

<null/>

class

The <class> tag can be used to represent an instance of Class. For example,

<object class="java.lang.Class method="forName">
   <string>java.awt.event.ActionListener</string>
</object>
is shortened to

<class>java.awt.event.ActionListener</class>

which is equivalent to ActionListener.class.

Static Constants
(only in releases after 1.4.0 beta) (待考)

As of the release following 1.4.0 beta, the values of static constants may be written using the class and field attributes to specify the declaring class and field name of the constant, respectively. Thus

<void class="javax.swing.JTable" method="getField">
  <string>AUTO_RESIZE_OFF</string>
  <void id="Integer0" method="get">
    <null/>
  </void>
</void>
<object idref="Integer0"/>

is shortened to

<object class="javax.swing.JTable" field="AUTO_RESIZE_OFF"/>

property

以get,set开头的方法可以简写:

如,<void method="getText"/>
is shortened to:

<void property="text"/>

如,

<void method="setText">
  <string>Hello, world</string>
</void>
is shortened to:

<void property="text">
  <string>Hello, world</string>
</void>

Index

继承于java.util,list接口的get,set方法可以简写为:

<void method="get">
  <int>3</int>
<void>
is shortened to

<void index="3"/>
which corresponds to the following Java code:

Object o = aList.get(3);

又,

<void index="3">
  <string>Hello, world</string>
</void>
is equivalent to

<void method="set">
  <int>3</int>
  <string>Hello, world</string>
</void>
which corresponds to the following Java code:

aList.set(3, "Hello, world")

array

用<array>来表示数组,

<array class="java.awt.Component" length="3"/>
It corresponds to the following Java code:

Component[] a = new Component[3];

 

version1。4之后,还可以:

<array class="int">
  <int>123</int>
  <int>456</int>
</array>
represents the following Java code fragment:

int[] intArray = {123, 456};

which represents JTable.AUTO_RESIZE_OFF.

The Top Level environment

定义在<java></java>这个层次的一些高级属性,涉及到XMLDecoder(待考)

7DTD

http://java.sun.com/products/jfc/tsc/articles/persistence3/javabeans.dtd

 

一个例子:

http://java.sun.com/products/jfc/tsc/articles/persistence3/Browse.xml

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.0" class="java.beans.XMLDecoder">
  <object class="javax.swing.JFrame">
    <void id="JPanel0" property="contentPane">
      <void method="add">
        <object id="JLabel0" class="javax.swing.JLabel">
          <void property="text">
            <string>URL:</string>
          </void>
          <void property="location">
            <object class="java.awt.Point">
              <int>10</int>
              <int>10</int>
            </object>
          </void>
        </object>
      </void>
      <void method="add">
        <object id="JScrollPane0" class="javax.swing.JScrollPane">
          <void property="preferredSize">
            <object class="java.awt.Dimension">
              <int>500</int>
              <int>300</int>
            </object>
          </void>
          <void property="viewport">
            <void method="add">
              <object id="JEditorPane0" class="javax.swing.JEditorPane"/>
            </void>
          </void>
          <void property="location">
            <object class="java.awt.Point">
              <int>10</int>
              <int>40</int>
            </object>
          </void>
        </object>
      </void>
      <void method="add">
        <object id="JTextField0" class="javax.swing.JTextField">
          <void property="text">
            <string>file:///C:/</string>
          </void>
          <void method="addActionListener">
            <object class="java.beans.EventHandler" method="create">
              <class>java.awt.event.ActionListener</class>
              <object idref="JEditorPane0"/>
              <string>page</string>
              <string>source.text</string>
            </object>
          </void>
          <void property="location">
            <object class="java.awt.Point">
              <int>62</int>
              <int>10</int>
            </object>
          </void>
        </object>
      </void>
      <void property="layout">
        <object class="javax.swing.SpringLayout">
          <void method="putConstraint">
            <string>East</string>
            <object idref="JTextField0"/>
            <int>0</int>
            <string>East</string>
            <object idref="JScrollPane0"/>
          </void>
          <void method="putConstraint">
            <string>West</string>
            <object idref="JTextField0"/>
            <int>10</int>
            <string>East</string>
            <object idref="JLabel0"/>
          </void>
          <void method="putConstraint">
            <string>South</string>
            <object idref="JPanel0"/>
            <int>10</int>
            <string>South</string>
            <object idref="JScrollPane0"/>
          </void>
          <void method="putConstraint">
            <string>East</string>
            <object idref="JPanel0"/>
            <int>10</int>
            <string>East</string>
            <object idref="JScrollPane0"/>
          </void>
          <void method="putConstraint">
            <string>North</string>
            <object idref="JScrollPane0"/>
            <int>40</int>
            <string>North</string>
            <object idref="JPanel0"/>
          </void>
        </object>
      </void>
    </void>
    <void method="pack"/>
    <void property="visible">
      <boolean>true</boolean>
    </void>
  </object>
</java>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:861843次
    • 积分:11544
    • 等级:
    • 排名:第1340名
    • 原创:253篇
    • 转载:166篇
    • 译文:3篇
    • 评论:80条
    最新评论
    my webs