Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/sonoma/SiteMenu.java


1   /*
2    * dbContent.java
3    *
4    * Created on April 28, 2002, 12:13 AM
5    */
6   
7   package com.sonoma;
8   import org.w3c.dom.Document;
9   import org.w3c.dom.Node;
10  import org.w3c.dom.Element;
11  import org.w3c.dom.NamedNodeMap;
12  import org.w3c.dom.Attr;
13  import org.w3c.dom.NodeList;
14  import com.sonoma.objects.spParagraph;
15  /**
16   * SiteMenu manages Chapters and Sections.  Allowing
17   * users to add/edit/move/ and remove sections or chapters
18   * @author  Roy Hoobler
19   * @version 
20   */
21  public class SiteMenu extends Object {
22      private Node xmlCurrentNode; // current XML Node
23  
24      /** Creates new dbContent */
25      public SiteMenu() {
26      }
27      /** Creates a valid XPath with a Chapter and SectionID 
28       *@param doc is the current DocBook 
29       *@param ChapterID is the Chapter's @id attribute value
30       *@param SectionID is the sections @id attribute Value
31       */
32      public String createMenuXPath(String ChapterID, String SectionID){
33          return createXPath(ChapterID, SectionID);
34      }
35      /** Creates a valid XPath with a ChapterID
36       *@param doc is the current DocBook 
37       *@param ChapterID is the Chapter's @id attribute value
38       */
39      public String createMenuXPath(String ChapterID){
40          return createXPath(ChapterID, null);
41      }
42      private String createXPath(String ChapterID, String SectionID){
43          String sXPath = new String("");
44          if(SectionID!=null){
45               sXPath = "/book/chapter[@id='"+ChapterID+"']/sect1[@id='" + SectionID + "']";
46          }else{
47              sXPath = "/book/chapter[@id='"+ChapterID+"']";
48          }
49          return sXPath;
50      }
51      /** Creates a new Chapter in the DocBook
52       *@param doc is the current DocBook 
53       *@param CurrentChapterID is where the new chapter will be inserted (before the current)
54       *@param Title a text title for the chapter
55       *@param NewID is the @id attribute for the new chapter.
56       */
57      public final void addChapter(Document doc,String CurrentChapterID, String Title, String NewID){
58          XMLUtility myXML = new XMLUtility();
59          Node ndNewChapter;
60          Node ndNewTitle;
61          Element elNewChapter;
62          Node ndReturn;
63          Node ndParent;
64          if (NewID==null || NewID.length()<1){
65              NewID="11111";
66          }
67          
68          xmlCurrentNode = myXML.getNode(doc, createMenuXPath(CurrentChapterID));
69          ndNewChapter = myXML.createTextElement(doc,"chapter","");
70          ndNewTitle = myXML.createTextElement(doc,"title",Title);
71          ndNewChapter.appendChild(ndNewTitle);
72          elNewChapter = (Element) ndNewChapter;
73          elNewChapter.setAttribute("id",NewID);
74          if (xmlCurrentNode!=null){
75              ndParent = xmlCurrentNode.getParentNode();
76              ndParent.insertBefore(ndNewChapter,xmlCurrentNode);
77          }else{
78              ndParent = myXML.getNode(doc,"/book");
79              ndParent.insertBefore(ndNewChapter,null);
80          }
81          //create a new section
82          //create a default paragraph for this chapter
83          Node ndNewSection = addSect1(ndNewChapter,Title,NewID+"s1",myXML);
84          Node ndNewParagraph;
85          ndNewParagraph=myXML.createTextElement(doc,"title",Title);
86          ndNewSection.appendChild(ndNewParagraph);
87          ndNewParagraph=myXML.createTextElement(doc,"para","Enter Content Here");
88          ndNewSection.appendChild(ndNewParagraph);
89          ndNewChapter.appendChild(ndNewSection);
90  //        createDefaultParagraph(doc, "222");
91          
92      }
93      /** Updates a new Chapter in the DocBook
94       *@param doc is the current DocBook 
95       *@param OldID the current DocBook chapter @id attribute value
96       *@param NewTitle is the new text title for the chapter
97       *@param NewID is the new @id attribute for the chapter.
98       */
99      public void updateChapter(Document doc, String OldID, String NewID, String NewTitle){
100         //find the node and the title, change and update
101         XMLUtility myXML = new XMLUtility();
102         Node ndChapter;
103         Node ndTitle;
104         Element elChapter;
105         xmlCurrentNode = myXML.getNode(doc, createMenuXPath(OldID));
106         NamedNodeMap nmAttributes = xmlCurrentNode.getAttributes();
107         System.out.println("Update " + createMenuXPath(OldID));
108         ndTitle = myXML.getNode(doc,"/book/chapter[@id='"+OldID+"']/title");
109         System.out.println("Chapter: " + xmlCurrentNode.getNodeName());
110         System.out.println("Title: " + ndTitle.getNodeName());
111         ndTitle.getFirstChild().setNodeValue(NewTitle);
112         elChapter = (Element) xmlCurrentNode;
113         elChapter.setAttribute("id",NewID);
114     }
115     /** Removes a chapter and adds it back in a new location (before the new location)
116      *@param doc is the current DocBook 
117      *@param OldID the current DocBook chapter @id attribute value
118      *@param NewID the current chapter will be pasted before the chapter with this @id attribute.
119      */
120     public Node moveChapter(Document doc, String OldID, String NewID){
121         //remove the old chapter and add it back before the new spot
122         Node ndRemoved = removeChapter(doc,OldID);
123         XMLUtility myXML = new XMLUtility();
124         Node ndParent;
125         Node ndNewChapter = myXML.getNode(doc, createMenuXPath(NewID));
126         if (ndNewChapter!=null){
127             ndParent = ndNewChapter.getParentNode();
128             return ndParent.insertBefore(ndRemoved,ndNewChapter);
129         }else{
130             ndParent = myXML.getNode(doc,"/book");
131             return ndParent.insertBefore(ndRemoved,null);
132         }
133     }
134     /** Removes a chapter 
135      *@param doc is the current DocBook 
136      *@param OldID the current DocBook chapter @id attribute value
137      */
138     public Node removeChapter(Document doc, String OldID){
139         XMLUtility myXML = new XMLUtility();
140         Node ndParent;
141         xmlCurrentNode = myXML.getNode(doc, createMenuXPath(OldID));
142         ndParent = xmlCurrentNode.getParentNode();
143         return ndParent.removeChild(xmlCurrentNode);
144     }
145     /** adds a new section to the end of a chapter node
146      *@param ndChapter the current chapter
147      *@param Label is the value for the chapter's @label attribute
148      *@param NewID is the value for the chapter's @id attribute
149      *@myXML is a passed XMLUtility reference for creating text elements.
150      */
151     public Node addSect1(Node ndChapter, String Label, String NewID, XMLUtility myXML){
152         Node ndNewSect;
153         Element elNewSect;
154         ndNewSect = myXML.createTextElement(ndChapter.getOwnerDocument(),"sect1","");
155         elNewSect = (Element) ndNewSect;
156         elNewSect.setAttribute("id",NewID);
157         elNewSect.setAttribute("label",Label);
158         Node ndNewParagraph=myXML.createTextElement(ndChapter.getOwnerDocument(),"title",Label);
159         ndNewSect.appendChild(ndNewParagraph);
160         return ndNewSect;
161 //        ndChapter.appendChild(ndNewSect);
162     }
163     /** adds a new section to a selected chapter before the chapter with the selectedID value
164      *@param doc the current DocBook XML file
165      *@param Label the label attribute for the new section
166      *@param NewID the id of the new section
167      *@param SectionID the new section will be inserted before this section
168      *@param ChapterID used to select the correct section in the correct Chapter
169      */
170     public Node addSect1(Document doc, String Label,String NewID, String SectionID, String ChapterID){
171         XMLUtility myXML = new XMLUtility();
172         Element elNewSect;
173         Node ndParent;
174         Node ndNewSect;
175         Node ndReturn;
176         
177         xmlCurrentNode = myXML.getNode(doc, createMenuXPath(ChapterID, SectionID));
178         ndNewSect = myXML.createTextElement(doc,"sect1","");
179         elNewSect = (Element) ndNewSect;
180         elNewSect.setAttribute("id",NewID);
181         elNewSect.setAttribute("label",Label);
182         Node ndNewParagraph=myXML.createTextElement(doc,"title",Label);
183         ndNewSect.appendChild(ndNewParagraph);
184         
185         ndNewParagraph=myXML.createTextElement(doc,"para","Enter Content Here");
186         ndNewSect.appendChild(ndNewParagraph);
187         if(xmlCurrentNode!=null){
188             ndParent = xmlCurrentNode.getParentNode();
189             ndReturn = ndParent.insertBefore(ndNewSect,xmlCurrentNode);
190         }else{
191             ndParent = myXML.getNode(doc,createMenuXPath(ChapterID));
192             ndReturn = ndParent.insertBefore(ndNewSect,null);
193         }
194         return ndReturn;
195     }
196     /** Updates a Section
197      *@param doc the current DocBook XML file
198      *@param Label the label attribute for the new section
199      *@param NewID the new id of the section
200      *@param SectionID the current id of the section
201      *@param ChapterID used to select the correct section in the correct Chapter
202      */
203     public void updateSect1(Document doc, String Label,String NewID, String SectionID, String ChapterID){
204         XMLUtility myXML = new XMLUtility();
205         Element elSect;
206         Node ndNewSect;
207         Node ndTitle;
208         xmlCurrentNode = myXML.getNode(doc, createMenuXPath(ChapterID, SectionID));
209         ndTitle = xmlCurrentNode.getFirstChild();
210         ndTitle = myXML.getNode(doc,"/book/chapter[@id='"+ChapterID+"']/sect1[@id='"+SectionID+"']/title");
211         elSect = (Element) xmlCurrentNode;
212         elSect.setAttribute("id",NewID);
213         elSect.setAttribute("label",Label);
214         
215         if (ndTitle==null){
216             System.out.println("cant find "+"/book/chapter[@id='"+ChapterID+"']/sect1[@id='"+SectionID+"']");
217         }
218         ndTitle.getFirstChild().setNodeValue("2");
219     
220     }
221     
222     /** Moves a section from one place to another within the DocBook file
223      *@param doc the current DocBook XML file
224      *@param CurrentSectionID is the @id value of the section to be moved
225      *@param CurrentChapterID the @id value of the chapter of the current section to be moved
226      *@param NewSectionID is the section will be pasted before the section with this @id value and the @id value of the chapter.
227      *@param NewChapterID see above
228      */
229     public Node moveSect1(Document doc, String CurrentSectionID, String CurrentChapterID, String NewSectionID, String NewChapterID){
230         XMLUtility myXML = new XMLUtility();
231         Node ndParent;
232         Node ndRemoved = removeSect1(doc, CurrentSectionID,CurrentChapterID);
233         xmlCurrentNode = myXML.getNode(doc, createMenuXPath(NewChapterID, NewSectionID));
234         if (xmlCurrentNode != null){
235             ndParent = xmlCurrentNode.getParentNode();
236             return ndParent.insertBefore(ndRemoved,xmlCurrentNode);
237         }else{
238             ndParent = myXML.getNode(doc,createMenuXPath(NewChapterID));
239             return ndParent.insertBefore(ndRemoved,null);
240         }
241     }
242     /** Removes a section from a chapter in the DocBook file
243      *@param SectionID the current id of the section
244      *@param ChapterID used to select the correct section in the correct Chapter
245      */
246     public Node removeSect1(Document doc, String SectionID, String ChapterID){
247         XMLUtility myXML = new XMLUtility();
248         xmlCurrentNode = myXML.getNode(doc, createMenuXPath(ChapterID, SectionID));
249         Node ndParent;
250         ndParent = xmlCurrentNode.getParentNode();
251         return ndParent.removeChild(xmlCurrentNode);
252     }
253 }