/** * Construct a new plain text content with the given text. */ public static TextContent plainText(String text) { TextContent content = new TextContent(); content.setText(text); return content; }
/** * Returns the XHTML content of this text content, or {@code null} if no such * element exists. */ public XmlBlob getXhtml() { // Non-xhtml types don't have a div element. if (getType() != Content.Type.XHTML) { return null; } XmlBlob div = getElement(DIV); if (div == null) { div = new XmlBlob(DIV); setXhtml(div); } return div; } /**
/** * Returns {@code true} if there is no content element for this text content. */ public boolean isEmpty() { return StringUtil.isEmpty(getText()) && getElementCount() == 0; }
/** * Construct a new html text content with the given html. */ public static TextContent html(String html) { TextContent content = new TextContent(); content.setHtml(html); return content; }
/** * Construct a new Xhtml text content from the given div. */ public static TextContent xhtml(XmlBlob div) { TextContent content = new TextContent(); content.setXhtml(div); return content; }
/** * {@inheritDoc} * * In the case of plain text or html content, we resolve a null content body * to an empty string. */ @Override public Element resolve(ElementMetadata<?,?> metadata, ValidationContext vc) { int type = getType(); if (type == IContent.Type.TEXT && Service.getVersion().isBefore(Service.Versions.V2) && getAttributeValue(Content.TYPE) == null) { setAttributeValue(Content.TYPE, "text"); } if (type == IContent.Type.TEXT || type == IContent.Type.HTML) { if (getTextValue() == null) { setTextValue(""); } } return super.resolve(metadata, vc); }
/** * Returns a plain-text representation of this text content. For content that * is already plain text, this just returns the text value, but for html and * xhtml it first converts the data into a plain text representation before * returning it. */ public String getPlainText() { switch (getType()) { case IContent.Type.XHTML: return getXhtml().getBlob(); case IContent.Type.TEXT: return getText(); case IContent.Type.HTML: return HtmlToText.htmlToPlainText(getText()); default: throw new IllegalStateException("Shouldn't be possible, " + "getType can only return TEXT, HTML, or XHTML."); } }
@Override public Element resolve(ElementMetadata<?, ?> meta, ValidationContext vc) { String titleAttribute = getAttributeValue(TITLE); TextContent title = getElement(Source.TITLE); // Make sure that the title is in both the attribute and element. if (titleAttribute != null) { if (title == null) { title = TextContent.plainText(titleAttribute); addElement(Source.TITLE, title); } else { String titleContent = title.getPlainText(); // Verify that the attribute and element have the same value. if (!titleAttribute.equals(titleContent)) { vc.addError(this, CoreErrorDomain.ERR.duplicateTitle); } } } else if (title != null) { titleAttribute = title.getPlainText(); setAttributeValue(TITLE, titleAttribute); } return super.resolve(meta, vc); }
/** * Assumes the <content> element's contents are plain-text and returns its * value as a string * * @return A string containing the plain-text value of the content tag. * * @throws IllegalStateException If the content element is not a text type. */ public String getPlainTextContent() { TextContent content = getTextContent(); return (content == null) ? null : content.getPlainText(); }
/** * Returns the text content of this element, if this is a plain text or html * text content. If this is an XHTML text content this method returns null. * If you want a plain-text representation of the XHTML content call * {@link #getPlainText()} instead. */ public String getText() { return getTextValue(KEY); }
/** * Returns the type of this content, either {@link Content.Type#TEXT}, * {@link Content.Type#HTML}, or {@link Content.Type#XHTML}. If the value * of the {@link #TYPE} attribute is unknown, plain text * {@link Content.Type#TEXT} will be returned. */ @Override public int getType() { int type = getType(this); return (type == UNKNOWN_TYPE) ? IContent.Type.TEXT : type; }
/** * Backwards-compatibility method, exactly the same as {@link #getText()}. */ public String getHtml() { return getText(); }
/** * {@inheritDoc} * * In the case of plain text or html content, we resolve a null content body * to an empty string. */ @Override public Element resolve(ElementMetadata<?,?> metadata, ValidationContext vc) { int type = getType(); if (type == IContent.Type.TEXT && Service.getVersion().isBefore(Service.Versions.V2) && getAttributeValue(Content.TYPE) == null) { setAttributeValue(Content.TYPE, "text"); } if (type == IContent.Type.TEXT || type == IContent.Type.HTML) { if (getTextValue() == null) { setTextValue(""); } } return super.resolve(metadata, vc); }
/** * Returns a plain-text representation of this text content. For content that * is already plain text, this just returns the text value, but for html and * xhtml it first converts the data into a plain text representation before * returning it. */ public String getPlainText() { switch (getType()) { case IContent.Type.XHTML: return getXhtml().getBlob(); case IContent.Type.TEXT: return getText(); case IContent.Type.HTML: return HtmlToText.htmlToPlainText(getText()); default: throw new IllegalStateException("Shouldn't be possible, " + "getType can only return TEXT, HTML, or XHTML."); } }
@Override public Element resolve(ElementMetadata<?, ?> meta, ValidationContext vc) { String titleAttribute = getAttributeValue(TITLE); TextContent title = getElement(Source.TITLE); // Make sure that the title is in both the attribute and element. if (titleAttribute != null) { if (title == null) { title = TextContent.plainText(titleAttribute); addElement(Source.TITLE, title); } else { String titleContent = title.getPlainText(); // Verify that the attribute and element have the same value. if (!titleAttribute.equals(titleContent)) { vc.addError(this, CoreErrorDomain.ERR.duplicateTitle); } } } else if (title != null) { titleAttribute = title.getPlainText(); setAttributeValue(TITLE, titleAttribute); } return super.resolve(meta, vc); }
/** * Construct a new html text content with the given html. */ public static TextContent html(String html) { TextContent content = new TextContent(); content.setHtml(html); return content; }
/** * Assumes the <content> element's contents are plain-text and returns its * value as a string * * @return A string containing the plain-text value of the content tag. * * @throws IllegalStateException If the content element is not a text type. */ public String getPlainTextContent() { TextContent content = getTextContent(); return (content == null) ? null : content.getPlainText(); }
/** * Construct a new Xhtml text content from the given div. */ public static TextContent xhtml(XmlBlob div) { TextContent content = new TextContent(); content.setXhtml(div); return content; }