Search in sources :

Example 1 with CoreDocumentImpl

use of org.apache.xerces.dom.CoreDocumentImpl in project translationstudio8 by heartsome.

the class MessageParser method htmlToText.

/**
	 * 将 html 格式的文本过滤掉标签.
	 * @param html
	 *            html 格式的字符串
	 * @return String
	 * 			  过滤掉 html 标签后的文本。如果 html 为空,返回空串""
	 */
private String htmlToText(String html) {
    if (html == null) {
        return "";
    }
    DOMFragmentParser parser = new DOMFragmentParser();
    CoreDocumentImpl codeDoc = new CoreDocumentImpl();
    InputSource inSource = new InputSource(new ByteArrayInputStream(html.getBytes()));
    inSource.setEncoding(textCharset);
    DocumentFragment doc = codeDoc.createDocumentFragment();
    try {
        parser.parse(inSource, doc);
    } catch (Exception e) {
        return "";
    }
    textBuffer = new StringBuffer();
    processNode(doc);
    return textBuffer.toString();
}
Also used : InputSource(org.xml.sax.InputSource) ByteArrayInputStream(java.io.ByteArrayInputStream) CoreDocumentImpl(org.apache.xerces.dom.CoreDocumentImpl) DOMFragmentParser(org.cyberneko.html.parsers.DOMFragmentParser) DocumentFragment(org.w3c.dom.DocumentFragment) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with CoreDocumentImpl

use of org.apache.xerces.dom.CoreDocumentImpl in project gocd by gocd.

the class XmlEntityReference method init.

protected void init(ThreadContext context, IRubyObject[] args) {
    if (args.length < 2) {
        throw getRuntime().newArgumentError(args.length, 2);
    }
    IRubyObject doc = args[0];
    IRubyObject name = args[1];
    Document document = ((XmlNode) doc).getOwnerDocument();
    // FIXME: disable error checking as a workaround for #719. this depends on the internals of Xerces.
    CoreDocumentImpl internalDocument = (CoreDocumentImpl) document;
    boolean oldErrorChecking = internalDocument.getErrorChecking();
    internalDocument.setErrorChecking(false);
    Node node = document.createEntityReference(rubyStringToString(name));
    internalDocument.setErrorChecking(oldErrorChecking);
    setNode(context, node);
}
Also used : CoreDocumentImpl(org.apache.xerces.dom.CoreDocumentImpl) Node(org.w3c.dom.Node) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Document(org.w3c.dom.Document)

Example 3 with CoreDocumentImpl

use of org.apache.xerces.dom.CoreDocumentImpl in project gocd by gocd.

the class XmlNode method fixUserData.

/**
     * This is a hack to fix #839. We should submit a patch to Xerces.
     * It looks like CoreDocumentImpl.adoptNode() doesn't copy
     * the user data associated with child nodes (recursively).
     */
private void fixUserData(Document previous, Node ret) {
    String key = NokogiriHelpers.ENCODED_STRING;
    for (Node child = ret.getFirstChild(); child != null; child = child.getNextSibling()) {
        CoreDocumentImpl previousDocument = (CoreDocumentImpl) previous;
        child.setUserData(key, previousDocument.getUserData(child, key), null);
        fixUserData(previous, child);
    }
}
Also used : CoreDocumentImpl(org.apache.xerces.dom.CoreDocumentImpl) Node(org.w3c.dom.Node) NokogiriHelpers.clearCachedNode(nokogiri.internals.NokogiriHelpers.clearCachedNode) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyString(org.jruby.RubyString)

Example 4 with CoreDocumentImpl

use of org.apache.xerces.dom.CoreDocumentImpl in project nokogiri by sparklemotion.

the class XmlEntityReference method init.

protected void init(ThreadContext context, IRubyObject[] args) {
    if (args.length < 2) {
        throw getRuntime().newArgumentError(args.length, 2);
    }
    IRubyObject doc = args[0];
    IRubyObject name = args[1];
    Document document = ((XmlNode) doc).getOwnerDocument();
    // FIXME: disable error checking as a workaround for #719. this depends on the internals of Xerces.
    CoreDocumentImpl internalDocument = (CoreDocumentImpl) document;
    boolean oldErrorChecking = internalDocument.getErrorChecking();
    internalDocument.setErrorChecking(false);
    Node node = document.createEntityReference(rubyStringToString(name));
    internalDocument.setErrorChecking(oldErrorChecking);
    setNode(context, node);
}
Also used : CoreDocumentImpl(org.apache.xerces.dom.CoreDocumentImpl) Node(org.w3c.dom.Node) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Document(org.w3c.dom.Document)

Example 5 with CoreDocumentImpl

use of org.apache.xerces.dom.CoreDocumentImpl in project nokogiri by sparklemotion.

the class XmlNode method fixUserData.

/**
     * This is a hack to fix #839. We should submit a patch to Xerces.
     * It looks like CoreDocumentImpl.adoptNode() doesn't copy
     * the user data associated with child nodes (recursively).
     */
private void fixUserData(Document previous, Node ret) {
    String key = NokogiriHelpers.ENCODED_STRING;
    for (Node child = ret.getFirstChild(); child != null; child = child.getNextSibling()) {
        CoreDocumentImpl previousDocument = (CoreDocumentImpl) previous;
        child.setUserData(key, previousDocument.getUserData(child, key), null);
        fixUserData(previous, child);
    }
}
Also used : CoreDocumentImpl(org.apache.xerces.dom.CoreDocumentImpl) NokogiriHelpers.clearCachedNode(nokogiri.internals.NokogiriHelpers.clearCachedNode) Node(org.w3c.dom.Node) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyString(org.jruby.RubyString) NokogiriHelpers.convertString(nokogiri.internals.NokogiriHelpers.convertString)

Aggregations

CoreDocumentImpl (org.apache.xerces.dom.CoreDocumentImpl)5 Node (org.w3c.dom.Node)4 NokogiriHelpers.clearCachedNode (nokogiri.internals.NokogiriHelpers.clearCachedNode)2 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)2 RubyString (org.jruby.RubyString)2 IRubyObject (org.jruby.runtime.builtin.IRubyObject)2 Document (org.w3c.dom.Document)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 MessagingException (javax.mail.MessagingException)1 NokogiriHelpers.convertString (nokogiri.internals.NokogiriHelpers.convertString)1 DOMFragmentParser (org.cyberneko.html.parsers.DOMFragmentParser)1 DocumentFragment (org.w3c.dom.DocumentFragment)1 InputSource (org.xml.sax.InputSource)1