下面列出了怎么用javax.swing.text.html.HTML.Tag的API类实例代码及写法,或者点击链接到github查看源代码。
/** Generates a new event with a URL based upon Ghidra's resources if needed. */
private HyperlinkEvent maybeCreateNewHyperlinkEventWithUpdatedURL(HyperlinkEvent event) {
Element element = event.getSourceElement();
if (element == null) {
return event;// this shouldn't happen since we were triggered from an A tag
}
AttributeSet a = element.getAttributes();
AttributeSet anchor = (AttributeSet) a.getAttribute(HTML.Tag.A);
if (anchor == null) {
return event;// this shouldn't happen since we were triggered from an A tag
}
String HREF = (String) anchor.getAttribute(HTML.Attribute.HREF);
Msg.trace(this, "HREF of <a> tag: " + HREF);
URL newUrl = getURLForHREFFromResources(HREF);
if (newUrl == null) {
return event;// unable to locate a resource by the name--bad link!
}
return new HyperlinkEvent(event.getSource(), event.getEventType(), newUrl,
event.getDescription(), event.getSourceElement());
}
@Override
public View create(Element e) {
AttributeSet attributes = e.getAttributes();
Object elementName = attributes.getAttribute(AbstractDocument.ElementNameAttribute);
if (elementName != null) {
// not an HTML element
return super.create(e);
}
Object html = attributes.getAttribute(StyleConstants.NameAttribute);
if (html instanceof HTML.Tag) {
HTML.Tag tag = (Tag) html;
if (tag == HTML.Tag.IMG) {
return new GHelpImageView(e);
}
}
return super.create(e);
}
public int getTextIndex() {
return EventQueueWait.exec(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
String href = getText();
int hRefIndex = 0;
int current = 0;
JEditorPane editor = (JEditorPane) parent.getComponent();
HTMLDocument document = (HTMLDocument) editor.getDocument();
Iterator iterator = document.getIterator(Tag.A);
while (iterator.isValid()) {
if (current++ >= index) {
return hRefIndex;
}
String attribute = ((HTMLDocument) ((JEditorPane) parent.getComponent()).getDocument())
.getText(iterator.getStartOffset(), iterator.getEndOffset() - iterator.getStartOffset());
if (attribute != null && attribute.equals(href)) {
hRefIndex++;
}
iterator.next();
}
return -1;
}
});
}
private List<IJavaElement> selectByProperties(final ArrayList<IJavaElement> r, JSONObject o) {
final Properties p;
if (o.has("select")) {
String spec = o.getString("select");
if (!spec.startsWith("text=") && !spec.startsWith("link=")) {
int pos = Integer.parseInt(spec);
return Arrays.asList((IJavaElement) new JEditorPanePosJavaElement(this, pos));
}
p = parseSelectProperties(spec);
} else {
p = PropertyHelper.asProperties(o);
}
EventQueueWait.exec(new Runnable() {
@Override
public void run() {
fillElements(Tag.A, r, new PropertyPredicate(p));
}
});
return r;
}
public int getHRefIndex() {
return EventQueueWait.exec(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
String href = getAttribute("href");
int hRefIndex = 0;
int current = 0;
JEditorPane editor = (JEditorPane) parent.getComponent();
HTMLDocument document = (HTMLDocument) editor.getDocument();
Iterator iterator = document.getIterator(Tag.A);
while (iterator.isValid()) {
if (current++ >= index) {
return hRefIndex;
}
AttributeSet attributes = iterator.getAttributes();
if (attributes != null) {
Object attributeObject = attributes.getAttribute(HTML.Attribute.HREF);
if (attributeObject != null) {
String attribute = attributeObject.toString();
if (attribute.equals(href)) {
hRefIndex++;
}
}
}
iterator.next();
}
return -1;
}
});
}
private void fillElements(Tag tag, ArrayList<IJavaElement> r, Predicate predicate) {
HTMLDocument document = (HTMLDocument) ((JEditorPane) getComponent()).getDocument();
Iterator iterator = document.getIterator(tag);
int index = 0;
while (iterator.isValid()) {
JEditorPaneTagJavaElement e = new JEditorPaneTagJavaElement(this, tag, index++);
if (predicate.isValid(e)) {
r.add(e);
}
iterator.next();
}
}
private Tag findTag(String tagName) {
for (Tag tag : allTags) {
if (tagName.toUpperCase().equals(tag.toString().toUpperCase())) {
return tag;
}
}
return null;
}
@Override
public void handleStartTag(Tag tag, MutableAttributeSet attributes, int pos) {
if (Tag.A.equals(tag)) {
String href = (String) attributes.getAttribute(HTML.Attribute.HREF);
hrefList.add(href.replace('\\', '/'));
tagPositions.add(pos);
}
}
public JEditorPaneTagJavaElement(JEditorPaneJavaElement parent, Tag tag, int index) {
super(parent);
this.parent = parent;
this.tag = tag;
this.index = index;
}
public void handleStartTag(Tag t, MutableAttributeSet a, int p) {
}
public void handleStartTag(Tag t, MutableAttributeSet a, int p) {
}
public void handleStartTag(Tag t, MutableAttributeSet a, int p) {
}
/**
* Creates an incoming message tag.
*
* @param messageID the identifier
* @param contactName the name of the contact sending the message
* @param contactDisplayName the display name of the contact sending the
* message
* @param avatarPath the path to the avatar file
* @param date the date, when the message was sent
* @param message the message content
* @param contentType the content type HTML or PLAIN_TEXT
* @param isHistory indicates if this is a message coming from history
* @return the created incoming message tag
*/
private static String createSimpleIncomingMessageTag(
String messageID,
String contactName,
String contactDisplayName,
String avatarPath,
Date date,
String message,
String contentType,
boolean isHistory)
{
StringBuilder headerBuffer = new StringBuilder();
SimpleDateFormat sdf = new SimpleDateFormat(HistoryService.DATE_FORMAT);
headerBuffer.append("<h2 id=\"").append(MESSAGE_HEADER_ID).append("\" ");
headerBuffer.append(DATE_ATTRIBUTE).append("=\"")
.append(sdf.format(date)).append("\">");
headerBuffer.append("<a style=\"color:");
headerBuffer.append(MSG_IN_NAME_FOREGROUND).append(";");
headerBuffer.append("font-weight:bold;");
headerBuffer.append("text-decoration:none;\" ");
headerBuffer.append("href=\"").append(contactName).append("\">");
headerBuffer.append(
contactDisplayName).append(createEditedAtTag(messageID, -1));
headerBuffer.append("</a>");
headerBuffer.append("</h2>");
final StringBuilder messageBuff = new StringBuilder();
messageBuff.append("<table width=\"100%\" ");
messageBuff.append(NAME_ATTRIBUTE).append("=\"")
.append(Tag.TABLE.toString())
.append("\" id=\"messageHeader\" ");
messageBuff.append("style=\"background-color:");
messageBuff.append(MSG_NAME_BACKGROUND).append(";\">");
messageBuff.append("<tr>");
messageBuff.append("<td align=\"left\" >");
messageBuff.append(headerBuffer.toString());
messageBuff.append("</td>");
messageBuff.append("<td align=\"right\"><h2>");
messageBuff.append(getDateString(date))
.append(GuiUtils.formatTime(date));
messageBuff.append("</h2></td>");
messageBuff.append("</tr>");
messageBuff.append("</table>");
messageBuff.append(
createSimpleMessageTag( messageID,
contactName,
message,
contentType,
date,
false,
isHistory));
return messageBuff.toString();
}
/**
* Create an outgoing message tag.
*
* @param messageID the identifier of the message
* @param contactName the name of the account sending the message
* @param contactDisplayName the display name of the account sending the
* message
* @param avatarPath the path to the avatar image
* @param date the date, when the message was sent
* @param message the content of the message
* @param contentType the content type HTML or PLAIN_TEXT
* @param isHistory indicates if this is a message coming from history
* @return the created outgoing message tag
*/
private static String createSimpleOutgoingMessageTag( String messageID,
String contactName,
String contactDisplayName,
String avatarPath,
Date date,
String message,
String contentType,
boolean isHistory)
{
StringBuilder headerBuffer = new StringBuilder();
SimpleDateFormat sdf = new SimpleDateFormat(HistoryService.DATE_FORMAT);
headerBuffer.append("<h3 id=\"").append(MESSAGE_HEADER_ID).append("\" ");
headerBuffer.append(DATE_ATTRIBUTE).append("=\"")
.append(sdf.format(date)).append("\">");
headerBuffer.append("<a style=\"color:#535353;");
headerBuffer.append("font-weight:bold;");
headerBuffer.append("text-decoration:none;\" ");
headerBuffer.append("href=\"").append(contactName).append("\">");
headerBuffer.append(contactDisplayName)
.append(createEditedAtTag(messageID, -1));
headerBuffer.append("</a>");
headerBuffer.append("</h3>");
StringBuffer messageBuff = new StringBuffer();
messageBuff.append("<table width=\"100%\" ");
messageBuff.append(NAME_ATTRIBUTE).append("=\"")
.append(Tag.TABLE.toString())
.append("\" id=\"messageHeader\"");
messageBuff.append("style=\"background-color:");
messageBuff.append(MSG_NAME_BACKGROUND).append(";\">");
messageBuff.append("<tr>");
messageBuff.append("<td align=\"left\" >");
messageBuff.append(headerBuffer.toString());
messageBuff.append("</td>");
messageBuff.append("<td align=\"right\"><h3>");
messageBuff.append(getDateString(date))
.append(GuiUtils.formatTime(date));
messageBuff.append("</h3></td>");
messageBuff.append("</tr>");
messageBuff.append("</table>");
messageBuff.append(
createSimpleMessageTag( messageID,
contactName,
message,
contentType,
date,
false,
isHistory));
return messageBuff.toString();
}