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              }
41              else if (matcher2.find()) {
42                  doc.getContent()
43                     .setContent(matcher2.replaceAll("<controlfield tag=\"001\">" + fileNodeUUID + "</controlfield>"));
44              }
45              else {
46                  int ind = content.indexOf("</leader>") + 9;
47                  if (ind == 8) {
48                      ind = content.indexOf("<leader/>") + 9;
49                      if (ind == 8) {
50                          ind = content.indexOf("record>") + 7;
51                      }
52                  }
53                  StringBuilder sb = new StringBuilder();
54                  sb.append(content.substring(0, ind));
55                  sb.append("<controlfield tag=\"001\">");
56                  sb.append(fileNodeUUID);
57                  sb.append("</controlfield>");
58                  sb.append(content.substring(ind + 1));
59                  doc.getContent().setContent(sb.toString());
60              }
61          }
62      }
63  }