LoginSignup
4
5

More than 5 years have passed since last update.

JavaのJNAを利用してlibxml2のxmlReaderのクラスを書く

Posted at

JavaのJNAを利用してlibxml2のxmlRederの定義します。昔、libxml2とjavaのstaxのどちらがパフォーマンスが良いか測定したときに書きました。JNAの書き方の参考になると思います。

JNAを利用するとJNIを書かなくて良いので、ネイティブライブラリのメンテナンスが楽になります。
JNAの説明は、http://ja.wikipedia.org/wiki/Java_Native_Access に書いてあります。

import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;

public class LibXml2 {
    static {
        Native.register("xml2");
    }

    static public class XmlParserProperties {
        public static final int XML_PARSER_LOADDTD = 1;
        public static final int XML_PARSER_DEFAULTATTRS = 2;
        public static final int XML_PARSER_VALIDATE = 3;
        public static final int XML_PARSER_SUBST_ENTITIES = 4;
    }

    static public class XmlParserOption {
        public static final int XML_PARSE_RECOVER = 1;// recover on errors
        public static final int XML_PARSE_NOENT = 2;// substitute entities
        public static final int XML_PARSE_DTDLOAD = 4;// load the external
                                                        // subset
        public static final int XML_PARSE_DTDATTR = 8;// default DTD attributes
        public static final int XML_PARSE_DTDVALID = 16;// validate with the DTD
        public static final int XML_PARSE_NOERROR = 32;// suppress error reports
        public static final int XML_PARSE_NOWARNING = 64;// suppress warning
                                                            // reports
        public static final int XML_PARSE_PEDANTIC = 128;// pedantic error
                                                            // reporting
        public static final int XML_PARSE_NOBLANKS = 256;// remove blank nodes
        public static final int XML_PARSE_SAX1 = 512;// use the SAX1 interface
                                                        // internally
        public static final int XML_PARSE_XINCLUDE = 1024;// Implement XInclude
                                                            // substitition
        public static final int XML_PARSE_NONET = 2048;// Forbid network access
        public static final int XML_PARSE_NODICT = 4096;// Do not reuse the
                                                        // context dictionnary
        public static final int XML_PARSE_NSCLEAN = 8192;// remove redundant
                                                            // namespaces
                                                            // declarations
        public static final int XML_PARSE_NOCDATA = 16384;// merge CDATA as text
                                                            // nodes
        public static final int XML_PARSE_NOXINCNODE = 32768;// do not generate
                                                                // XINCLUDE
                                                                // START/END
                                                                // nodes
        public static final int XML_PARSE_COMPACT = 65536;// compact small text
                                                            // nodes; no
                                                            // modification of
                                                            // the tree allowed
                                                            // afterwards (will
                                                            // possibly crash if
                                                            // you try to modify
                                                            // the tree)
        public static final int XML_PARSE_OLD10 = 131072;// parse using XML-1.0
                                                            // before update 5
        public static final int XML_PARSE_NOBASEFIX = 262144;// do not fixup
                                                                // XINCLUDE
                                                                // xml:base uris
        public static final int XML_PARSE_HUGE = 524288;// relax any hardcoded
                                                        // limit from the parser
        public static final int XML_PARSE_OLDSAX = 1048576;// parse using SAX2
                                                            // interface from
                                                            // before 2.7.0
    }

    static public class XmlParserSeverities {
        public static final int XML_PARSER_SEVERITY_VALIDITY_WARNING = 1;
        public static final int XML_PARSER_SEVERITY_VALIDITY_ERROR = 2;
        public static final int XML_PARSER_SEVERITY_WARNING = 3;
        public static final int XML_PARSER_SEVERITY_ERROR = 4;
    }

    static public class XmlReaderTypes {
        public static final int XML_READER_TYPE_NONE = 0;
        public static final int XML_READER_TYPE_ELEMENT = 1;
        public static final int XML_READER_TYPE_ATTRIBUTE = 2;
        public static final int XML_READER_TYPE_TEXT = 3;
        public static final int XML_READER_TYPE_CDATA = 4;
        public static final int XML_READER_TYPE_ENTITY_REFERENCE = 5;
        public static final int XML_READER_TYPE_ENTITY = 6;
        public static final int XML_READER_TYPE_PROCESSING_INSTRUCTION = 7;
        public static final int XML_READER_TYPE_COMMENT = 8;
        public static final int XML_READER_TYPE_DOCUMENT = 9;
        public static final int XML_READER_TYPE_DOCUMENT_TYPE = 10;
        public static final int XML_READER_TYPE_DOCUMENT_FRAGMENT = 11;
        public static final int XML_READER_TYPE_NOTATION = 12;
        public static final int XML_READER_TYPE_WHITESPACE = 13;
        public static final int XML_READER_TYPE_SIGNIFICANT_WHITESPACE = 14;
        public static final int XML_READER_TYPE_END_ELEMENT = 15;
        public static final int XML_READER_TYPE_END_ENTITY = 16;
        public static final int XML_READER_TYPE_XML_DECLARATION = 17;
    }

