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

Quick Search    Search Deep

Source code: com/sonoma/SiteContent.java


1   package com.sonoma;
2   import java.io.FileInputStream;
3   import java.io.ByteArrayInputStream;
4   
5   
6   import java.io.FileInputStream;
7   import java.util.Vector;
8   
9   import java.io.FileNotFoundException;
10  import org.w3c.dom.Document;
11  import org.w3c.dom.Node;
12  import org.w3c.dom.Element;
13  import org.w3c.dom.NamedNodeMap;
14  import org.w3c.dom.NodeList;
15  import org.xml.sax.SAXException;
16  import org.xml.sax.InputSource;
17  
18  /**
19  * Creates DocBook Paragraph of nodes (item nodes) and attributes.
20  * Also used to create new postings (DocBook Articles)
21  * Other classes and JSP pages use this object instead of Xerces or XML.
22  * SiteContent.java
23  * @author Roy Hoobler
24  * @version 1.1 04/7/2002
25  */
26  public class SiteContent{
27      private Node xmlNode;
28      
29      /** Deprecated Not supported*/
30      private boolean saveDoc() {
31          return false;
32      }
33      /** Creates XPath Query based on request parameters, usually from JSP pages.
34       *@param iPath is the current DocBook or DocBook Article paragraph node
35       *@param sSection the label of the section of the current paragraph.  If null or empty string an Article document is used (/article)
36       *@param isText if true, the /text() element is added to the end of the XPath, otherwise, the paragraph is the final node
37       */
38      public String createXPath(int iPara, String sSection, boolean isText){
39          String sXPath = new String("");
40          if(sSection == null || sSection == ""){
41              sXPath = "/article/*[" + iPara + "]";
42          }else{
43              sXPath = "/book/chapter/sect1[@id='" + sSection + "']/*["+iPara+"]";
44          }
45          if (isText){
46              sXPath = sXPath + "/text()";
47          }
48          System.out.println(sXPath);
49          return sXPath;
50      }
51      /** Adds a Title element to the chapter or article.  Maybe this should be citetitle?
52       *@param doc is the current DocBook or DocBook Article
53       *@param sSection the label of the section to be added to
54       *@param nd iPara the paragraph before which the new one will be added
55       *@param title The string value of the title to add
56       */
57      public final Node addTitle(Document doc, String sSection, int iPara, String title){
58          XMLUtility myXML = new XMLUtility();
59          Node ndNew;
60          Node ndNewText;
61          Node ndReturn;
62          Node ndParent;
63          xmlNode = myXML.getNode(doc, createXPath(iPara, sSection,false));
64          //if xmlNode is null then the parent of the first node, else get xmlNodeParent????
65          //this will add the new paragraph to the end
66          if(xmlNode == null){
67              ndParent = myXML.getNode(doc,createXPath(1,sSection,false)).getParentNode();
68           }else{
69              ndParent = xmlNode.getParentNode();
70          }
71          ndNew = doc.createElement("title");
72          ndNewText = doc.createTextNode(title);
73          ndNew.appendChild(ndNewText);
74          ndReturn = ndParent.insertBefore(ndNew, xmlNode);
75          return ndReturn;
76      }
77      /** Deletes paragraphs title, anything at the section level 
78       *@param iPath is the current DocBook or DocBook Article paragraph node
79       *@param sSection the label of the section of the current paragraph.  If null or empty string an Article document is used (/article)
80       *@param iPara the paragraph to be deleted
81       */
82    public final boolean deleteParagraph(Document doc, String sSection, int iPara){
83          Node ndDeleted;
84          Node nd;
85          XMLUtility myXML = new XMLUtility();
86          xmlNode = myXML.getNode(doc, createXPath(iPara, sSection, false));
87          if (xmlNode!=null){
88              //remove this node
89              ndDeleted = xmlNode;
90              nd = ndDeleted.getParentNode();
91              nd.removeChild(ndDeleted);
92          }
93      xmlNode = null;
94      myXML = null;
95      return true;
96    }
97        /** Copies a paragraph node to a new position then deletes it 
98       *@param iPath is the current DocBook or DocBook Article paragraph node
99       *@param sSection the label of the section of the current paragraph.  If null or empty string an Article document is used (/article)
100      *@param iPara the paragraph to be moved
101        *@param iNewPosition the position in the current document (before the paragraph is deleted) where the paragraph should be moved
102        */
103   public final boolean moveParagraph(Document doc, String sSection, int iPara, int iNewPosition){
104         Node ndDeleted;
105         Node nd;
106         int iFirst = iPara<iNewPosition?iPara:iNewPosition;
107         int iLast = iPara>iNewPosition?iPara:iNewPosition;
108         
109         //remove the greatest node first
110         XMLUtility myXML = new XMLUtility();
111         xmlNode = myXML.getNode(doc, createXPath(iFirst, sSection, false));
112         if (xmlNode!=null){
113             ndDeleted = xmlNode;
114             Node ndBefore = myXML.getNode(doc, createXPath(iLast, sSection, false));
115             nd = ndDeleted.getParentNode();
116             //add the removed node to it's new position
117             nd.insertBefore(ndBefore, xmlNode);
118             ndDeleted = nd.removeChild(ndDeleted);
119             //add the first node to the end
120             ndBefore = myXML.getNode(doc, createXPath(iLast, sSection, false));
121             nd.insertBefore(ndDeleted,ndBefore);
122             
123         }
124     xmlNode = null;
125     myXML = null;
126     return true;
127   }
128     /** Updates the Node of a paragraph for DocBooks and DocBook Articles.  Actually deleting the old paragraph, processing and adding a new paragraph in its place
129      *@param doc is the current DocBook or DocBook Article
130      *@param sSection the label of the section to be added to
131      *@param nd iPara the paragraph before which the new one will be added
132      *@param oPara is a paragraph object representing a DocBook Paragraph
133      */
134     public Node updateParagraph(Document doc, String sSection, int iPara, com.sonoma.objects.spParagraph para){
135         Node ndDeleted;
136         Node nd;
137         XMLUtility myXML = new XMLUtility();
138         xmlNode = myXML.getNode(doc, createXPath(iPara, sSection, false));
139         if (xmlNode!=null){
140             //remove this node
141             ndDeleted = xmlNode;
142             nd = ndDeleted.getParentNode();
143             nd.removeChild(ndDeleted);
144         }
145         //now loop through, adding it back.
146         xmlNode = addParagraph(doc, sSection,iPara,para);
147         return xmlNode;
148     }
149     /** Adds a new paragraph Before the specified paragraph number 
150      *@param doc is the current DocBook or DocBook Article
151      *@param sSection the label of the section to be added to
152      *@param nd iPara the paragraph before which the new one will be added
153      *@param oPara is a paragraph object representing a DocBook Paragraph
154      */
155     public Node addParagraph(Document doc, String sSection, int iPara, com.sonoma.objects.spParagraph para){
156         XMLUtility myXML = new XMLUtility();
157         Node ndNew;
158         Node ndReturn;
159         Node ndParent;
160         xmlNode = myXML.getNode(doc, createXPath(iPara, sSection,false));
161         //if xmlNode is null then the parent of the first node, else get xmlNodeParent????
162         //this will add the new paragraph to the end
163         if(xmlNode == null){
164             ndParent = myXML.getNode(doc,createXPath(1,sSection,false)).getParentNode();
165          }else{
166             ndParent = xmlNode.getParentNode();
167         }
168         ndNew = doc.createElement("para");
169         ndNew = processParagraph(doc,myXML,ndNew,para);
170         ndReturn = ndParent.insertBefore(ndNew, xmlNode);
171         return ndReturn;
172     }
173     /** Not yet implemented or used */
174     public boolean addAuthorInfo(){
175         return false;
176     }
177     /** Creates a new article (posting) xml file.  A line break starts a new paragrph 
178      *@param doc is the current DocBook or DocBook Article
179      *@param art is the Sonoma object representing a collection of paragraphs used to create the new article
180      */
181     public boolean addLongArticle(Document doc,com.sonoma.objects.dbNewArticle art){
182         Node xmlNode;
183         XMLUtility myXML = new XMLUtility();
184         Vector articleParagraphs = art.getSpParagraphVector();
185         for(int i=0;i<articleParagraphs.size();i++){
186             com.sonoma.objects.spParagraph tmp = new com.sonoma.objects.spParagraph();
187             tmp = (com.sonoma.objects.spParagraph) articleParagraphs.elementAt(i);
188             System.out.println(i+" :***: " + tmp.getContent()[0]);
189             addParagraph(doc, "", i+1, tmp);
190             //for now, just change the title!
191             if(i==0){
192                 xmlNode = myXML.getNode(doc,"/article/title");
193                 xmlNode = xmlNode.getFirstChild();
194                 xmlNode.setNodeValue(tmp.getTitle());
195             }
196         }
197         xmlNode = null;
198         myXML = null;
199         return false;
200     }
201     /** Creates a new paragraph element and sub elements (only inlinemediaobject and ulinks are currently supported 
202      *@param doc is the current DocBook or DocBook Article
203      *@param myXML is the xml processing classes
204      *@param nd is the node to be processes
205      *@param oPara is a paragraph object representing a DocBook Paragraph
206      */
207     private Node processParagraph(Document doc, XMLUtility myXML, Node nd, com.sonoma.objects.spParagraph oPara){
208         Node ndNewSub;
209         Element elSub;
210         Node ndNewText;
211         String[] arContent = oPara.getContent();
212         String[] arLinks = oPara.getURL();
213         String[] arLinksTitle = oPara.getURLTitle();
214         String sTitle = oPara.getTitle();
215         String[] arPics = oPara.getPictureFile();
216         //title
217         if(sTitle != null){
218             ndNewSub = myXML.createTextElement(doc,"citetitle",sTitle);
219             nd.appendChild(ndNewSub);
220         }
221         for(int i=0;i<arContent.length ;i++){
222             if(arPics != null && i<arPics.length && arPics[i] != null && arPics[i].trim().length() > 1){
223                 try{
224                     ndNewSub = nd.appendChild(doc.createElement("inlinemediaobject"));
225                     ndNewSub = ndNewSub.appendChild(doc.createElement("imageobject"));
226                     elSub = (Element) ndNewSub.appendChild(doc.createElement("imagedata"));
227                     elSub.setAttribute("fileref",arPics[i]);
228                 }catch(Exception e){
229                     e.printStackTrace();
230                 }
231             }
232             try{
233             ndNewText = doc.createTextNode(arContent[i] + "");
234             nd.appendChild(ndNewText);
235             System.out.println("processing " + i + ": " + ndNewText.getNodeValue());
236             if(arLinks != null && i<arLinks.length){
237                 elSub = (Element) nd.appendChild(myXML.createTextElement(doc,"ulink",arLinksTitle[i]));
238                 elSub.setAttribute("url",arLinks[i]);
239                 
240             }   
241             }catch(Exception e){
242                 e.printStackTrace();
243             }
244         }
245         return nd;
246     }
247     /** Deprecated.  Not used or supported */
248     public boolean deleteParagraph(Document doc, int iPara, String section){
249         return true;
250     }
251     
252     
253     
254 }