Nekit
v 0.9

com.nekhem.nekit
Class NekitDocument

java.lang.Object
  |
  +--javax.swing.text.AbstractDocument
        |
        +--javax.swing.text.DefaultStyledDocument
              |
              +--javax.swing.text.html.HTMLDocument
                    |
                    +--com.nekhem.nekit.NekitDocument
All Implemented Interfaces:
javax.swing.text.Document, java.io.Serializable, javax.swing.text.StyledDocument

public class NekitDocument
extends javax.swing.text.html.HTMLDocument

All the routines to actually manipulate the text Ovverrides some of the methods of HTMLDocument to workaround some problems FIXME: reduce redundancy

See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class javax.swing.text.html.HTMLDocument
javax.swing.text.html.HTMLDocument.BlockElement, javax.swing.text.html.HTMLDocument.HTMLReader, javax.swing.text.html.HTMLDocument.Iterator, javax.swing.text.html.HTMLDocument.RunElement
 
Nested classes inherited from class javax.swing.text.DefaultStyledDocument
javax.swing.text.DefaultStyledDocument.AttributeUndoableEdit, javax.swing.text.DefaultStyledDocument.ElementBuffer, javax.swing.text.DefaultStyledDocument.ElementSpec, javax.swing.text.DefaultStyledDocument.SectionElement
 
Nested classes inherited from class javax.swing.text.AbstractDocument
javax.swing.text.AbstractDocument.AbstractElement, javax.swing.text.AbstractDocument.AttributeContext, javax.swing.text.AbstractDocument.BranchElement, javax.swing.text.AbstractDocument.Content, javax.swing.text.AbstractDocument.DefaultDocumentEvent, javax.swing.text.AbstractDocument.ElementEdit, javax.swing.text.AbstractDocument.LeafElement
 
Field Summary
 java.util.Vector paragraphTags
          All the tags that define a paragraph
 java.util.Vector titleTags
          All tags that define a title
 
Fields inherited from class javax.swing.text.html.HTMLDocument
AdditionalComments
 
Fields inherited from class javax.swing.text.DefaultStyledDocument
buffer, BUFFER_SIZE_DEFAULT
 
Fields inherited from class javax.swing.text.AbstractDocument
BAD_LOCATION, BidiElementName, ContentElementName, ElementNameAttribute, listenerList, ParagraphElementName, SectionElementName
 
Fields inherited from interface javax.swing.text.Document
StreamDescriptionProperty, TitleProperty
 
Method Summary
 void adjustEnd()
          Ensures the presence of a paragraph at the end of the document.
 boolean anyActiveHere(int pos, java.util.Vector tags)
          Is the offset given included in any of the tags given?
 javax.swing.text.html.HTMLDocument.BlockElement attachBlockTag(int index, int removeQty, javax.swing.text.html.HTMLDocument.BlockElement parent, javax.swing.text.Element attrSource)
          Create a copy BlockElement and attach it to 'parent' at position 'index' replacing 'removeQty' elements
 javax.swing.text.html.HTMLDocument.RunElement attachLeaf(int start, int end, javax.swing.text.html.HTMLDocument.BlockElement parent, javax.swing.text.Element attrSource)
          Create a copy RunElement and attach it to 'parent' with range given
 boolean charDeletable(int pos)
          Can somebody delete thar char?
 void clearFormat(int start, int end)
          Format the given region to normal text
 java.lang.String cutHtmlSlice(int offset, int length)
          Remove a region of the document and get a HTML representation of it
 void dumpAttributes(int caretPos)
          Debug method that writes the Element structure at offset given
 com.nekhem.nekit.Interval findLineEnds(int start, int end)
          Find the minimal region, delimited by newlines, that includes the range given
 javax.swing.text.Element findTagElement(int offset, javax.swing.text.html.HTML.Tag tag)
          Find the Element that includes this offset and that corresponds to this tag
 java.lang.String getAlignment(int offset)
          Get the tipe of alignment ("","center","right")
 java.lang.String getChar(int pos)
          Get the "one char string" at the position
 javax.swing.text.html.HTMLDocument getDocument()
          getter
 java.lang.String getHtmlSlice(int offset, int length)
          Get a HTML representation of a region of the document
 javax.swing.text.html.HTML.Tag getTag(javax.swing.text.Element el)
          Get the tag corresponding to the Element
 javax.swing.text.html.HTMLDocument.BlockElement insertBlockTag(javax.swing.text.html.HTMLDocument.BlockElement parent, javax.swing.text.Element attrSource)
          Insert a copy of attrSource at the start of parent
 void insertBreak(int pos)
          Insert a BR tag (simple newline)
 void insertHRule(int caretPos)
          Method for inserting a horizontal rule (HR) element
 void insertHtml(int offset, java.lang.String html)
          Insert a piece of HTML code
 void insertParagraphBreak(int pos)
          Method for inserting a Parag (P) element
 boolean isSimpleBlank(int pos)
          Is the char at position 'pos' a space char?
 boolean isTag(javax.swing.text.Element el, javax.swing.text.html.HTML.Tag tag)
          Does the Element correspond to this tag?
 boolean oneOfTheeseTagsInRange(int start, int end, java.lang.Object[] tags)
          Is there any of the tags listed in range given?
 boolean oneOfTheeseTagsInRange(int start, int end, java.util.Vector tags)
          Is there any of the tags listed in range given?
 int paragraphize(int start, int end)
          Transform the given region in a separated paragraph
 void remove(int start, int length)
          "No exception" remove()
 boolean removeBreak(int pos)
          if at pos there is a break (tag BR) remove it
 boolean removeListElementIfVoid(int pos)
          If the current position is in a List Element and the Element has no characters, remove it.
 boolean removeParagraphIfVoid(int pos)
          If the current position contains a null paragraph, remove it
