可以通过HTMLDocument添加属性吗?

huangapple go评论70阅读模式
英文:

Is it possible to add an Attribute using HTMLDocument?

问题

目标是检查特定属性如果未找到则将其附加到现有属性列表

尽管可能有其他选项可用但此示例仅限于使用HTMLDocument对象和相关对象进行操作

```java
import java.io.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

class AddAttributeTest 
{

    static String srg = "<html> " + 
            "  <head>" + 
            "      Hello World" + 
            "  </head>" + 
            "  <body a1=\"ABC\" a2=\"3974\" a3=\"A1B2\">     " + 
            "    <H1>Start Here<H1>" + 
            "    <p>This is the body</p>" + 
            "  </body>" + 
            "</html>" ;

    public static void main(String[] args)
    {
        EditorKit kit = new HTMLEditorKit();
        HTMLDocument doc = new HTMLDocument();//

        try
        {
            Reader rd = new StringReader(srg); 
            kit.read(rd, doc, 0);
            ElementIterator it = new ElementIterator(doc);
            Element elem = null;

            while ( (elem = it.next()) != null )
            {
                if (elem.getName().equals("body"))
                {
                    AttributeSet as = elem.getAttributes();
                    if (as.isDefined("a1"))
                    {
                        System.out.println("a1 exists : " + as.getAttribute("a1"));
                    }

                    if (as.isDefined("a4"))
                    {
                        System.out.println("a4 exists : " + as.getAttribute("a4"));
                    }
                    else
                    {
                        System.out.println("a4 is missing and need to add");
                        //将缺失的属性添加到现有属性列表的末尾。
                        //elem - 仅存在get....()调用。
                        //as - 仅存在get...()调用和检查。
                    }
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        System.exit(1);
    }
}

此示例说明它能够找到"a1"属性并打印出其值。

a1 exists : ABC
a4 is missing and need to add

接下来,它尝试定位未找到的"a4"属性并尝试添加它。

在Eclipse中,我已经检查了Element和AttributeSet对象的可用调用,它们都是getters()或布尔检查。没有任何"add"例程或setters()可以调用。

尝试了各种不同的方式来搜索和添加属性,除了我正在尝试的方式。

是否可能使用HTMLDocument和相关调用来实现它?

英文:

The objective is to check for specific attribute and if not found then append it to the list of existing attributes.

Although there may be other options available, this example is restricted to working with the HTMLDocument object and associated objects.

import java.io.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
class AddAttributeTest 
{
static String srg = &quot;&lt;html&gt; &quot; + 
&quot;  &lt;head&gt;&quot; + 
&quot;      Hello World&quot; + 
&quot;  &lt;/head&gt;&quot; + 
&quot;  &lt;body a1=\&quot;ABC\&quot; a2=\&quot;3974\&quot; a3=\&quot;A1B2\&quot;&gt;     &quot; + 
&quot;    &lt;H1&gt;Start Here&lt;H1&gt;&quot; + 
&quot;    &lt;p&gt;This is the body&lt;/p&gt;&quot; + 
&quot;  &lt;/body&gt;&quot; + 
&quot;&lt;/html&gt;&quot; ;
public static void main(String[] args)
{
EditorKit kit = new HTMLEditorKit();
HTMLDocument doc = new HTMLDocument();//
try
{
Reader rd = new StringReader(srg); 
kit.read(rd, doc, 0);
ElementIterator it = new ElementIterator(doc);
Element elem = null;
while ( (elem = it.next()) != null )
{
if (elem.getName().equals(&quot;body&quot;))
{
AttributeSet as = elem.getAttributes();
if (as.isDefined(&quot;a1&quot;))
{
System.out.println(&quot;a1 exists : &quot; + as.getAttribute(&quot;a1&quot;));
}
if (as.isDefined(&quot;a4&quot;))
{
System.out.println(&quot;a4 exists : &quot; + as.getAttribute(&quot;a4&quot;));
}
else
{
System.out.println(&quot;a4 is missing and need to add&quot;);
//Add the missing attribute to the end of the existing list of attributes.
//elem - only get....() calls exist.
//as - only get...() calls and checks exist. 
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
System.exit(1);
}
}

This example illustrates that it is able to find the "a1" attribute and print out its value.

a1 exists : ABC
a4 is missing and need to add

Next it attempts to locate the "a4" attribute which it does not find and attempts to add it.

Using Eclipse I have check available calls for both the Element & AttributeSet objects and they are all getters() or boolean checks. There weren't any "add" routines or setters() to call.

Searching and any references to adding attributes are implemented in a variety of different ways except for how I am trying to do it.

Is it possible to implement it using HTMLDocument and associated calls?

答案1

得分: 1

如果您想要使用 HTMLDocument 进行编辑,您将需要使用 writeLock() 和 writeUnlock() 方法。这些方法受到保护,因此您需要扩展 HtmlDocument 以公开暴露这些方法。

public static class MyHTMLDocument extends HTMLDocument {
    public void doWriteLock() {
        writeLock();
    }

    public void doWriteUnlock() {
        writeUnlock();
    }
}

在您的代码中,使用 MyHTMLDocument 的实例来替代 HTMLDocument:

MyHTMLDocument doc = new MyHTMLDocument();

并且按照以下方式添加缺失的 a4 属性:

doc.doWriteLock();
((BlockElement) elem).addAttribute("a4", "value");
doc.doWriteUnlock();
英文:

If you want to edit using HTMLDocument you will need to use writeLock() and writeUnlock(). These methods are protected, so you need to extend HtmlDocument to expose those methods publicly.

public static class MyHTMLDocument extends HTMLDocument {
public void doWriteLock() {
writeLock();
}
public void doWriteUnlock() {
writeUnlock();
}
}

In your code, use an instance of MyHTMLDocument instead of HTMLDocument:

MyHTMLDocument doc = new MyHTMLDocument();

and add the missing a4 attribute as follows:

doc.doWriteLock();
((BlockElement) elem).addAttribute(&quot;a4&quot;, &quot;value&quot;);
doc.doWriteUnlock();

huangapple
  • 本文由 发表于 2020年9月19日 19:13:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63968112.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定