Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

structured Document elements

Status
Not open for further replies.

dontj

Programmer
Feb 8, 2005
5
0
0
US
I am trying to create a hierarchical Document where I have a root element and some headers that are children of that root like this:


BranchElement(section)
LeafElement(content)
root
BranchElement(paragraph)
LeafElement(content)
Header 1
BranchElement(paragraph)
LeafElement(content)
Header 2
BranchElement(paragraph)
LeafElement(content)
Header 3
BranchElement(paragraph)
LeafElement(content)


The Java source for my custom Document is listed below. Its producing output like this which I don't want:


BranchElement(section)
LeafElement(content)
root
BranchElement(paragraph)
LeafElement(content)
Header 1
BranchElement(paragraph)
LeafElement(content)
Header 2
BranchElement(paragraph)
LeafElement(content)
Header 3
BranchElement(paragraph)
LeafElement(content)


Everytime I add a Element it becomes a child of the previous element. How can I manipulate the ElementSpecs to generate the hierarchy at the top that I want? Thanks.

Code:
class CustomDocument extends DefaultStyledDocument {

public void insertRoot(String root) {
  char[] chars = root.toCharArray();
  int length = chars.length;
		
  ElementSpec[] es = new ElementSpec[] {
    new ElementSpec(null, ElementSpec.StartTagType),
    new ElementSpec(null, ElementSpec.ContentType, chars, 0, length),
    new ElementSpec(null, ElementSpec.EndTagType)	
  };					
  create(es);						
}
	
public void insertHeader(String header) {
  char[] chars = header.toCharArray();
  int length = chars.length;
  int offset = getLength();
		
  ElementSpec[] es = new ElementSpec[] {		
    new ElementSpec(null, ElementSpec.StartTagType),
    new ElementSpec(null, ElementSpec.ContentType, chars, 0, length),
    new ElementSpec(null, ElementSpec.EndTagType)
  };						
					
  try {
    insert(offset, es);
  } catch (BadLocationException e) {			
  }				
}      

  public static void main(String a[]) {
    CustomDocument doc = new CustomDocument(); 
        
    doc.insertRoot("root");
    doc.insertHeader("Header 1");
    doc.insertHeader("Header 2");
    doc.insertHeader("Header 3");	
  }
}


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top