static java.lang.String removeUnNeeded(java.lang.String html)
          Keep only the part between body tags.
 java.lang.String safeGetText(int offset, int length)
          No exception getText (the limits are adjusted to become legal)
 void setBaseAttributes(javax.swing.text.AttributeSet as)
           
 void setTagsAttributes(int start, int end, java.util.Vector tags, javax.swing.text.html.HTML.Attribute attrId, java.lang.String attrvalue)
          Give to any tag in 'tags' that stays in the given range, the attibute 'attrId=attrvalue'
 void simpleInsert(int pos, java.lang.String text)
          "no Exception" insertString() this is not good practice, but I needed more readable algoritms in the development phase....
 void splitList(int pos)
          Split the current "bullet list"-line in two.
 boolean tagActiveHere(int pos, javax.swing.text.html.HTML.Tag tag)
          Is the offset given included in the tag given?
 java.lang.String textInRange(int start, int end)
          Get the region of text
 void transferLeafs(javax.swing.text.html.HTMLDocument.RunElement[] array, javax.swing.text.html.HTMLDocument.BlockElement parent)
          Move Elements to a new parent
 
Methods inherited from class javax.swing.text.html.HTMLDocument
create, createBranchElement, createDefaultRoot, createLeafElement, fireChangedUpdate, fireUndoableEditUpdate, getBase, getElement, getElement, getIterator, getParser, getPreservesUnknownTags, getReader, getReader, getStyleSheet, getTokenThreshold, insert, insertAfterEnd, insertAfterStart, insertBeforeEnd, insertBeforeStart, insertUpdate, processHTMLFrameHyperlinkEvent, setBase, setInnerHTML, setOuterHTML, setParagraphAttributes, setParser, setPreservesUnknownTags, setTokenThreshold
 
Methods inherited from class javax.swing.text.DefaultStyledDocument
addDocumentListener, addStyle, getBackground, getCharacterElement, getDefaultRootElement, getFont, getForeground, getLogicalStyle, getParagraphElement, getStyle, getStyleNames, removeDocumentListener, removeStyle, removeUpdate, setCharacterAttributes, setLogicalStyle, styleChanged
 
Methods inherited from class javax.swing.text.AbstractDocument
addUndoableEditListener, createPosition, dump, fireInsertUpdate, fireRemoveUpdate, getAsynchronousLoadPriority, getAttributeContext, getBidiRootElement, getContent, getCurrentWriter, getDocumentFilter, getDocumentListeners, getDocumentProperties, getEndPosition, getLength, getListeners, getProperty, getRootElements, getStartPosition, getText, getText, getUndoableEditListeners, insertString, postRemoveUpdate, putProperty, readLock, readUnlock, removeUndoableEditListener, render, replace, setAsynchronousLoadPriority, setDocumentFilter, setDocumentProperties, writeLock, writeUnlock
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.swing.text.Document
addUndoableEditListener, createPosition, getEndPosition, getLength, getProperty, getRootElements, getStartPosition, getText, getText, insertString, putProperty, removeUndoableEditListener, render
 

Field Detail

paragraphTags

public java.util.Vector paragraphTags
All the tags that define a paragraph


titleTags

