public class SchemaValidatorImpl extends Object implements SchemaValidator
形式チェックにはスキーマ定義ファイルを使用する。 本クラスはシングルトンとして使用すること。
スキーマ定義ファイルは変換を行うオブジェクトのクラスと 同じパッケージ、 同じ名前、拡張子”.xsd”で配置すること。
例)XML変換対象のオブジェクトが「sample.SampleBean」クラスの場合、
スキーマ定義ファイルはクラスパス上の「sample/SampleBean.xsd」 ファイルとなる。
XMLのパースにはDOMパーサが必要となる。 TERASOLUNAでは詳細なエラー情報を取得するために、 拡張したXML11ConfigurationEx
、
XMLErrorReporterEx
を利用する。
本クラスは、スキーマ定義に名前空間を使用することができる。 名前空間を使用する場合、namespace
属性にtrueを設定する必要がある。
デフォルトはfalseで、名前空間を使用しない設定になっている。 名前空間を使用する場合、スキーマ定義ファイルのキャッシュを行うことができる。
名前空間を使用するためには、以下の設定が必要である。
namespace
属性にtrueを設定する。cache
属性をfalseに設定する。initNamespaceProperties()
メソッドを呼び出して、 プロパティファイルを読み込む。名前空間を使用しない場合のスキーマファイル設定例
【形式チェック対象のXMLデータ】
<sample-dto>
<user-id>15</user-id>
<user-name>user1</user-name>
<item>
<id>100</id>
<name>item1</name>
<price>1000</price>
</item>
<item>
<id>101</id>
<name>item2</name>
<price>2000</price>
</item>
</sample-dto>
【スキーマ定義ファイルの設定例】
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="sample-dto" type="sample-dto-type"/>
<xs:complexType name="sample-dto-type">
<xs:sequence>
<xs:element name="user-id" type="xs:int" />
<xs:element name="user-name" type="xs:string" />
<xs:element name="item" type="item-type" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="item-type">
<xs:sequence>
<xs:element name="id" type="xs:int" />
<xs:element name="name" type="xs:string" />
<xs:element name="price" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:schema>
名前空間を使用する場合のスキーマファイル設定例
【形式チェック対象のXMLデータサンプル】 <sample-dto xmlns="http://xxx.co.jp/sample/samplebean"> <user-id>15</user-id> <user-name>user1</user-name> <item> <id>100</id> <name>item1</name> <price>1000</price> </item> <item> <id>101</id> <name>item2</name> <price>2000</price> </item> </sample-dto> 【スキーマ定義ファイルの設定サンプル】 <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://xxx.co.jp/sample/samplebean" targetNamespace="http://xxx.co.jp/sample/samplebean" elementFormDefault="qualified"> <xs:element name="sample-dto" type="tns:sample-dto-type"/> <xs:complexType name="sample-dto-type"> <xs:sequence> <xs:element name="user-id" type="xs:int"/> <xs:element name="user-name" type="xs:string"/> <xs:element name="item" type="tns:item-type" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> <xs:complexType name="item-type"> <xs:sequence> <xs:element name="id" type="xs:int"/> <xs:element name="name" type="xs:string"/> <xs:element name="price" type="xs:int"/> </xs:sequence> </xs:complexType> </xs:schema>XML11ConfigurationEx
,
ErrorMessages
,
XMLErrorReporterEx
,
jp.terasoluna.fw.web.rich.springmvc.bind.XMLServletRequestDataBinder
,
XMLGrammarPoolImpl
修飾子とタイプ | フィールドと説明 |
---|---|
protected boolean |
allowDTD
DTDの許容設定。
|
protected boolean |
cache
スキーマ定義のキャッシュ使用設定。
|
protected org.apache.xerces.xni.grammars.XMLGrammarPool |
grammarPool
名前空間の文法プール。
|
protected boolean |
namespace
名前空間の使用設定。
|
protected boolean |
namespaceCheck
名前空間のチェック設定。
|
protected Properties |
namespaceProperties
クラス名と名前空間のマッピングを定義したプロパティ。
|
protected String |
namespacePropertyFileName
クラス名と名前空間のマッピングを定義したプロパティファイル名。
|
static String |
NESTED_FOLDER_SEPARATOR
フォルダのセパレータ。
|
static String |
NESTED_PACKAGE_SEPARATOR
パッケージのセパレータ。
|
static String |
XSD_FILE_SUFFIX
スキーマ定義ファイルのサフィックス。
|
コンストラクタと説明 |
---|
SchemaValidatorImpl() |
修飾子とタイプ | メソッドと説明 |
---|---|
protected void |
checkNamespaceProperties()
名前空間が重複していないかのチェックを行う。
|
protected org.apache.xerces.parsers.DOMParser |
createDomParser(Object object)
DOMパーサを生成する。
|
protected org.apache.xerces.xni.parser.XMLParserConfiguration |
createXmlParserConfiguration()
XMLParserConfigurationを生成する。
|
protected String |
getNamespaceName(Object object)
プロパティファイルから名前空間を取得する。
|
protected String |
getSchemaFilePath(Object object)
スキーマファイルのパスを取得する。
|
protected URL |
getUrl(Object object)
URLを取得する。
|
void |
initNamespaceProperties()
初期化時に名前空間をプロパティに設定する。
|
protected void |
loadNamespaceProperties()
名前空間が定義されたプロパティファイルを読み込み、属性にセットする。
|
void |
setAllowDTD(boolean allowDTD)
DTDの許容設定を行う。
|
void |
setCache(boolean cache)
スキーマ定義のキャッシュ使用設定を行う。
|
protected void |
setCommonParserFeature(org.apache.xerces.parsers.DOMParser parser)
パーサ共通のFeatureを設定する
|
protected void |
setCommonParserProperty(org.apache.xerces.parsers.DOMParser parser,
ErrorMessages errorMessages)
パーサ共通のPropertyを設定する。
|
void |
setNamespace(boolean namespace)
名前空間の使用設定を行う。
|
void |
setNamespaceCheck(boolean namespaceCheck)
名前空間のチェック設定を行う。
|
void |
setNamespaceProperties(Properties namespaceProperties)
クラス名と名前空間のマッピングを定義したプロパティを設定する
|
void |
setNamespacePropertyFileName(String namespacePropertyFileName)
クラス名と名前空間のマッピングを定義したプロパティファイル名を設定する。
|
Document |
validate(InputStream in,
Object object,
ErrorMessages errorMessages)
XMLデータの形式チェックを行うメソッド。
|
public static final String NESTED_PACKAGE_SEPARATOR
public static final String NESTED_FOLDER_SEPARATOR
protected String namespacePropertyFileName
protected boolean cache
protected boolean namespace
protected org.apache.xerces.xni.grammars.XMLGrammarPool grammarPool
protected Properties namespaceProperties
protected boolean namespaceCheck
protected boolean allowDTD
public void setCache(boolean cache)
cache
- スキーマ定義ファイルのキャッシュを行う場合、truepublic void setNamespace(boolean namespace)
namespace
- 名前空間を使用する場合、truepublic void setNamespaceCheck(boolean namespaceCheck)
namespaceCheck
- 名前空間をチェックする場合、truepublic void setNamespaceProperties(Properties namespaceProperties)
namespaceProperties
- クラス名と名前空間のマッピングを定義したプロパティpublic void setNamespacePropertyFileName(String namespacePropertyFileName)
namespacePropertyFileName
- クラス名と名前空間のマッピングを定義したプロパティファイル名public void setAllowDTD(boolean allowDTD)
allowDTD
- XMLにDTD(DOCTYPE)が含まれることを許容する場合はtrue (推奨はfalse)public void initNamespaceProperties()
protected void loadNamespaceProperties()
protected void checkNamespaceProperties()
public Document validate(InputStream in, Object object, ErrorMessages errorMessages)
XMLデータをDOMツリーに変換する際に、XMLスキーマによる 形式チェック(妥当性検証)を実行する。
形式チェックには、スキーマ定義ファイルを用いる。
正常に形式チェックが終了した場合、DOMツリーを返却する。
ユーザの入力が原因と考えられるデータ型のエラーが発生した場合、引数のerrorMessagesにエラー情報を格納して、nullを返却する。
それ以外のエラーに関しては、OXMappingExceptionのサブクラスにラップしてスローする。
validate
インタフェース内 SchemaValidator
in
- XMLデータobject
- 変換対象のオブジェクトerrorMessages
- エラーメッセージが格納されるインスタンスprotected org.apache.xerces.parsers.DOMParser createDomParser(Object object) throws SAXNotRecognizedException, SAXNotSupportedException
object
- パース対象のオブジェクトSAXNotSupportedException
- 認識できないキーが設定された場合にスローされる例外SAXNotRecognizedException
- サポートされていない値が設定された場合にスローされる例外protected org.apache.xerces.xni.parser.XMLParserConfiguration createXmlParserConfiguration()
protected void setCommonParserProperty(org.apache.xerces.parsers.DOMParser parser, ErrorMessages errorMessages) throws SAXNotRecognizedException, SAXNotSupportedException
parser
- DOMパーサerrorMessages
- エラーSAXNotRecognizedException
- 認識できないキーが設定された場合にスローされる例外SAXNotSupportedException
- サポートされていない値が設定された場合にスローされる例外protected void setCommonParserFeature(org.apache.xerces.parsers.DOMParser parser) throws SAXNotRecognizedException, SAXNotSupportedException
parser
- DOMパーサSAXNotRecognizedException
- 認識できないキーが設定された場合にスローされる例外SAXNotSupportedException
- サポートされていない値が設定された場合にスローされる例外protected URL getUrl(Object object)
引数のオブジェクトと同一のクラスパスから、定義ファイルを取得する
object
- オブジェクトprotected String getSchemaFilePath(Object object)
object
- スキーマチェック対象のオブジェクトCopyright © 2014. All Rights Reserved.