    static public class XmlTextReaderMode {
        public static final int XML_TEXTREADER_MODE_INITIAL = 0;
        public static final int XML_TEXTREADER_MODE_INTERACTIVE = 1;
        public static final int XML_TEXTREADER_MODE_ERROR = 2;
        public static final int XML_TEXTREADER_MODE_EOF = 3;
        public static final int XML_TEXTREADER_MODE_CLOSED = 4;
        public static final int XML_TEXTREADER_MODE_READING = 5;
    }

    public static native Pointer xmlReaderForMemory(Pointer buffer, int size, final byte[] url, final byte[] encoding, int option);

    public static native int xmlReaderNewFd(Pointer reader, int fd, byte[] url, byte[] encoding, int options);

    public static native void xmlFreeTextReader(Pointer xmlTextReader);

    public static native int xmlTextReaderAttributeCount(Pointer reader);

    public static native NativeLong xmlTextReaderByteConsumed(Pointer reader);

    public static native int xmlTextReaderClose(Pointer reader);

    public static native Pointer xmlTextReaderConstLocalName(Pointer reader);

    public static native Pointer xmlTextReaderConstName(Pointer reader);

    public static native Pointer xmlTextReaderConstNamespaceUri(Pointer reader);

    public static native Pointer xmlTextReaderConstPrefix(Pointer reader);

    public static native Pointer xmlTextReaderConstString(Pointer reader);

    public static native Pointer xmlTextReaderConstValue(Pointer reader);

    public static native Pointer xmlTextReaderConstXmlLang(Pointer reader);

    public static native Pointer xmlTextReaderConstXmlVersion(Pointer reader);

    public static native int xmlTextReaderDepth(Pointer reader);

    public static native Pointer xmlTextReaderGetAttribute(Pointer reader, byte[] name);

    public static native Pointer xmlTextReaderGetAttributeNo(Pointer reader, int no);

    public static native Pointer xmlTextReaderGetAttributeNs(Pointer reader, byte[] localName, byte[] namespaceURI);

    public static native int xmlTextReaderGetParserColumnNumber(Pointer reader);

    public static native int xmlTextReaderGetParserLineNumber(Pointer reader);

    public static native int xmlTextReaderHasAttributes(Pointer reader);

    public static native int xmlTextReaderHasValue(Pointer reader);

    public static native int xmlTextReaderIsEmptyElement(Pointer reader);

    public static native int xmlTextReaderIsValid(Pointer reader);

    public static native Pointer xmlTextReaderLocalName(Pointer reader);

    public static native int xmlTextReaderMoveToAttribute(Pointer reader, byte[] name);

    public static native int xmlTextReaderMoveToAttributeNo(Pointer reader, int no);

    public static native int xmlTextReaderMoveToAttributeNs(Pointer reader, byte[] localName, byte[] namespaceURI);

    public static native int xmlTextReaderMoveToElement(Pointer reader);

    public static native int xmlTextReaderMoveToFirstAttribute(Pointer reader);

    public static native int xmlTextReaderMoveToNextAttribute(Pointer reader);

    public static native Pointer xmlTextReaderName(Pointer reader);

    public static native Pointer xmlTextReaderNamespaceUri(Pointer reader);

    public static native int xmlTextReaderNext(Pointer reader);

    public static native int xmlTextReaderNextSibling(Pointer reader);

    public static native int xmlTextReaderNodeType(Pointer reader);

    public static native int xmlTextReaderNormalization(Pointer reader);

    public static native Pointer xmlTextReaderPrefix(Pointer reader);

    public static native int xmlTextReaderQuoteChar(Pointer reader);

    public static native int xmlTextReaderRead(Pointer reader);

    public static native int xmlTextReaderReadAttributeValue(Pointer reader);

    public static native Pointer xmlTextReaderReadInnerXml(Pointer reader);

    public static native Pointer xmlTextReaderReadOuterXml(Pointer reader);

    public static native int xmlTextReaderReadState(Pointer reader);

    public static native Pointer xmlTextReaderReadString(Pointer reader);

}
4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5