public java.util.Vector titleTags
All tags that define a title

Method Detail

setBaseAttributes

public void setBaseAttributes(javax.swing.text.AttributeSet as)
Parameters:
as -

getDocument

public javax.swing.text.html.HTMLDocument getDocument()
getter

Returns:
the document

simpleInsert

public void simpleInsert(int pos,
                         java.lang.String text)
"no Exception" insertString() this is not good practice, but I needed more readable algoritms in the development phase.... FIXME: find another way

Parameters:
pos - insert position
text - ascii text to insert

textInRange

public java.lang.String textInRange(int start,
                                    int end)
Get the region of text

Parameters:
start - initial position
end - end position
Returns:
the ascii text

safeGetText

public java.lang.String safeGetText(int offset,
                                    int length)
No exception getText (the limits are adjusted to become legal)

Parameters:
offset - start position
length - -
Returns:
the ascii text

getChar

public java.lang.String getChar(int pos)
Get the "one char string" at the position

Parameters:
pos - offset position
Returns:
the char

remove

public void remove(int start,
                   int length)
"No exception" remove()

Specified by:
remove in interface javax.swing.text.Document
Overrides:
remove in class javax.swing.text.AbstractDocument
Parameters:
start -
length -

removeListElementIfVoid

public boolean removeListElementIfVoid(int pos)
If the current position is in a List Element and the Element has no characters, remove it.

Parameters:
pos - the offset
Returns:
true if removed

insertHtml

public void insertHtml(int offset,
                       java.lang.String html)
Insert a piece of HTML code

Parameters:
offset - insertion position
html - html code

getHtmlSlice

public java.lang.String getHtmlSlice(int offset,
                                     int length)
Get a HTML representation of a region of the document

Parameters:
offset - start position
length - number of document characters (tags are not considered)
Returns:
HTML code

cutHtmlSlice

public java.lang.String cutHtmlSlice(int offset,
                                     int length)
Remove a region of the document and get a HTML representation of it

Parameters:
offset - start position
length - number of document characters
Returns:
the HTML code of the region removed

getTag

public javax.swing.text.html.HTML.Tag getTag(javax.swing.text.Element el)
Get the tag corresponding to the Element

Parameters:
el - the Element
Returns:
the tag

isTag

public boolean isTag(javax.swing.text.Element el,
                     javax.swing.text.html.HTML.Tag tag)
Does the Element correspond to this tag?

Parameters:
el -
tag -

findTagElement

public javax.swing.text.Element findTagElement(int offset,
                                               javax.swing.text.html.HTML.Tag tag)
Find the Element that includes this offset and that corresponds to this tag

Parameters:
offset - textual offset
tag - -t
Returns:
the Element found or null

oneOfTheeseTagsInRange

public boolean oneOfTheeseTagsInRange(int start,
                                      int end,
                                      java.lang.Object[] tags)
Is there any of the tags listed in range given?

Parameters:
start - range start
end - range end
tags - tag list
Returns:
true if yes

oneOfTheeseTagsInRange

public boolean oneOfTheeseTagsInRange(int start,
                                      int end,
                                      java.util.Vector tags)
Is there any of the tags listed in range given?

Parameters:
start -
end -
tags -

insertBlockTag

public javax.swing.text.html.HTMLDocument.BlockElement insertBlockTag(javax.swing.text.html.HTMLDocument.BlockElement parent,
                                                                      javax.swing.text.Element attrSource)
Insert a copy of attrSource at the start of parent

Parameters:
parent -
attrSource -

attachBlockTag

public javax.swing.text.html.HTMLDocument.BlockElement attachBlockTag(int index,
                                                                      int removeQty,
                                                                      javax.swing.text.html.HTMLDocument.BlockElement parent,
                                                                      javax.swing.text.Element attrSource)
Create a copy BlockElement and attach it to 'parent' at position 'index' replacing 'removeQty' elements

Parameters:
index - order number in 'parent'
removeQty - how many childs of 'parent' remove?
parent - branch to attach to
attrSource - the Element to copy from
Returns:
the new Block element

attachLeaf

public javax.swing.text.html.HTMLDocument.RunElement attachLeaf(int start,
                                                                int end,
                                                                javax.swing.text.html.HTMLDocument.BlockElement parent,
                                                                javax.swing.text.Element attrSource)
Create a copy RunElement and attach it to 'parent' with range given

Parameters:
start - range start
end - range end
parent -
attrSource - the Element to copy
Returns:
the created Element

transferLeafs

