KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > snapper > business > ConverterImpl


1 package org.enhydra.snapper.business;
2
3 /**
4  * @author Milin Radivoj
5  *
6  */

7
8 import org.enhydra.snapper.Snapper;
9 import org.enhydra.snapper.business.xml.*;
10 import java.io.*;
11
12 public class ConverterImpl{
13
14         private Search searchOld;
15         
16         public void setXml(String JavaDoc xml){
17             StringReader is = new StringReader(xml);
18             searchOld = new SearchImpl();
19             read(is);
20         }
21         
22         public boolean read(Reader is){
23             try {
24                 if (is!=null)
25                     searchOld = SearchImpl.unmarshal(is);
26                 return true;
27             } catch (IOException ex){
28                 ex.printStackTrace();
29                 System.out.println("Problem with reading InputStream have occured!");
30                 return false;
31             }
32         }
33         
34         public Search transform()
35          {
36 // System.out.println("Krece Zeus transformacija");
37

38             Search result = SearchImpl.newInstance();
39             SearchResult searchResultNew = SearchResultImpl.newInstance();
40             try{
41             
42             SearchSummary searchSummaryold = searchOld.getSearchSummary();
43             result.setSearchSummary(searchSummaryold);
44             SearchResult searcResult = searchOld.getSearchResult();
45             java.util.List JavaDoc lista = searcResult.getResultList();
46             for(int i=0;i<lista.size();i++)
47              {
48                Result newResult = ResultImpl.newInstance();
49                Result current = (Result)lista.get(i);
50              
51             
52                 FileType fileTypeOld = current.getFileType();
53                 Title titleOld = current.getTitle();
54                 AbsolutFilePath fullPathOld = current.getAbsolutFilePath();
55                 AbsolutFileName absolutFileNameOld = current.getAbsolutFileName();
56                 SiteName siteOld = current.getSiteName();
57                 Score scoreOld = current.getScore();
58                 Modified modifiedOld =current.getModified();
59                 Content contentOld =current.getContent();
60                 Properties propertiesOld =current.getProperties();
61                 MetaData metaDataOld =current.getMetaData();
62                 
63                 
64                 String JavaDoc absolutfileNameOld = absolutFileNameOld.getValue(); //extenzija
65
String JavaDoc full = fullPathOld.getValue(); //sve
66

67                 
68                 Mapper mapper = new Mapper();
69                 
70                 newResult.setSiteName(siteOld);
71                 
72                 Title titleNew = TitleImpl.newInstance();
73                 titleNew.setValue(titleOld.getValue());
74                 newResult.setTitle(titleNew);
75                 
76                 newResult.setFileType(fileTypeOld);
77             
78                 FileNameImpl fileNameImplNew = FileNameImpl.newInstance();
79                 
80                 int indexOF = absolutfileNameOld.lastIndexOf(".");
81                 if(indexOF>0)
82                 fileNameImplNew.setValue(absolutfileNameOld.substring(0,absolutfileNameOld.lastIndexOf(".")));
83                     else
84                     fileNameImplNew.setValue(absolutfileNameOld);
85                 
86                     newResult.setFileName(fileNameImplNew);
87             
88                 AbsolutFileName absolutFileNameNew = AbsolutFileNameImpl.newInstance();
89                 String JavaDoc temp1 = fullPathOld.getValue();
90                 absolutFileNameNew.setValue(absolutfileNameOld);
91                 newResult.setAbsolutFileName(absolutFileNameNew);
92                 
93                 AbsolutDirPath absolutDirPathNew = AbsolutDirPathImpl.newInstance();
94                 absolutDirPathNew.setValue(mapper.getAbsulutDirPath(full,siteOld.getValue()));
95                 newResult.setAbsolutDirPath(absolutDirPathNew);
96         
97                 AbsolutFilePath absolutFilePathNew =AbsolutFilePathImpl.newInstance();
98                 String JavaDoc temp = fullPathOld.getValue();
99                 
100                 
101                  if(Snapper.getFileSeparatorConvention()!=null &&
102                         Snapper.getFileSeparatorConvention().equalsIgnoreCase("unix")) {
103                     temp = temp.replaceAll("\\\\","/");
104                       }
105                 
106                 absolutFilePathNew.setValue(temp);
107                 
108                 
109                 newResult.setAbsolutFilePath(absolutFilePathNew);
110
111                 RelativeDirPath relativeDirPathNew =RelativeDirPathImpl.newInstance();
112                 relativeDirPathNew.setValue(mapper.getRelativeDirPath(full,absolutfileNameOld,siteOld.getValue()));
113                 newResult.setRelativeDirPath(relativeDirPathNew);
114         
115                 RelativeFilePath relativeFilePath = RelativeFilePathImpl.newInstance();
116                 relativeFilePath.setValue(mapper.getRelativeFilePath(full,absolutfileNameOld,siteOld.getValue()));
117                 newResult.setRelativeFilePath(relativeFilePath);
118             
119                 MappedImpl mappedImplNew = MappedImpl.newInstance();
120                 mappedImplNew.setValue(mapper.mapFullPathToLogical(full,siteOld.getValue(),null));
121                 newResult.setMapped(mappedImplNew);
122             
123                 GrantedDownload grantedDownload = GrantedDownloadImpl.newInstance();
124                 grantedDownload.setValue(mapper.getGrantedDownload(siteOld.getValue()));
125                 newResult.setGrantedDownload(grantedDownload);
126                 newResult.setScore(scoreOld);
127         
128                 newResult.setModified(modifiedOld);
129                 
130                 Content contentNew = ContentImpl.newInstance();
131                 contentNew.setValue(contentOld.getValue());
132                 newResult.setContent(contentNew);
133      
134             if(propertiesOld!=null)
135             {
136                 Properties propertiesNew = PropertiesImpl.newInstance();
137                 propertiesNew.setValue(propertiesOld.getValue());
138                 newResult.setProperties(propertiesNew);
139             }
140             
141             if(metaDataOld!=null)
142             {
143             
144                 MetaData metaDataNew = MetaDataImpl.newInstance();
145                 metaDataNew.setValue(metaDataOld.getValue());
146                 newResult.setMetaData(metaDataNew);
147             }
148                 
149                 searchResultNew.addResult(newResult);
150                 
151              }
152             result.setSearchResult(searchResultNew);
153                 return result;
154             } catch (Exception JavaDoc ex){
155                 ex.printStackTrace();
156                 return null;
157             }
158             
159          }
160          public static String JavaDoc replaceAll(
161                 String JavaDoc input,
162                 String JavaDoc forReplace,
163                 String JavaDoc replaceWith) {
164                 if( input == null )
165                   return null;
166                 StringBuffer JavaDoc result = new StringBuffer JavaDoc();
167                 boolean hasMore = true;
168                 while (hasMore) {
169                   int start = input.indexOf(forReplace);
170                   int end = start + forReplace.length();
171                   if (start != -1) {
172                     result.append(input.substring(0, start) + replaceWith);
173                     input = input.substring(end);
174                   }
175                   else {
176                     hasMore = false;
177                     result.append(input);
178                   }
179                 }
180                 if (result.toString().equals(""))
181                   return input; //nothing is changed
182
else
183                   return result.toString();
184               }
185
186     
187     
188 }
189
Popular Tags