云自無心水自閑

          天平山上白云泉,云自無心水自閑。何必奔沖山下去,更添波浪向人間!
          posts - 288, comments - 524, trackbacks - 0, articles - 6
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

           

           

          http://www.w3schools.com/schema/schema_howto.asp


          What is an XML Schema?

          The purpose of an XML Schema is to define the legal building blocks of an XML document, just like a DTD.

          An XML Schema:

          • defines elements that can appear in a document
          • defines attributes that can appear in a document
          • defines which elements are child elements
          • defines the order of child elements
          • defines the number of child elements
          • defines whether an element is empty or can include text
          • defines data types for elements and attributes
          • defines default and fixed values for elements and attributes

           

          Well-Formed is not Enough

          A well-formed XML document is a document that conforms to the XML syntax rules, like:

          • it must begin with the XML declaration
          • it must have one unique root element
          • start-tags must have matching end-tags
          • elements are case sensitive
          • all elements must be closed
          • all elements must be properly nested
          • all attribute values must be quoted
          • entities must be used for special characters

          The <schema> element may contain some attributes. A schema declaration often looks something like this:

          <?xml version="1.0"?>
          <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
          targetNamespace="http://www.w3schools.com"
          xmlns="http://www.w3schools.com"
          elementFormDefault="qualified">
          ...
          ...
          </xs:schema>

          The following fragment:

          xmlns:xs="http://www.w3.org/2001/XMLSchema"

          indicates that the elements and data types used in the schema come from the "http://www.w3.org/2001/XMLSchema" namespace. It also specifies that the elements and data types that come from the "http://www.w3.org/2001/XMLSchema" namespace should be prefixed with xs:

          This fragment:

          targetNamespace="http://www.w3schools.com"

          indicates that the elements defined by this schema (note, to, from, heading, body.) come from the "http://www.w3schools.com" namespace.

          This fragment:

          xmlns="http://www.w3schools.com"

          indicates that the default namespace is "http://www.w3schools.com".

          This fragment:

          elementFormDefault="qualified"

          indicates that any elements used by the XML instance document which were declared in this schema must be namespace qualified.

           

          What is a Simple Element?

          A simple element is an XML element that can contain only text. It cannot contain any other elements or attributes.

           

          Defining a Simple Element

          The syntax for defining a simple element is:

          <xs:element name="xxx" type="yyy"/>

          XML Schema has a lot of built-in data types. The most common types are:

          • xs:string
          • xs:decimal
          • xs:integer
          • xs:boolean
          • xs:date
          • xs:time

          Default and Fixed Values for Simple Elements

          Simple elements may have a default value OR a fixed value specified.

          A default value is automatically assigned to the element when no other value is specified.

          In the following example the default value is "red":

          <xs:element name="color" type="xs:string" default="red"/>

          A fixed value is also automatically assigned to the element, and you cannot specify another value.

          In the following example the fixed value is "red":

          <xs:element name="color" type="xs:string" fixed="red"/>

           

          How to Define an Attribute?

          The syntax for defining an attribute is:

          <xs:attribute name="xxx" type="yyy"/>

           

           

           

           

          Restrictions are used to define acceptable values for XML elements or attributes. Restrictions on XML elements are called facets.

          Restrictions on Values

          The following example defines an element called "age" with a restriction. The value of age cannot be lower than 0 or greater than 120:

          <xs:element name="age">
          <xs:simpleType>
          <xs:restriction base="xs:integer">
          <xs:minInclusive value="0"/>
          <xs:maxInclusive value="120"/>
          </xs:restriction>
          </xs:simpleType>
          </xs:element> 


          Restrictions on a Set of Values

          To limit the content of an XML element to a set of acceptable values, we would use the enumeration constraint.

          The example below defines an element called "car" with a restriction. The only acceptable values are: Audi, Golf, BMW:

          <xs:element name="car">
          <xs:simpleType>
          <xs:restriction base="xs:string">
          <xs:enumeration value="Audi"/>
          <xs:enumeration value="Golf"/>
          <xs:enumeration value="BMW"/>
          </xs:restriction>
          </xs:simpleType>
          </xs:element> 

          The example above could also have been written like this:

          <xs:element name="car" type="carType"/>
          <xs:simpleType name="carType">
          <xs:restriction base="xs:string">
          <xs:enumeration value="Audi"/>
          <xs:enumeration value="Golf"/>
          <xs:enumeration value="BMW"/>
          </xs:restriction>
          </xs:simpleType>

          Note: In this case the type "carType" can be used by other elements because it is not a part of the "car" element.


          Restrictions on a Series of Values

          To limit the content of an XML element to define a series of numbers or letters that can be used, we would use the pattern constraint.

          The example below defines an element called "letter" with a restriction. The only acceptable value is ONE of the LOWERCASE letters from a to z:

          <xs:element name="letter">
          <xs:simpleType>
          <xs:restriction base="xs:string">
          <xs:pattern value="[a-z]"/>
          </xs:restriction>
          </xs:simpleType>
          </xs:element> 

          The next example defines an element called "initials" with a restriction. The only acceptable value is THREE of the UPPERCASE letters from a to z:

          <xs:element name="initials">
          <xs:simpleType>
          <xs:restriction base="xs:string">
          <xs:pattern value="[A-Z][A-Z][A-Z]"/>
          </xs:restriction>
          </xs:simpleType>
          </xs:element> 

          The next example also defines an element called "initials" with a restriction. The only acceptable value is THREE of the LOWERCASE OR UPPERCASE letters from a to z:

          <xs:element name="initials">
          <xs:simpleType>
          <xs:restriction base="xs:string">
          <xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/>
          </xs:restriction>
          </xs:simpleType>
          </xs:element> 

          The next example defines an element called "choice" with a restriction. The only acceptable value is ONE of the following letters: x, y, OR z:

          <xs:element name="choice">
          <xs:simpleType>
          <xs:restriction base="xs:string">
          <xs:pattern value="[xyz]"/>
          </xs:restriction>
          </xs:simpleType>
          </xs:element> 

          The next example defines an element called "prodid" with a restriction. The only acceptable value is FIVE digits in a sequence, and each digit must be in a range from 0 to 9:

          <xs:element name="prodid">
          <xs:simpleType>
          <xs:restriction base="xs:integer">
          <xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/>
          </xs:restriction>
          </xs:simpleType>
          </xs:element> 


          Other Restrictions on a Series of Values

          The example below defines an element called "letter" with a restriction. The acceptable value is zero or more occurrences of lowercase letters from a to z:

          <xs:element name="letter">
          <xs:simpleType>
          <xs:restriction base="xs:string">
          <xs:pattern value="([a-z])*"/>
          </xs:restriction>
          </xs:simpleType>
          </xs:element> 

          The next example also defines an element called "letter" with a restriction. The acceptable value is one or more pairs of letters, each pair consisting of a lower case letter followed by an upper case letter. For example, "sToP" will be validated by this pattern, but not "Stop" or "STOP" or "stop":

          <xs:element name="letter">
          <xs:simpleType>
          <xs:restriction base="xs:string">
          <xs:pattern value="([a-z][A-Z])+"/>
          </xs:restriction>
          </xs:simpleType>
          </xs:element> 

          The next example defines an element called "gender" with a restriction. The only acceptable value is male OR female:

          <xs:element name="gender">
          <xs:simpleType>
          <xs:restriction base="xs:string">
          <xs:pattern value="male|female"/>
          </xs:restriction>
          </xs:simpleType>
          </xs:element> 

          The next example defines an element called "password" with a restriction. There must be exactly eight characters in a row and those characters must be lowercase or uppercase letters from a to z, or a number from 0 to 9:

          <xs:element name="password">
          <xs:simpleType>
          <xs:restriction base="xs:string">
          <xs:pattern value="[a-zA-Z0-9]{8}"/>
          </xs:restriction>
          </xs:simpleType>
          </xs:element> 


          Restrictions on Whitespace Characters

          To specify how whitespace characters should be handled, we would use the whiteSpace constraint.

          This example defines an element called "address" with a restriction. The whiteSpace constraint is set to "preserve", which means that the XML processor WILL NOT remove any white space characters:

          <xs:element name="address">
          <xs:simpleType>
          <xs:restriction base="xs:string">
          <xs:whiteSpace value="preserve"/>
          </xs:restriction>
          </xs:simpleType>
          </xs:element> 

          This example also defines an element called "address" with a restriction. The whiteSpace constraint is set to "replace", which means that the XML processor WILL REPLACE all white space characters (line feeds, tabs, spaces, and carriage returns) with spaces:

          <xs:element name="address">
          <xs:simpleType>
          <xs:restriction base="xs:string">
          <xs:whiteSpace value="replace"/>
          </xs:restriction>
          </xs:simpleType>
          </xs:element> 

          This example also defines an element called "address" with a restriction. The whiteSpace constraint is set to "collapse", which means that the XML processor WILL REMOVE all white space characters (line feeds, tabs, spaces, carriage returns are replaced with spaces, leading and trailing spaces are removed, and multiple spaces are reduced to a single space):

          <xs:element name="address">
          <xs:simpleType>
          <xs:restriction base="xs:string">
          <xs:whiteSpace value="collapse"/>
          </xs:restriction>
          </xs:simpleType>
          </xs:element> 


          Restrictions on Length

          To limit the length of a value in an element, we would use the length, maxLength, and minLength constraints.

          This example defines an element called "password" with a restriction. The value must be exactly eight characters:

          <xs:element name="password">
          <xs:simpleType>
          <xs:restriction base="xs:string">
          <xs:length value="8"/>
          </xs:restriction>
          </xs:simpleType>
          </xs:element> 

          This example defines another element called "password" with a restriction. The value must be minimum five characters and maximum eight characters:

          <xs:element name="password">
          <xs:simpleType>
          <xs:restriction base="xs:string">
          <xs:minLength value="5"/>
          <xs:maxLength value="8"/>
          </xs:restriction>
          </xs:simpleType>
          </xs:element> 


          Restrictions for Datatypes

          Constraint Description
          enumeration Defines a list of acceptable values
          fractionDigits Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero
          length Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero
          maxExclusive Specifies the upper bounds for numeric values (the value must be less than this value)
          maxInclusive Specifies the upper bounds for numeric values (the value must be less than or equal to this value)
          maxLength Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero
          minExclusive Specifies the lower bounds for numeric values (the value must be greater than this value)
          minInclusive Specifies the lower bounds for numeric values (the value must be greater than or equal to this value)
          minLength Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero
          pattern Defines the exact sequence of characters that are acceptable
          totalDigits Specifies the exact number of digits allowed. Must be greater than zero
          whiteSpace Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled

           

           

           

          What is a Complex Element?

          A complex element is an XML element that contains other elements and/or attributes.

          There are four kinds of complex elements:

          • empty elements
          • elements that contain only other elements
          • elements that contain only text
          • elements that contain both other elements and text

          How to Define a Complex Element

          Look at this complex XML element, "employee", which contains only other elements:

          <employee>
          <firstname>John</firstname>
          <lastname>Smith</lastname>
          </employee>

          We can define a complex element in an XML Schema two different ways:

          1. The "employee" element can be declared directly by naming the element, like this:

          <xs:element name="employee">
          <xs:complexType>
          <xs:sequence>
          <xs:element name="firstname" type="xs:string"/>
          <xs:element name="lastname" type="xs:string"/>
          </xs:sequence>
          </xs:complexType>
          </xs:element>

          If you use the method described above, only the "employee" element can use the specified complex type. Note that the child elements, "firstname" and "lastname", are surrounded by the <sequence> indicator. This means that the child elements must appear in the same order as they are declared. You will learn more about indicators in the XSD Indicators chapter.

          2. The "employee" element can have a type attribute that refers to the name of the complex type to use:

          <xs:element name="employee" type="personinfo"/>
          <xs:complexType name="personinfo">
          <xs:sequence>
          <xs:element name="firstname" type="xs:string"/>
          <xs:element name="lastname" type="xs:string"/>
          </xs:sequence>
          </xs:complexType>

           

          You can also base a complex element on an existing complex element and add some elements, like this:

          <xs:element name="employee" type="fullpersoninfo"/>
          <xs:complexType name="personinfo">
          <xs:sequence>
          <xs:element name="firstname" type="xs:string"/>
          <xs:element name="lastname" type="xs:string"/>
          </xs:sequence>
          </xs:complexType>
          <xs:complexType name="fullpersoninfo">
          <xs:complexContent>
          <xs:extension base="personinfo">
          <xs:sequence>
          <xs:element name="address" type="xs:string"/>
          <xs:element name="city" type="xs:string"/>
          <xs:element name="country" type="xs:string"/>
          </xs:sequence>
          </xs:extension>
          </xs:complexContent>
          </xs:complexType>

           

          Complex Types with Mixed Content

          An XML element, "letter", that contains both text and other elements:

          <letter>
          Dear Mr.<name>John Smith</name>.
          Your order <orderid>1032</orderid>
          will be shipped on <shipdate>2001-07-13</shipdate>.
          </letter>

          The following schema declares the "letter" element:

          <xs:element name="letter">
          <xs:complexType mixed="true">
          <xs:sequence>
          <xs:element name="name" type="xs:string"/>
          <xs:element name="orderid" type="xs:positiveInteger"/>
          <xs:element name="shipdate" type="xs:date"/>
          </xs:sequence>
          </xs:complexType>
          </xs:element>

          Note: To enable character data to appear between the child-elements of "letter", the mixed attribute must be set to "true". The <xs:sequence> tag means that the elements defined (name, orderid and shipdate) must appear in that order inside a "letter" element.

           

          We can control HOW elements are to be used in documents with indicators.


          Indicators

          There are seven indicators:

          Order indicators:

          • All
          • Choice
          • Sequence

          Occurrence indicators:

          • maxOccurs
          • minOccurs

          Group indicators:

          • Group name
          • attributeGroup name

          All Indicator

          The <all> indicator specifies that the child elements can appear in any order, and that each child element must occur only once:

          <xs:element name="person">
          <xs:complexType>
          <xs:all>
          <xs:element name="firstname" type="xs:string"/>
          <xs:element name="lastname" type="xs:string"/>
          </xs:all>
          </xs:complexType>
          </xs:element>

          Note: When using the <all> indicator you can set the <minOccurs> indicator to 0 or 1 and the <maxOccurs> indicator can only be set to 1

           

          Choice Indicator

          The <choice> indicator specifies that either one child element or another can occur:

          <xs:element name="person">
          <xs:complexType>
          <xs:choice>
          <xs:element name="employee" type="employee"/>
          <xs:element name="member" type="member"/>
          </xs:choice>
          </xs:complexType>
          </xs:element>

           

          Sequence Indicator

          The <sequence> indicator specifies that the child elements must appear in a specific order:

          <xs:element name="person">
          <xs:complexType>
          <xs:sequence>
          <xs:element name="firstname" type="xs:string"/>
          <xs:element name="lastname" type="xs:string"/>
          </xs:sequence>
          </xs:complexType>
          </xs:element>

           

          Occurrence Indicators

          Occurrence indicators are used to define how often an element can occur.

          Note: For all "Order" and "Group" indicators (any, all, choice, sequence, group name, and group reference) the default value for maxOccurs and minOccurs is 1.

           

          maxOccurs Indicator

          The <maxOccurs> indicator specifies the maximum number of times an element can occur:

          <xs:element name="person">
          <xs:complexType>
          <xs:sequence>
          <xs:element name="full_name" type="xs:string"/>
          <xs:element name="child_name" type="xs:string" maxOccurs="10"/>
          </xs:sequence>
          </xs:complexType>
          </xs:element>

          The example above indicates that the "child_name" element can occur a minimum of one time (the default value for minOccurs is 1) and a maximum of ten times in the "person" element.

           

          minOccurs Indicator

          The <minOccurs> indicator specifies the minimum number of times an element can occur:

          <xs:element name="person">
          <xs:complexType>
          <xs:sequence>
          <xs:element name="full_name" type="xs:string"/>
          <xs:element name="child_name" type="xs:string"
          maxOccurs="10" minOccurs="0"/>
          </xs:sequence>
          </xs:complexType>
          </xs:element>

          The example above indicates that the "child_name" element can occur a minimum of zero times and a maximum of ten times in the "person" element.

          Tip: To allow an element to appear an unlimited number of times, use the maxOccurs="unbounded" statement

           

          Group Indicators

          Group indicators are used to define related sets of elements.

          Element Groups

          Element groups are defined with the group declaration, like this:

          <xs:group name="groupname">
          ...
          </xs:group>

          You must define an all, choice, or sequence element inside the group declaration. The following example defines a group named "persongroup", that defines a group of elements that must occur in an exact sequence:

          <xs:group name="persongroup">
          <xs:sequence>
          <xs:element name="firstname" type="xs:string"/>
          <xs:element name="lastname" type="xs:string"/>
          <xs:element name="birthday" type="xs:date"/>
          </xs:sequence>
          </xs:group>

          After you have defined a group, you can reference it in another definition, like this:

          <xs:group name="persongroup">
          <xs:sequence>
          <xs:element name="firstname" type="xs:string"/>
          <xs:element name="lastname" type="xs:string"/>
          <xs:element name="birthday" type="xs:date"/>
          </xs:sequence>
          </xs:group>
          <xs:element name="person" type="personinfo"/>
          <xs:complexType name="personinfo">
          <xs:sequence>
          <xs:group ref="persongroup"/>
          <xs:element name="country" type="xs:string"/>
          </xs:sequence>
          </xs:complexType>

          Attribute Groups

          Attribute groups are defined with the attributeGroup declaration, like this:

          <xs:attributeGroup name="groupname">
          ...
          </xs:attributeGroup>

          The following example defines an attribute group named "personattrgroup":

          <xs:attributeGroup name="personattrgroup">
          <xs:attribute name="firstname" type="xs:string"/>
          <xs:attribute name="lastname" type="xs:string"/>
          <xs:attribute name="birthday" type="xs:date"/>
          </xs:attributeGroup>

          After you have defined an attribute group, you can reference it in another definition, like this:

          <xs:attributeGroup name="personattrgroup">
          <xs:attribute name="firstname" type="xs:string"/>
          <xs:attribute name="lastname" type="xs:string"/>
          <xs:attribute name="birthday" type="xs:date"/>
          </xs:attributeGroup>
          <xs:element name="person">
          <xs:complexType>
          <xs:attributeGroup ref="personattrgroup"/>
          </xs:complexType>
          </xs:element>

           

          The <any> element enables us to extend the XML document with elements not specified by the schema!


          The <any> Element

          The <any> element enables us to extend the XML document with elements not specified by the schema.

          The following example is a fragment from an XML schema called "family.xsd". It shows a declaration for the "person" element. By using the <any> element we can extend (after <lastname>) the content of "person" with any element:

          <xs:element name="person">
          <xs:complexType>
          <xs:sequence>
          <xs:element name="firstname" type="xs:string"/>
          <xs:element name="lastname" type="xs:string"/>
          <xs:any minOccurs="0"/>
          </xs:sequence>
          </xs:complexType>
          </xs:element>

          Now we want to extend the "person" element with a "children" element. In this case we can do so, even if the author of the schema above never declared any "children" element.

          Look at this schema file, called "children.xsd":

          <?xml version="1.0" encoding="ISO-8859-1"?>
          <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
          targetNamespace="http://www.w3schools.com"
          xmlns="http://www.w3schools.com"
          elementFormDefault="qualified">
          <xs:element name="children">
          <xs:complexType>
          <xs:sequence>
          <xs:element name="childname" type="xs:string"
          maxOccurs="unbounded"/>
          </xs:sequence>
          </xs:complexType>
          </xs:element>
          </xs:schema>

          The XML file below (called "Myfamily.xml"), uses components from two different schemas; "family.xsd" and "children.xsd":

          <?xml version="1.0" encoding="ISO-8859-1"?>
          <persons xmlns="http://www.microsoft.com"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:SchemaLocation="http://www.microsoft.com family.xsd
          http://www.w3schools.com children.xsd">
          <person>
          <firstname>Hege</firstname>
          <lastname>Refsnes</lastname>
          <children>
          <childname>Cecilie</childname>
          </children>
          </person>
          <person>
          <firstname>Stale</firstname>
          <lastname>Refsnes</lastname>
          </person>
          </persons>

          The XML file above is valid because the schema "family.xsd" allows us to extend the "person" element with an optional element after the "lastname" element.

          The <any> and <anyAttribute> elements are used to make EXTENSIBLE documents! They allow documents to contain additional elements that are not declared in the main XML schema.

           

          The <anyAttribute> element enables us to extend the XML document with attributes not specified by the schema!


          The <anyAttribute> Element

          The <anyAttribute> element enables us to extend the XML document with attributes not specified by the schema.

          The following example is a fragment from an XML schema called "family.xsd". It shows a declaration for the "person" element. By using the <anyAttribute> element we can add any number of attributes to the "person" element:

          <xs:element name="person">
          <xs:complexType>
          <xs:sequence>
          <xs:element name="firstname" type="xs:string"/>
          <xs:element name="lastname" type="xs:string"/>
          </xs:sequence>
          <xs:anyAttribute/>
          </xs:complexType>
          </xs:element>

          Now we want to extend the "person" element with a "gender" attribute. In this case we can do so, even if the author of the schema above never declared any "gender" attribute.

          Look at this schema file, called "attribute.xsd":

          <?xml version="1.0" encoding="ISO-8859-1"?>
          <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
          targetNamespace="http://www.w3schools.com"
          xmlns="http://www.w3schools.com"
          elementFormDefault="qualified">
          <xs:attribute name="gender">
          <xs:simpleType>
          <xs:restriction base="xs:string">
          <xs:pattern value="male|female"/>
          </xs:restriction>
          </xs:simpleType>
          </xs:attribute>
          </xs:schema>

          The XML file below (called "Myfamily.xml"), uses components from two different schemas; "family.xsd" and "attribute.xsd":

          <?xml version="1.0" encoding="ISO-8859-1"?>
          <persons xmlns="http://www.microsoft.com"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:SchemaLocation="http://www.microsoft.com family.xsd
          http://www.w3schools.com attribute.xsd">
          <person gender="female">
          <firstname>Hege</firstname>
          <lastname>Refsnes</lastname>
          </person>
          <person gender="male">
          <firstname>Stale</firstname>
          <lastname>Refsnes</lastname>
          </person>
          </persons>

          The XML file above is valid because the schema "family.xsd" allows us to add an attribute to the "person" element.

          The <any> and <anyAttribute> elements are used to make EXTENSIBLE documents! They allow documents to contain additional elements that are not declared in the main XML schema.

          posted @ 2007-09-10 14:15 云自無心水自閑 閱讀(1018) | 評論 (0)編輯 收藏


          ViewStack的大小是由其子組件的大小決定的,而ViewStack并不會在改變活動子組件的時候自動resize。
          只有使用以下方法來控制ViewStack的大小。
          1. 使用相同的固定值明確指定所有子組件的大小
          2. 使用相同的比例值指定所有子組件的大小
          3. 將ViewStack的width和height的值設置為一個固定或者值。

          上述3種方法是Adobe官方文檔提供的。但是這3種方法不能解決ViewStack子組件大小不一致,ViewStack不能自動調整的問題。
          我最后是使用ActionScript動態解決的。
          在更換ViewStack的active child之間,首先設置viewStack的大小。

          <mx:ViewStack id="appStack">
                  <mx:VBox id="v1"/>
                  <mx:VBox id="v2"/>
          </mx:ViewStack>

          public function changeChild() : void {
                  appStack.width = 200;
                  appStack.height = 200;
                  appStack.selectChild = v1;
          }

          我錯了,使用ViewStack的resizeToContent是最好的解決辦法。

          posted @ 2007-08-24 20:26 云自無心水自閑 閱讀(1675) | 評論 (0)編輯 收藏

          Spring的關鍵之一就是容器,在Spring中主要是兩種容器:一個是BeanFactory,一個是ApplicationContext。
          容器的作用是,管理所有的bean的生命周期,從創建bean的實例開始,到最后bean的消亡。
          這兩種容器的作用基本相同,但是Application Context是BeanFactory的子類,增加了一些功能,所以更為強大一些,主要體現在3個方面:
          1. 能解析文本消息,提供文本的國際化(I18N)。
          2. ApplicationContext提供一種通用的方法來加載文件資源,比如:圖像文件。
          3. 能夠發布事件到注冊的監聽器。
          所以,在大多數應用中,都使用Application Context。

          ApplicationContext接口的實現類有很多,但常用的有3個:
          1. ClassPathXmlApplicationContext
          2. FileSystemXmlApplicationContext
          3. XmlWebApplicationContext

          ApplicationContext和BeanFactory的另一個區別在于對singleton bean的加載上。Bean Factory延遲加載所有的bean直到getBean()的調用,而ApplicationContext稍微智能一些,預先加載所有的singleton bean。

          posted @ 2007-08-23 00:12 云自無心水自閑 閱讀(628) | 評論 (1)編輯 收藏

          1. 安裝Mysql,Maven等等。這些在網上都有詳細的說明。
          2. 我更改了Maven的Repository的路徑,缺省是放在C:\document and settings\<user name>\.m2\repository目錄下,我覺得放在C:下不好,所以更換了路徑。
          打開~maven/conf/setting.xml,修改<localRepository>的值。

          3. 使用Maven下載appfuse
          我使用的是struts所以,使用的命令是:
          mvn archetype:create -DarchetypeGroupId=org.appfuse -DarchetypeArtifactId=appfuse-modular-struts -DremoteRepositories=http://static.appfuse.org/repository -DarchetypeVersion=2.0-m5 -DgroupId=com.mycompany.app -DartifactId=appfuse
          這里,我把下載的目錄名改為了appfuse,在appfuse.org的quick start中是使用myproject的。

          4. 運行的過程中,會出錯,我在兩臺機器上都遇到了錯誤。
          關系不大。可以繼續進行。

          5. 下載源代碼。
          我是在Eclipse中使用Subversion下載的,可以使用mvn appfuse:full-source,但是只能下載到web下的代碼,service, data等部分的代碼就沒有了。
          Svn的Repository的地址是:https://appfuse.dev.java.net/svn/appfuse

          6. Java Source Code已經盡在掌握了,只是還分布在不同的目錄里。
          分別是在:data,service,webapp,都在main\java目錄下。

          7. 開始獲取jsp,配置文件等。
          首先cd ~maven\repo\org\hibernate\jtidy\r8-20060801
          edit jtidy-r8-20060801.pom
          去掉一個重復的 <licenses> 標簽.

          8. 去掉mysql的root用戶的密碼,
          update user set password=password('') where user='root'; flush privileges;

          9. cd appfuse
          mvn integration-test
          在appfuse-snapshot1.0目錄下,把jsp、image,js,css等全部復制過來
          另外,還有很多配置文件,象applicationContext-dao.xml等等。
          還有一個,就是library了。其中有一個要注意的是ehcache需要使用1.3.0, 如果使用1.2.X,會報
          javax.servlet.ServletException: Failure when attempting to set Content-Encoding: gzip
          這個錯誤。

          10. 我是使用Eclipse的Tomcat插件的,因此,建立了一個Tomcat project
          把Java源文件復制到web-inf\src下,
          org.appfuse.dao
          org.appfuse.model
          org.appfuse.service
          org.appfuse.util
          org.appfuse.webapp(Webapp目錄下)
          另外,
          common
          decorators
          images
          scripts
          styles
          template
          403.jsp
          404.jsp
          index.jsp.......
          還有web-inf目錄下的:
          數10個配置文件和lib目錄下數10個jar文件

          11. 啟動Tomcat插件,在瀏覽器中瀏覽:http://localhost:8080/appfuse/index.jsp
          用戶:admin 密碼:admin
          OK.

          之所以,這么麻煩的折騰,主要是想在appfuse應用中,打斷點,進行逐步跟蹤。充分了解認知演習appfuse的細節。

          posted @ 2007-08-08 22:37 云自無心水自閑 閱讀(6158) | 評論 (6)編輯 收藏

           

          最近做的一個程序是用Swing的,要求能夠根據不同的分辨率自動調整界面上所有組件的大小。也就是說不是寫死是1024×768,并且字體也需要根據大小自動變化。
          我使用的工具是Netbeans,為實現動態變化,我使用了GridBagLayout。首先,新建一個類,繼承JPanel。然后設置JPanel的Layout為GridBagLayout。當然,根據情況,可以和Html中的表格一樣,Panel里面嵌套Panel,要點是每個Panel的Layout都設置為GridBagLayout(使用其他的Layout也可以實現這樣的功能,但是個人感覺GridBagLayout最容易控制和使用)。
          Layout的設置只是第一步,缺省情況下,GridBagLayout會把Panel中所有的組件排成一行,從左到右逐個排放。這時候,就要使用Customize Layout(定制布局)的功能,點擊后,會再彈出一個窗口
          在新的窗口中,可以拖動Panel里面的組件,象表格一樣,組織安放所有的組件,相當方便。
          這些步驟完成后,重要的兩個屬性是,填充(Fill),建議把所有組件的Fill屬性,都選成Both,也就是水平和垂直方向都延伸填充。這樣,Panel里面的所有組件會平鋪開來,占滿Panel的所有空間。那么,如何調整這些組件的大小呢?需要使用weightx和weighty這兩個屬性。這兩屬性的值使用0.0~1.0之間的小數,數越大,組件所占據的空間越大。
          通過以上的設置,就可以實現組件大小隨著Panel大小的變化而變化了。
          那么,又如何實現字體的變化呢?這個只能通過編程實現了。但是initComponents函數里的代碼都是自動生成的,如何添加自定義的代碼呢。點擊屬性面板里的字體屬性后面的小方框,在彈出的對話框里,點擊高級按鈕,勾選“生成初始化后的代碼”,然后在文本框里,輸入代碼,這段自定義代碼,會在每次自動生成代碼的時候,添加到initComponents函數中。

          posted @ 2007-07-27 20:16 云自無心水自閑 閱讀(21819) | 評論 (4)編輯 收藏

           

          SubVersion的官方網站中有兩個版本可供下載,一個是for apaache2.0.X的,一個是for apache2.2.X的,第一個是可執行文件,在已經安裝了Apache2.0.X的機器上運行后,會自動在httpd.conf文件中添加相應的內容,并自動復制模塊和動態鏈接庫到相應目錄。
          而for Apache2.2.X的那個是一個壓縮包,需要手工在apache的httpd.conf中添加相應內容,主要是啟用DAV,并增加一個location。這些步驟在網上都可以搜索得到,但是我發現,網上的很多文章都忽略了將動態鏈接庫復制到apache的bin目錄下這一個步驟,這樣會導致apache http server無法啟動。
          需要復制的文件是:
          libdb44.dll
          libeay32.dll
          ssleay32.dll

          這些文件可以復制到D:\Program Files\Apache Software Foundation\Apache2.2\bin目錄(也就是apache安裝目錄的bin目錄)下。

          posted @ 2007-07-27 19:50 云自無心水自閑 閱讀(1583) | 評論 (0)編輯 收藏

          原文地址:http://www.ibm.com/developerworks/java/library/j-cq08296/?ca=dgr-lnxw07JUnit4vsTestNG

          JUnit到了4.0以后,增加了許多新特性,變得更加靈活易用。但是另一個也是基于Annotations的TestNG在靈活易用方面已經走在了JUnit的前面。實際上,TestNG是基于Annotation的測試框架的先驅,那么這兩者之間的差別是什么呢,在這里,將對兩者進行一些簡單的比較。


          首先兩者在外觀上看起來是非常相似的。使用起來都非常的簡便。但是從核心設計的出發點來說,兩者是不一樣的。JUnit一直將自己定位于單元測試框架,也就是說用于測試單個對象。而TestNG定位于更高層次的測試,因此具備了一些JUnit所沒有的功能。

          A simple test case

          At first glance, tests implemented in JUnit 4 and TestNG look remarkably similar. To see what I mean, take a look at the code in Listing 1, a JUnit 4 test that has a macro-fixture (a fixture that is called just once before any tests are run), which is denoted by the @BeforeClass attribute:

          先來看一個簡單的測試例子:
          第一眼看上去,JUnit和TestNG幾乎一模一樣。
          package test.com.acme.dona.dep;

          import static org.junit.Assert.assertEquals;
          import static org.junit.Assert.assertNotNull;
          import org.junit.BeforeClass;
          import org.junit.Test;

          public class DependencyFinderTest {
           
          private static DependencyFinder finder;

           @BeforeClass
           
          public static void init() throws Exception {
            finder 
          = new DependencyFinder();
           }


           @Test
           
          public void verifyDependencies() 
            
          throws Exception {
             String targetClss 
          = 
               
          "test.com.acme.dona.dep.DependencyFind";

             Filter[] filtr 
          = new Filter[] 
                
          new RegexPackageFilter("java|junit|org")}
          ;

             Dependency[] deps 
          = 
                finder.findDependencies(targetClss, filtr);

             assertNotNull(
          "deps was null", deps);
             assertEquals(
          "should be 5 large"5, deps.length);    
           }

          }

           

           

           

          package test.com.acme.dona.dep;

          import static org.testng.Assert.assertEquals;
          import static org.testng.Assert.assertNotNull;
          import org.testng.annotations.BeforeClass;
          import org.testng.annotations.Configuration;
          import org.testng.annotations.Test;

          public class DependencyFinderTest {
           
          private DependencyFinder finder;

           @BeforeClass
           
          private void init(){
            
          this.finder = new DependencyFinder();
           }


           @Test
           
          public void verifyDependencies() 
            
          throws Exception {
             String targetClss 
          = 
               
          "test.com.acme.dona.dep.DependencyFind";

             Filter[] filtr 
          = new Filter[] 
                
          new RegexPackageFilter("java|junit|org")}
          ;

             Dependency[] deps 
          = 
                finder.findDependencies(targetClss, filtr);
             
             assertNotNull(deps, 
          "deps was null" );
             assertEquals(
          5, deps.length, "should be 5 large");        
           }

          }


          仔細觀察,會發現一些不一樣的地方。首先,JUnit要求BeforClass的方法為static,因此finder也隨之需要聲明為static。另外init方法必須聲明為public。而TestNG卻都不需要這樣做。

          posted @ 2007-07-14 19:21 云自無心水自閑 閱讀(2708) | 評論 (0)編輯 收藏

          看一下典型的Log4j.properties

          log4j.rootLogger=DEBUG, A1

          log4j.appender.A1=org.apache.log4j.FileAppender
          log4j.appender.A1.File=${LOG_PATH}/pi.log
          log4j.appender.A1.layout=org.apache.log4j.PatternLayout
          log4j.appender.A1.layout.ConversionPattern=[%t] %-5p %c - %m%n

          第一行就是定義了Log4j中的層次結構的最頂端root的一些屬性
          log4j.rootLogger=DEBUG, A1, 聲明log4j的根節點允許Debug以上級別的日志輸出。其Appender的名稱為:A1

          從第二行開始就是對名為A1的Appender進行定義了
          log4j.appender.A1=org.apache.log4j.FileAppender
          這一行定義了這個appender使用的類型,這里是把日志輸出到文件,目前log4j支持的appender種類不少,最常見的就是FileAppender和ConsoleAppender了。
          其他還有數據庫、郵件等等

          log4j.appender.A1.File=${LOG_PATH}/pi.log
          這一行定義了日志輸出文件所在的目錄和文件名稱

          log4j.appender.A1.layout=org.apache.log4j.PatternLayout
          這一行定義日志文件所輸出使用的模式

          log4j.appender.A1.layout.ConversionPattern=[%t] %-5p %c - %m%n
          這一行定義了日志每一行的格式

          posted @ 2007-07-07 00:11 云自無心水自閑 閱讀(411) | 評論 (0)編輯 收藏

          Log4j

          Log4j的核心是3大組件:Logger、Appender、Layout。

          首先是Logger,Logger的意義在于它不象System.out,它可以根據需要屏蔽部分的Log輸出,同時其他的Log輸出不受影響。
          一、Logger有層次結構。有了層次結構就意味著有了繼承關系,也就意味著可以重用。這似乎和面向對象語言很想象。
          Logger都是有名稱的,而Logger的名稱和Java一樣,也是XXX.XXX.XXX,和Java一樣的規則,相當簡單。類似于Java中的Object,Logger的存在一個默認的根節點:root

          Logger的名稱一般這樣獲得:
          Logger logger = Logger.getLogger("XXX.XXX.XXX");
          或者
          Logger logger = Logger.getLogger(this.getClass());
          Log4j并沒有強制要求用類名作為Logger的名稱,但是這是推薦的做法。

          二、有了層次結構后,就要說一下級別了,文章開始的時候就提到,Log4j的優勢就在于能夠根據需要過濾Log的輸出,主要(不是全部)就是通過級別實現的。
          Log4j把級別分為:Fatal,Error,Warn,Info,Debug。這樣的區分也是經過慎重考慮的,如果引入太多的層次,會使得程序開發者在記錄日志的時候,難以選擇,會挑花了眼。
          級別之間存在優先級的高低。
          通過如下語句,輸出不同級別的Log
          logger.debug("...");
          logger.info("...");
          logger.warn("...");
          logger.error("...");
          logger.fatal("...");

          如果Logger的級別設為Warn,那么只有級別比Warn高的語句的Log才會輸出。 比如:logger.debug語句這時不起作用。

          三、層次結構的繼承關系現在便發揮作用了。子節點如果沒有顯式定義級別,那么自動繼承最近的父節點的級別。這樣,就不需要為每一個Logger都去定義級別了,因為至少根節點是存在的,可以從根節點中獲得級別定義。

          四、全局級別,可以通過設置日志的“門檻”,來實現全局強制性的級別控制。
          LoggerResposity reposity = x.getLoggerResposity();
          resposity.setThreshold(Level.WARN); 這完全可以在配置文件中配置。
          這樣,logger.info語句將不再起作用。

           

          其次是Appender,Appender決定了Log究竟輸出到什么地方,Log4j提供了多重輸出的功能,也就是說可以為Log定義多個輸出地點。
          同樣,層次結構在這里也發揮的威力,子節點的Logger將會繼承父節點的Appender,免去了一個一個定義Appender的工作,根節點默認的Appender的Console。
          當然,也可以設置不繼承父節點的Appender


          最后是Layout,Layout決定了Log的格式。

          Log4j的配置完全可以通過編程實現,對于特別簡單的應用來說,絕對是夠用了。但是,對于稍微大一點的應用,把配置硬編碼在程序中是不靈活的。所以,使用配置配置文件是比較好的選擇。

          posted @ 2007-06-23 09:11 云自無心水自閑 閱讀(801) | 評論 (0)編輯 收藏

           Java在傳統上感覺和硬件打交道的比較少,這部分工作用C語言的比較多。
          但并不是說Java不具備這樣的能力。

          Sun就發布了Java和串口以及并口的開發包。但是在正常的搜索中只能找到Linux和Solaris版本的類庫。
          下面這個鏈接是Win32平臺下的包,找了很久才找到。
          http://javashoplm.sun.com/ECom/docs/Welcome.jsp?StoreId=22&PartDetailId=7235-javacomm-2.0-spec-oth-JSpec&SiteId=JSC&TransactionId=noreg


          下面簡單說一下使用的步驟。

          1. 把下載包中的win32com.dll放到windows\system32目錄下,文檔中說是放在jdk的bin目錄下,但是會報錯,我后來放在system32目錄下解決此問題
          2. 把comm.jar和javax.comm.properties這兩個文件放在類路徑中,注意要放在一起。文檔上是要求放在jdk的lib目錄中。
          如果缺少javax.comm.properties文件,就找不到任何一個串口和并口

                  portList = CommPortIdentifier.getPortIdentifiers();/*不帶參數的getPortIdentifiers方法獲得一個枚舉對象,該對象又包含了系統中管理每個端口的CommPortIdentifier對象。注意這里的端口不僅僅是指串口,也包括并口。這個方法還可以帶參數。getPortIdentifiers(CommPort)獲得與已經被應用程序打開的端口相對應的CommPortIdentifier對象。 getPortIdentifier(String portName)獲取指定端口名(比如“COM1”)的CommPortIdentifier對象。*/

                  
          while (portList.hasMoreElements()) {
                      portId 
          = (CommPortIdentifier) portList.nextElement();
                      
          if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)/*getPortType方法返回端口類型*/ {
                          
          if (portId.getName().equals("COM1"))/* 找Windows下的第一個串口*/ {
                              SimpleRead reader 
          = new SimpleRead();
                          }

                      }

                  }

          最后向大家推薦一個工具:Virtual Serial Port Driver XP4
          這個工具可以在機器上虛擬一對相連接的串口。
          這樣在一臺機器上不需要其他設備,就可以進行串口程序的測試、調試工作了。

           

          posted @ 2007-05-18 20:39 云自無心水自閑 閱讀(7810) | 評論 (14)編輯 收藏

          僅列出標題
          共29頁: First 上一頁 15 16 17 18 19 20 21 22 23 下一頁 Last 
          主站蜘蛛池模板: 阳山县| 巨鹿县| 通化县| 清丰县| 库伦旗| 周宁县| 塔河县| 京山县| 根河市| 峨山| 奉贤区| 海口市| 永川市| 西畴县| 工布江达县| 永寿县| 西充县| 思南县| 揭阳市| 东山县| 虎林市| 呼伦贝尔市| 射阳县| 察哈| 咸丰县| 乐平市| 九龙坡区| 丽水市| 兴海县| 磐安县| 建宁县| 丘北县| 香河县| 苍溪县| 化德县| 厦门市| 资兴市| 奇台县| 辉南县| 昔阳县| 吉林市|