public void transferLeafs(javax.swing.text.html.HTMLDocument.RunElement[] array,
                          javax.swing.text.html.HTMLDocument.BlockElement parent)
Move Elements to a new parent

Parameters:
array - The Elements to move
parent - The new parent

tagActiveHere

public boolean tagActiveHere(int pos,
                             javax.swing.text.html.HTML.Tag tag)
Is the offset given included in the tag given?

Parameters:
pos - the offset
tag - the searched tag
Returns:
true if yes

anyActiveHere

public boolean anyActiveHere(int pos,
                             java.util.Vector tags)
Is the offset given included in any of the tags given?

Parameters:
pos - the offset
tags - the tag list
Returns:
true if yes

isSimpleBlank

public boolean isSimpleBlank(int pos)
Is the char at position 'pos' a space char?

Parameters:
pos - offset
Returns:
true if yes

removeBreak

public boolean removeBreak(int pos)
if at pos there is a break (tag BR) remove it

Parameters:
pos - the offset
Returns:
true if removed

getAlignment

public java.lang.String getAlignment(int offset)
Get the tipe of alignment ("","center","right")

Parameters:
offset - -
Returns:
the value of ALIGN attribute

findLineEnds

public com.nekhem.nekit.Interval findLineEnds(int start,
                                              int end)
Find the minimal region, delimited by newlines, that includes the range given

Parameters:
start - range start
end - range end
Returns:
The limits of the region found

dumpAttributes

public void dumpAttributes(int caretPos)
Debug method that writes the Element structure at offset given

Parameters:
caretPos - the offset

insertHRule

public void insertHRule(int caretPos)
                 throws java.io.IOException,
                        javax.swing.text.BadLocationException,
                        java.lang.RuntimeException
Method for inserting a horizontal rule (HR) element

Parameters:
caretPos -
Throws:
java.io.IOException
javax.swing.text.BadLocationException
java.lang.RuntimeException

insertParagraphBreak

public void insertParagraphBreak(int pos)
                          throws java.io.IOException,
                                 javax.swing.text.BadLocationException,
                                 java.lang.RuntimeException
Method for inserting a Parag (P) element

java.io.IOException
javax.swing.text.BadLocationException
java.lang.RuntimeException

insertBreak

public void insertBreak(int pos)
                 throws java.io.IOException,
                        javax.swing.text.BadLocationException,
                        java.lang.RuntimeException
Insert a BR tag (simple newline)

Parameters:
pos - the offset
Throws:
java.io.IOException
javax.swing.text.BadLocationException
java.lang.RuntimeException

removeUnNeeded

public static java.lang.String removeUnNeeded(java.lang.String html)
Keep only the part between body tags. This is because we work with HTML fragments while HTMLEditorKit works with complete HTML pages. FIXME: overlaps with NekitPane.removeGarbage

Parameters:
html - a complete HTML Document
Returns:
the interesting part of HTML document

setTagsAttributes

public void setTagsAttributes(int start,
                              int end,
                              java.util.Vector tags,
                              javax.swing.text.html.HTML.Attribute attrId,
                              java.lang.String attrvalue)
Give to any tag in 'tags' that stays in the given range, the attibute 'attrId=attrvalue'

Parameters:
start - range start
end - range end
tags - list of tags that must receive the attribute
attrId - the attribute name
attrvalue - the attribute value

adjustEnd

public void adjustEnd()
Ensures the presence of a paragraph at the end of the document. FIXME: could be no more needed


charDeletable

public boolean charDeletable(int pos)
Can somebody delete thar char? To simplify the user interface some deletions are forbidden: - at the boundary between normal text and bullet lists - at the boundary between two different alignments

Parameters:
pos - the character position
Returns:
true if yes

paragraphize

public int paragraphize(int start,
                        int end)
Transform the given region in a separated paragraph

Parameters:
start - start of the region
end - end of the region
Returns:
the new position of the start of the region

clearFormat

public void clearFormat(int start,
                        int end)
Format the given region to normal text

Parameters:
start - start of region
end - end of region

splitList

public void splitList(int pos)
Split the current "bullet list"-line in two. This method stays here because some of the methods used can be called only within the Document class, but should stay in ListContext.

Parameters:
pos - position of the split
See Also:
ListContext

removeParagraphIfVoid

public boolean removeParagraphIfVoid(int pos)
If the current position contains a null paragraph, remove it

Parameters:
pos - offset
Returns:
true if removed

Nekit
v 0.9

Submit a bug or feature
Nekhem Technologies