package jp.co.nec.ome.utility.xml;
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
//*****************************************************************************
/**
?* XML文書分析クラスです。<BR>
?* XMLに記述されている定義を分析し、子要素や子要素の內(nèi)容の取扱を容易にする
?* ユーティリティクラスです。
?* <P>
?* @author? Hisaya Saito
?* @version $Revision:?? 5.2? $ $Date:?? 20 May 2005 10:24:55? $
?* @since?? OpenMeisterEnterprise/EF 3.2
?*/
//*****************************************************************************
public class OmXMLAnalyzer {
? //***************************************************************************
? //コンストラクタ
? //***************************************************************************
? //***************************************************************************
? /**
?? * privateのコンストラクタです。<BR>
?? * スタティックメソッドのみの利用とし、インスタンス化は不許可とします。
?? */
? //***************************************************************************
? private OmXMLAnalyzer() {
? }
? //***************************************************************************
? //publicメソッド
? //***************************************************************************
? //***************************************************************************
? /**
?? * XMLファイルパスの文字列からXML文書のルート要素を取得します。
?? * @param? argFilePath XMLファイルパス
?? * @return ルート要素
?? * @exception FileNotFoundException 何らかの理由でファイルを開くことができない場合
?? * @exception ParserConfigurationException 要求された構(gòu)成を満たすDocumentBuilderを生成できない場合
?? * @exception SAXException 構(gòu)文解析エラーが発生した場合
?? * @exception IOException 入出力エラーが発生した場合
?? */
? //***************************************************************************
? public static Element getRootElement(String argFilePath)
?????????????????????????????????????????? throws FileNotFoundException,
????????????????????????????????????????????????? ParserConfigurationException,
????????????????????????????????????????????????? SAXException,
????????????????????????????????????????????????? IOException {
??? Element rootElement = null;
??? //XML文書解釈のためのParserの作成
??? File defineFile = new File(argFilePath);
??? InputStream inputStream = new FileInputStream(defineFile);
??? try {
????? DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
????? DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
????? Document document = documentBuilder.parse(inputStream);
????? //XML文書のツリーの最上位の要素rootを取得します。
????? rootElement = document.getDocumentElement();
??? } finally {
????? inputStream.close();
??? }
??? return rootElement;
? }
? //***************************************************************************
? /**
?? * 入力ストリームからXML文書のルート要素を取得します。
?? * @param? argInputStream
?? * @return ルート要素
?? * @exception FileNotFoundException 何らかの理由でファイルを開くことができない場合
?? * @exception ParserConfigurationException 要求された構(gòu)成を満たすDocumentBuilderを生成できない場合
?? * @exception SAXException 構(gòu)文解析エラーが発生した場合
?? * @exception IOException 入出力エラーが発生した場合
?? */
? //***************************************************************************
? public static Element getRootElement(InputStream argInputStream)
????? throws FileNotFoundException,
???????????? ParserConfigurationException,
???????????? SAXException,
???????????? IOException {
??? Element rootElement = null;
??? //XML文書解釈のためのParserの作成
??? try {
????? DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
????? DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
????? Document document = documentBuilder.parse(argInputStream);
????? //XML文書のツリーの最上位の要素rootを取得します。
????? rootElement = document.getDocumentElement();
??? } finally {
????? argInputStream.close();
??? }
??? return rootElement;
? }
? //***************************************************************************
? /**
?? * 指定した親要素の中で最初の子要素を取得します。
?? * @param? argParentElement??? 親要素
?? * @param? argChildElementName 子要素名
?? * @return 最初に取得できた子要素
?? */
? //***************************************************************************
? public static Element getChildElement(Element argParentElement,
??????????????????????????????????????? String argChildElementName) {
??? return getChildElement(argParentElement, argChildElementName, 0);
? }
? //***************************************************************************
? /**
?? * 指定した親要素の中の指定した順番の子要素を取得します。
?? * @param? argParentElement?????? 親要素
?? * @param? argChildElementName???????? 子要素名
?? * @param? argSpecificationNumber 順番
?? * @return 指定した順番の子要素
?? */
? //***************************************************************************
? public static Element getChildElement(Element argParentElement,
??????????????????????????????????????? String argChildElementName,
??????????????????????????????????????? int argSpecificationNumber) {
??? NodeList childNodeList =
??????????????????? argParentElement.getElementsByTagName(argChildElementName);
??? return (Element)childNodeList.item(argSpecificationNumber);
? }
? //***************************************************************************
? /**
?? * 指定した親要素の中の全ての子要素を配列で取得します。
?? * @param? argParentElement?????? 親要素
?? * @param? argChildElementName??? 子要素名
?? * @return 子要素の配列
?? */
? //***************************************************************************
? public static Element[] getChildElementList(Element argParentElement,
????????????????????????????????????????????? String argChildElementName) {
??? NodeList childNodeList =
??????????????????? argParentElement.getElementsByTagName(argChildElementName);
??? int listCount = childNodeList.getLength();
??? if(listCount == 0) {
????? return null;
??? }
??? Element[] childElementList = new Element[listCount];
??? for (int i0 = 0; i0 < listCount; i0++) {
????? childElementList[i0] = (Element)childNodeList.item(i0);
??? }
??? return childElementList;
? }
? //***************************************************************************
? /**
?? * 指定した要素の中で指定する屬性の値が設(shè)定されている子要素を取得します。
?? * @param? argParentElement??? 親要素
?? * @param? argChildElementName 子要素名
?? * @param? argAttributeName??? 屬性名
?? * @param? argAttributeValue?? 屬性の値
?? * @return 指定する屬性の値が設(shè)定されている子要素で最初に取得できたもの
?? */
? //***************************************************************************
? public static Element getChildElement(Element argParentElement,
??????????????????????????????????????? String argChildElementName,
??????????????????????????????????????? String argAttributeName,
??????????????????????????????????????? String argAttributeValue) {
??? NodeList childNodeList = argParentElement.getElementsByTagName(argChildElementName);
??? int listLength = childNodeList.getLength();
??? if(listLength == 0) {
????? return null;
??? }
??? Element childElement = null;
??? for (int i0 = 0; i0 < listLength; i0++) {
????? Element localChildElement = (Element)childNodeList.item(i0);
????? String attributeValue = localChildElement.getAttribute(argAttributeName);
????? if (attributeValue.equals(argAttributeValue)) {
??????? childElement = localChildElement;
??????? break;
????? }
??? }
??? return childElement;
? }
? //***************************************************************************
? /**
?? * 指定した要素の內(nèi)容を取得します。
?? * @param? argElement 內(nèi)容を取得したい要素
?? * @return 內(nèi)容
?? */
? //***************************************************************************
? public static String getElementContents(Element argElement) {
??? if(argElement != null) {
????? Text text = (Text)argElement.getFirstChild();
????? if(text == null) {
??????? return null;
????? }
????? return text.getNodeValue();
??? } else {
????? return null;
??? }
? }
? //***************************************************************************
? /**
?? * 指定した親要素の中で最初の子要素の內(nèi)容を取得します。
?? * @param? argParentElement 親要素
?? * @param? argChildElementName? 內(nèi)容を取得したい子要素名
?? * @return 內(nèi)容
?? */
? //***************************************************************************
? public static String getChildElementContents(Element argParentElement,
?????????????????????????????????????????????? String argChildElementName) {
??? Element childElement = getChildElement(argParentElement,
?????????????????????????????????????????? argChildElementName);
??? return getElementContents(childElement);
? }
? //***************************************************************************
? /**
?? * 指定した親要素の中で指定する屬性の値が設(shè)定されている子要素の內(nèi)容を取得します。
?? * @param? argParentElement??? 親要素
?? * @param? argChildElementName 內(nèi)容を取得したい子要素名
?? * @param? argAttributeName??? 屬性名
?? * @param? argAttributeValue?? 屬性の値
?? * @return 內(nèi)容
?? */
? //***************************************************************************
? public static String getChildElementContents(Element argParentElement,
?????????????????????????????????????????????? String argChildElementName,
?????????????????????????????????????????????? String argAttributeName,
?????????????????????????????????????????????? String argAttributeValue) {
??? Element childElement = getChildElement(argParentElement,
?????????????????????????????????????????? argChildElementName,
?????????????????????????????????????????? argAttributeName,
?????????????????????????????????????????? argAttributeValue);
??? return getElementContents(childElement);
? }
? //***************************************************************************
? /**
?? * 指定した親要素の中で最初の子要素の屬性の値を取得します。
?? * @param? argParentElement??? 親要素
?? * @param? argChildElementName 子要素名
?? * @param? argAttributeName??? 屬性名
?? * @return 屬性の値
?? */
? //***************************************************************************
? public static String getAttributeValue(Element argParentElement,
???????????????????????????????????????? String argChildElementName,
???????????????????????????????????????? String argAttributeName) {
??? return getAttributeValue(argParentElement, argChildElementName, 0, argAttributeName);
? }
? //***************************************************************************
? /**
?? * 指定した親要素の中で指定した順番の子要素の屬性の値を取得します。
?? * @param? argParentElement?????? 親要素
?? * @param? argChildElementName??? 子要素名
?? * @param? argSpecificationNumber 順番
?? * @param? argAttributeName?????? 屬性名
?? * @return 屬性の値
?? */
? //***************************************************************************
? public static String getAttributeValue(Element argParentElement,
???????????????????????????????????????? String argChildElementName,
???????????????????????????????????????? int argSpecificationNumber,
???????????????????????????????????????? String argAttributeName) {
??? Element childElement = getChildElement(argParentElement,
?????????????????????????????????????????? argChildElementName,
?????????????????????????????????????????? argSpecificationNumber);
??? return childElement.getAttribute(argAttributeName);
? }
}