是否可以使用 HTMLDocument 添加属性?
Is it possible to add an Attribute using HTMLDocument?
objective 用于检查特定属性,如果未找到,则将其附加到现有属性列表中。
尽管可能还有其他可用选项,但此示例仅限于使用 HTMLDocument 对象和关联对象。
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");
//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);
}
}
此示例说明它能够找到“a1”属性并打印出它的值。
a1 exists : ABC
a4 is missing and need to add
接下来它会尝试找到它没有找到的“a4”属性并尝试添加它。
我使用 Eclipse 检查了 Element 和 AttributeSet 对象的可用调用,它们都是 getters() 或布尔检查。没有任何“添加”例程或 setters() 可以调用。
搜索和任何对添加属性的引用都以各种不同的方式实现,除了我正在尝试的方式。
是否可以使用 HTMLDocument 和相关调用来实现它?
如果您想使用 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();
objective 用于检查特定属性,如果未找到,则将其附加到现有属性列表中。
尽管可能还有其他可用选项,但此示例仅限于使用 HTMLDocument 对象和关联对象。
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");
//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);
}
}
此示例说明它能够找到“a1”属性并打印出它的值。
a1 exists : ABC
a4 is missing and need to add
接下来它会尝试找到它没有找到的“a4”属性并尝试添加它。
我使用 Eclipse 检查了 Element 和 AttributeSet 对象的可用调用,它们都是 getters() 或布尔检查。没有任何“添加”例程或 setters() 可以调用。
搜索和任何对添加属性的引用都以各种不同的方式实现,除了我正在尝试的方式。
是否可以使用 HTMLDocument 和相关调用来实现它?
如果您想使用 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();