View Javadoc
1   /*
2    * Copyright 2012 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.documenthandler;
17  
18  import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
19  
20  import java.util.regex.Matcher;
21  import java.util.regex.Pattern;
22  
23  /**
24   * Class WorkBibMarcContentHandler to handle content of Marc Files.
25   *
26   * @author Rajesh Chowdary K
27   * @created Mar 21, 2012
28   */
29  public class WorkBibMarcContentHandler {
30  
31      public void doPreIngestContentManipulations(RequestDocument doc, final String fileNodeUUID) {
32          String content = doc.getContent().getContent();
33          if (content != null && content != "" && content.length() > 0) {
34              Pattern pattern = Pattern.compile("tag=\"001\">.*?</controlfield");
35              Pattern pattern2 = Pattern.compile("<controlfield.*?tag=\"001\"/>");
36              Matcher matcher = pattern.matcher(content);
37              Matcher matcher2 = pattern2.matcher(content);
38              if (matcher.find()) {
39                  doc.getContent().setContent(matcher.replaceAll("tag=\"001\">" + fileNodeUUID + "</controlfield"));
40              } else if (matcher2.find()) {
41                  doc.getContent()
42                          .setContent(matcher2.replaceAll("<controlfield tag=\"001\">" + fileNodeUUID + "</controlfield>"));
43              } else {
44                  int ind = content.indexOf("</leader>") + 9;
45                  if (ind == 8) {
46                      ind = content.indexOf("<leader/>") + 9;
47                      if (ind == 8) {
48                          ind = content.indexOf("record>") + 7;
49                      }
50                  }
51                  StringBuilder sb = new StringBuilder();
52                  sb.append(content.substring(0, ind));
53                  sb.append("<controlfield tag=\"001\">");
54                  sb.append(fileNodeUUID);
55                  sb.append("</controlfield>");
56                  sb.append(content.substring(ind + 1));
57                  doc.getContent().setContent(sb.toString());
58              }
59          }
60      }
61  
62  
63      public void doPreIngestContentManipulationsForTesting(RequestDocument doc, final String fileNodeUUID) {
64          String content = doc.getContent().getContent();
65          int ind = content.indexOf("</leader>") + 9;
66          if (ind == 8) {
67              ind = content.indexOf("<leader/>") + 9;
68              if (ind == 8) {
69                  ind = content.indexOf("record>") + 7;
70              }
71          }
72          StringBuilder sb = new StringBuilder();
73          sb.append(content.substring(0, ind));
74          sb.append("<controlfield tag=\"001\">");
75          sb.append(fileNodeUUID);
76          sb.append("</controlfield>");
77          sb.append(content.substring(ind + 1));
78          doc.getContent().setContent(sb.toString());
79      }
80  }