public static String getIndented(String inHTML) { String formated_html = null; try { StringWriter writer = new StringWriter(); new SourceFormatter(new Source(inHTML)).setIndentString(" ").setTidyTags(true).setCollapseWhiteSpace(true).writeTo(writer); formated_html = writer.toString(); } catch (IOException e) { LOG.log(Level.SEVERE, null, e); } return formated_html; }
Source htmlSource = new Source(htmlText); Segment htmlSeg = new Segment(htmlSource, 0, htmlSource.length()); Renderer htmlRend = new Renderer(htmlSeg); System.out.println(htmlRend.toString());
void replaceInOutputDocument(final OutputDocument outputDocument) { if (outputStyle==FormControlOutputStyle.REMOVE) { outputDocument.remove(getElement()); } else if (outputStyle==FormControlOutputStyle.DISPLAY_VALUE) { outputDocument.replace(getElement(),getDisplayValueHTML(getValue(),true)); } else { replaceAttributesInOutputDocumentIfModified(outputDocument); if (value!=UNCHANGED) outputDocument.replace(getElement().getContent(),CharacterReference.encode(value,false)); } } private String getValue() {
/** * Returns the raw (not {@linkplain CharacterReference#decode(CharSequence) decoded}) value of the attribute, or null if the attribute {@linkplain Attribute#hasValue() has no value}. * <p> * This is an internal convenience method. * * @return the raw (not {@linkplain CharacterReference#decode(CharSequence) decoded}) value of the attribute, or null if the attribute {@linkplain Attribute#hasValue() has no value}. */ String getRawValue(final String name) { final Attribute attribute=get(name); return attribute==null || !attribute.hasValue() ? null : attribute.getValueSegment().toString(); }
private StreamedSource(final Reader reader, final boolean isInternallyCreatedReader, final String encoding, final String encodingSpecificationInfo, final String preliminaryEncodingInfo) throws IOException { this.reader=reader; if (isInternallyCreatedReader) internallyCreatedReaderFinalizer=new InternallyCreatedReaderFinalizer(reader); streamedText=new StreamedText(reader); streamedParseText=new StreamedParseText(streamedText); source=new Source(streamedText,streamedParseText,encoding,encodingSpecificationInfo,preliminaryEncodingInfo); }
protected Tag constructTagAt(final Source source, final int pos) { final int closingDelimiterPos=source.getParseText().indexOf('>',pos+1); if (closingDelimiterPos==-1) return null; final Tag tag=constructStartTag(source,pos,closingDelimiterPos+1,"",null); if (source.logger.isErrorEnabled()) source.logger.error(source.getRowColumnVector(tag.getBegin()).appendTo(new StringBuilder(200).append("Encountered possible StartTag at ")).append(" whose content does not match a registered StartTagType").toString()); return tag; } }
public void appendTo(final Appendable appendable) throws IOException { this.appendable=appendable; if (segment instanceof Source) ((Source)segment).fullSequentialParse(); nextTag=segment.source.getNextTag(segment.begin); index=segment.begin; appendContent(segment.end,segment.getChildElements(),0); }
/** * Returns the character used to quote the value. * <p> * The return value is either a double-quote (<code>"</code>), a single-quote (<code>'</code>), or a space. * * @return the character used to quote the value, or a space if the value is not quoted or this attribute has no value. */ public char getQuoteChar() { if (valueSegment==valueSegmentIncludingQuotes) return ' '; // no quotes return source.charAt(valueSegmentIncludingQuotes.getBegin()); }
Tag getNextNonServerTag(int pos) { while (true) { final Tag tag=getNextTag(pos); if (tag==null) return null; if (!tag.getTagType().isServerTag()) return tag; pos=tag.end; } }
Tag getPreviousNonServerTag(int pos) { while (true) { final Tag tag=getPreviousTag(pos-1); if (tag==null) return null; if (!tag.getTagType().isServerTag()) return tag; pos=tag.begin-1; } }
private static boolean isXML(final Segment firstNonTextSegment) { if (firstNonTextSegment==null || !(firstNonTextSegment instanceof Tag)) return false; Tag tag=(Tag)firstNonTextSegment; if (tag.getTagType()==StartTagType.XML_DECLARATION) return true; // if document has a DOCTYPE declaration and it contains the text "xhtml", it is an XML document: if (tag.source.getParseText().indexOf("xhtml",tag.begin,tag.end)!=-1) return true; return false; }
static CharacterReference construct(final Source source, final int begin, final Config.UnterminatedCharacterReferenceSettings unterminatedCharacterReferenceSettings) { try { if (source.getParseText().charAt(begin)!='&') return null; return (source.getParseText().charAt(begin+1)=='#') ? NumericCharacterReference.construct(source,begin,unterminatedCharacterReferenceSettings) : CharacterEntityReference.construct(source,begin,unterminatedCharacterReferenceSettings.characterEntityReferenceMaxCodePoint); } catch (IndexOutOfBoundsException ex) { return null; } }
public Tag getPreviousTag(final int pos) { // Note that this method never returns tags for which tag.includInSearch() is false, so separate caching of unregistered tags won't work. if (cache.getSourceLength()==0) return null; if (pos<0 || pos>=cache.getSourceLength()) return null; int index=getIndexOfPos(pos); final CacheEntry cacheEntry=array[index]; final Tag tag; if (cacheEntry.pos==pos && cacheEntry.tag!=null && cacheEntry.tag.includeInSearch()) return cacheEntry.tag; tag=getPreviousTag(getPrevious(cacheEntry),pos,cacheEntry); addPreviousTag(pos,tag); return tag; }
private final void loadNextParsedSegment() { nextParsedSegment=findNextParsedSegment(); final int plainTextSegmentEnd=(nextParsedSegment!=null) ? nextParsedSegment.begin : streamedText.length(); nextSegment=(plainTextSegmentBegin<plainTextSegmentEnd) ? new Segment(source,plainTextSegmentBegin,plainTextSegmentEnd) : nextParsedSegment; if (nextParsedSegment!=null && plainTextSegmentBegin<nextParsedSegment.end) plainTextSegmentBegin=nextParsedSegment.end; }
private static CharacterReference getNext(final Source source, int pos, final Config.UnterminatedCharacterReferenceSettings unterminatedCharacterReferenceSettings) { final ParseText parseText=source.getParseText(); pos=parseText.indexOf('&',pos); while (pos!=-1) { final CharacterReference characterReference=construct(source,pos,unterminatedCharacterReferenceSettings); if (characterReference!=null) return characterReference; pos=parseText.indexOf('&',pos+1); } return null; }
private static CharacterReference getPrevious(final Source source, int pos, final Config.UnterminatedCharacterReferenceSettings unterminatedCharacterReferenceSettings) { final ParseText parseText=source.getParseText(); pos=parseText.lastIndexOf('&',pos); while (pos!=-1) { final CharacterReference characterReference=construct(source,pos,unterminatedCharacterReferenceSettings); if (characterReference!=null) return characterReference; pos=parseText.lastIndexOf('&',pos-1); } return null; }
private static boolean isInvalidEmptyElementTag(final StartTagType startTagType, final Source source, final int i, final String logType, final String tagName, final int logBegin) { // This checks whether we've found the characters "/>" but it wasn't recognised as the closing delimiter because isClosingSlashIgnored is true. if (startTagType!=StartTagType.NORMAL || !startTagType.atEndOfAttributes(source,i,false)) return false; if (source.logger.isErrorEnabled()) log(source,logType,tagName,logBegin,"contains a '/' character before the closing '>', which is ignored because tags of this name cannot be empty-element tags"); return true; }
/** * Registers this tag type for recognition by the parser. * <br />(<a href="TagType.html#RegistrationRelated">registration related</a> method) * <p> * The order of registration affects the <a href="TagType.html#Precedence">precedence</a> of the tag type when a potential tag is being parsed. * * @see #deregister() */ public final void register() { getLogger().debug("Register tag type: "+this); TagTypeRegister.add(this); }
private static boolean reachedMaxErrorCount(final int errorCount, final Source source, final String logType, final String tagName, final int logBegin, final int maxErrorCount) { if (errorCount<=maxErrorCount) return false; if (source.logger.isErrorEnabled()) log(source,logType,tagName,logBegin,"rejected because it contains too many errors"); return true; }