View Javadoc
1   /*
2    * Copyright 2011 The Kuali Foundation.
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.select.service.impl;
17  
18  import org.kuali.ole.select.businessobject.BibInfoBean;
19  import org.w3c.dom.Document;
20  import org.w3c.dom.Element;
21  import org.w3c.dom.Node;
22  import org.w3c.dom.NodeList;
23  
24  import javax.xml.parsers.DocumentBuilder;
25  import javax.xml.parsers.DocumentBuilderFactory;
26  import java.io.File;
27  
28  public class BibXMLToBibInfoBean {
29  
30      private DocumentBuilderFactory docFactory;
31      private DocumentBuilder docBuilder;
32      private Document doc;
33      private Element rootElement;
34      private BibInfoBean bibInfoBean;
35      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BibInfoBeanToBibXML.class);
36  
37      public BibInfoBean getBibInfoBean(String path) throws Exception {
38          initiateDocument(path);
39          setBeanValuess();
40          return bibInfoBean;
41      }
42  
43      private void initiateDocument(String path) throws Exception {
44          //File file = new File("\\olemarcfiles\\b2c3a5d9-0049-435a-9ac8-dbda01f2eb58.xml");
45          File file = new File(path);
46          docFactory = DocumentBuilderFactory.newInstance();
47          docBuilder = docFactory.newDocumentBuilder();
48          doc = docBuilder.parse(file);
49          doc.getDocumentElement().normalize();
50      }
51  
52      private void setBeanValuess() throws Exception {
53          bibInfoBean = new BibInfoBean();
54          NodeList nodeLst = doc.getElementsByTagName("bibData");
55          for (int s = 0; s < nodeLst.getLength(); s++) {
56              Node fstNode = nodeLst.item(s);
57              if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
58                  Element fstElmnt = (Element) fstNode;
59  
60                  NodeList titleIdLst = fstElmnt.getElementsByTagName("titleId");
61                  Element titleIdElmnt = (Element) titleIdLst.item(0);
62                  NodeList titleId = titleIdElmnt.getChildNodes();
63                  if (titleId.item(0) != null) {
64                      bibInfoBean.setTitleId(((Node) titleId.item(0)).getNodeValue());
65                  }
66  
67                  NodeList titleLst = fstElmnt.getElementsByTagName("title");
68                  Element titleElmnt = (Element) titleLst.item(0);
69                  NodeList title = titleElmnt.getChildNodes();
70                  if (title.item(0) != null) {
71                      bibInfoBean.setTitle(((Node) title.item(0)).getNodeValue());
72                  }
73  
74                  NodeList authorLst = fstElmnt.getElementsByTagName("author");
75                  Element authorElmnt = (Element) authorLst.item(0);
76                  NodeList author = authorElmnt.getChildNodes();
77                  if (author.item(0) != null) {
78                      bibInfoBean.setAuthor(((Node) author.item(0)).getNodeValue());
79                  }
80  
81                  NodeList editionLst = fstElmnt.getElementsByTagName("edition");
82                  Element editionElmnt = (Element) editionLst.item(0);
83                  NodeList edition = editionElmnt.getChildNodes();
84                  if (edition.item(0) != null) {
85                      bibInfoBean.setEdition(((Node) edition.item(0)).getNodeValue());
86                  }
87  
88                  NodeList standardNumberLst = fstElmnt.getElementsByTagName("standardNumber");
89                  Element standardNumberElmnt = (Element) standardNumberLst.item(0);
90                  NodeList standardNumber = standardNumberElmnt.getChildNodes();
91                  if (standardNumber.item(0) != null) {
92                      bibInfoBean.setStandardNumber(((Node) standardNumber.item(0)).getNodeValue());
93                  }
94  
95                  NodeList publisherLst = fstElmnt.getElementsByTagName("publisher");
96                  Element publisherElmnt = (Element) publisherLst.item(0);
97                  NodeList publisher = publisherElmnt.getChildNodes();
98                  if (publisher.item(0) != null) {
99                      bibInfoBean.setPublisher(((Node) publisher.item(0)).getNodeValue());
100                 }
101 
102                 NodeList placeOfPublicationLst = fstElmnt.getElementsByTagName("placeOfPublication");
103                 Element placeOfPublicationElmnt = (Element) placeOfPublicationLst.item(0);
104                 NodeList placeOfPublication = placeOfPublicationElmnt.getChildNodes();
105                 if (placeOfPublication.item(0) != null) {
106                     bibInfoBean.setPlaceOfPublication(((Node) placeOfPublication.item(0)).getNodeValue());
107                 }
108 
109                 NodeList yearOfPublicationLst = fstElmnt.getElementsByTagName("yearOfPublication");
110                 Element yearOfPublicationElmnt = (Element) yearOfPublicationLst.item(0);
111                 NodeList yearOfPublication = yearOfPublicationElmnt.getChildNodes();
112                 if (yearOfPublication.item(0) != null) {
113                     bibInfoBean.setYearOfPublication(((Node) yearOfPublication.item(0)).getNodeValue());
114                 }
115 
116 
117                 NodeList physicalDescriptionLst = fstElmnt.getElementsByTagName("physicalDescription");
118                 Element physicalDescriptionElmnt = (Element) physicalDescriptionLst.item(0);
119                 NodeList physicalDescription = physicalDescriptionElmnt.getChildNodes();
120                 if (physicalDescription.item(0) != null) {
121                     bibInfoBean.setYearOfPublication(((Node) physicalDescription.item(0)).getNodeValue());
122                 }
123 
124                 NodeList formatLst = fstElmnt.getElementsByTagName("format");
125                 Element formatElmnt = (Element) formatLst.item(0);
126                 NodeList format = formatElmnt.getChildNodes();
127                 if (format.item(0) != null) {
128                     bibInfoBean.setFormat(((Node) format.item(0)).getNodeValue());
129                 }
130 
131                 NodeList seriesLst = fstElmnt.getElementsByTagName("series");
132                 Element seriesElmnt = (Element) seriesLst.item(0);
133                 NodeList series = seriesElmnt.getChildNodes();
134                 if (series.item(0) != null) {
135                     bibInfoBean.setSeries(((Node) series.item(0)).getNodeValue());
136                 }
137 
138                 NodeList subjectsLst = fstElmnt.getElementsByTagName("subjects");
139                 Element subjectsElmnt = (Element) subjectsLst.item(0);
140                 NodeList subjects = subjectsElmnt.getChildNodes();
141                 if (subjects.item(0) != null) {
142                     bibInfoBean.setSubjects(((Node) subjects.item(0)).getNodeValue());
143                 }
144 
145                 NodeList priceLst = fstElmnt.getElementsByTagName("price");
146                 Element priceElmnt = (Element) priceLst.item(0);
147                 NodeList price = priceElmnt.getChildNodes();
148                 if (price.item(0) != null) {
149                     bibInfoBean.setPrice(((Node) price.item(0)).getNodeValue());
150                 }
151 
152                 NodeList requestorContactLst = fstElmnt.getElementsByTagName("requestorContact");
153                 Element requestorContactElmnt = (Element) requestorContactLst.item(0);
154                 NodeList requestorContact = requestorContactElmnt.getChildNodes();
155                 if (requestorContact.item(0) != null) {
156                     bibInfoBean.setRequestorContact(((Node) requestorContact.item(0)).getNodeValue());
157                 }
158 
159                 NodeList requestersNotesLst = fstElmnt.getElementsByTagName("requestersNotes");
160                 Element requestersNotesElmnt = (Element) requestersNotesLst.item(0);
161                 NodeList requestersNotes = requestersNotesElmnt.getChildNodes();
162                 if (requestersNotes.item(0) != null) {
163                     bibInfoBean.setRequestersNotes(((Node) requestersNotes.item(0)).getNodeValue());
164                 }
165 
166                 NodeList noOfCopiesLst = fstElmnt.getElementsByTagName("noOfCopies");
167                 Element noOfCopiesElmnt = (Element) noOfCopiesLst.item(0);
168                 NodeList noOfCopies = noOfCopiesElmnt.getChildNodes();
169                 if (noOfCopies.item(0) != null) {
170                     bibInfoBean.setNoOfCopies(((Node) noOfCopies.item(0)).getNodeValue());
171                 }
172 
173                 NodeList categoryLst = fstElmnt.getElementsByTagName("category");
174                 Element categoryElmnt = (Element) categoryLst.item(0);
175                 NodeList category = categoryElmnt.getChildNodes();
176                 if (category.item(0) != null) {
177                     bibInfoBean.setCategory(((Node) category.item(0)).getNodeValue());
178                 }
179 
180                 NodeList requestSourceLst = fstElmnt.getElementsByTagName("requestSource");
181                 Element requestSourceElmnt = (Element) requestSourceLst.item(0);
182                 NodeList requestSource = requestSourceElmnt.getChildNodes();
183                 if (requestSource.item(0) != null) {
184                     bibInfoBean.setRequestSource(((Node) requestSource.item(0)).getNodeValue());
185                 }
186 
187                 NodeList selectorLst = fstElmnt.getElementsByTagName("selector");
188                 Element selectorElmnt = (Element) selectorLst.item(0);
189                 NodeList selector = selectorElmnt.getChildNodes();
190                 if (selector.item(0) != null) {
191                     bibInfoBean.setSelector(((Node) selector.item(0)).getNodeValue());
192                 }
193 
194                 NodeList selectorNotesLst = fstElmnt.getElementsByTagName("selectorNotes");
195                 Element selectorNotesElmnt = (Element) selectorNotesLst.item(0);
196                 NodeList selectorNotes = selectorNotesElmnt.getChildNodes();
197                 if (selectorNotes.item(0) != null) {
198                     bibInfoBean.setSelectorNotes(((Node) selectorNotes.item(0)).getNodeValue());
199                 }
200             
201 /*            NodeList startPageLst = fstElmnt.getElementsByTagName("startPage");
202             Element startPageElmnt = (Element) startPageLst.item(0);
203             NodeList startPage = startPageElmnt.getChildNodes();
204             System.out.println("startPage : "  + ((Node) startPage.item(0)).getNodeValue());
205             
206             NodeList endPageLst = fstElmnt.getElementsByTagName("endPage");
207             Element endPageElmnt = (Element) endPageLst.item(0);
208             NodeList endPage = endPageElmnt.getChildNodes();
209             System.out.println("endPage : " + ((Node) endPage.item(0)).getNodeValue());*/
210 
211             }
212         }
213     }
214 
215 }