1 package org.jahia.services.fileextraction; 2 3 import java.io.InputStream ; 4 import java.io.File ; 5 import org.apache.commons.io.CopyUtils; 6 import java.io.FileOutputStream ; 7 import org.apache.slide.util.conf.ConfigurationException; 8 import java.util.Enumeration ; 9 import org.apache.slide.util.conf.Configuration; 10 import java.util.Map ; 11 import java.util.HashMap ; 12 import org.blinkenlights.jid3.MP3File; 13 import org.blinkenlights.jid3.ID3Tag; 14 import org.blinkenlights.jid3.v1.ID3V1Tag; 15 import org.blinkenlights.jid3.v2.ID3V2Tag; 16 import org.jahia.services.sites.JahiaSitesSlideService; 17 18 30 public class MP3Extractor implements FileExtractor { 31 32 protected Map propertyMap = new HashMap (); 33 34 public MP3Extractor () { 35 } 36 37 47 public String getContentAsString (String path, long lastModified, 48 InputStream fileStream) 49 throws Exception { 50 return getContentAsString(path, lastModified, fileStream, null); 51 } 52 53 64 public String getContentAsString (String path, long lastModified, 65 InputStream fileStream, String charSet) 66 throws Exception { 67 68 StringBuffer contentBuf = new StringBuffer (); 69 70 File tempFile = File.createTempFile("mp3extrator", "temp"); 73 FileOutputStream out = new FileOutputStream (tempFile); 74 CopyUtils.copy(fileStream, out); 75 out.flush(); 76 77 this.configure(((JahiaSitesSlideService)JahiaSitesSlideService.getInstance()).getConfiguration().getConfiguration("mp3-property-mapping")); 78 79 MP3File mp3File = new MP3File(tempFile); 80 ID3Tag[] tags = mp3File.getTags(); 81 82 for (int i=0; i < tags.length; i++) { 83 if (tags[i] instanceof ID3V1Tag) { 84 ID3V1Tag v1Tag = (ID3V1Tag) tags[i]; 85 if (v1Tag.getTitle() != null) { 86 contentBuf.append(v1Tag.getTitle()); 87 contentBuf.append(" "); 88 } 89 if (v1Tag.getArtist() != null) { 90 contentBuf.append(v1Tag.getArtist()); 91 contentBuf.append(" "); 92 } 93 } else if (tags[i] instanceof ID3V2Tag) { 94 ID3V2Tag v2Tag = (ID3V2Tag) tags[i]; 95 if (v2Tag.getTitle() != null) { 96 contentBuf.append(v2Tag.getTitle()); 97 contentBuf.append(" "); 98 } 99 if (v2Tag.getArtist() != null) { 100 contentBuf.append(v2Tag.getArtist()); 101 contentBuf.append(" "); 102 } 103 } 104 } 105 tempFile.delete(); 106 return contentBuf.toString(); 107 } 108 109 119 public ExtractedDocument getExtractedDocument (String path, 120 long lastModified, InputStream fileStream) 121 throws Exception { 122 return getExtractedDocument(path, lastModified, fileStream, null); 123 } 124 125 136 public ExtractedDocument getExtractedDocument (String path, 137 long lastModified, InputStream fileStream, String charSet) 138 throws Exception { 139 140 ExtractedDocumentImpl extDoc = new ExtractedDocumentImpl(); 141 StringBuffer contentBuf = new StringBuffer (); 142 143 File tempFile = File.createTempFile("mp3extrator", ".mp3"); 146 FileOutputStream out = new FileOutputStream (tempFile); 147 CopyUtils.copy(fileStream, out); 148 out.flush(); 149 out.close(); 150 151 this.configure(((JahiaSitesSlideService)JahiaSitesSlideService.getInstance()).getConfiguration().getConfiguration("mp3-property-mapping")); 152 153 MP3File mp3File = new MP3File(tempFile); 154 ID3Tag[] tags = mp3File.getTags(); 155 156 for (int i=0; i < tags.length; i++) { 157 if (tags[i] instanceof ID3V1Tag) { 158 ID3V1Tag v1Tag = (ID3V1Tag) tags[i]; 159 if (v1Tag.getTitle() != null) { 160 extDoc.setProperty( (String ) propertyMap.get("Title"), 161 v1Tag.getTitle()); 162 contentBuf.append(v1Tag.getTitle()); 163 contentBuf.append(" "); 164 } 165 if (v1Tag.getArtist() != null) { 166 extDoc.setProperty( (String ) propertyMap.get("Artist"), 167 v1Tag.getArtist()); 168 contentBuf.append(v1Tag.getArtist()); 169 contentBuf.append(" "); 170 } 171 } else if (tags[i] instanceof ID3V2Tag) { 172 ID3V2Tag v2Tag = (ID3V2Tag) tags[i]; 173 if (v2Tag.getTitle() != null) { 174 extDoc.setProperty( (String ) propertyMap.get("Title"), 175 v2Tag.getTitle()); 176 contentBuf.append(v2Tag.getTitle()); 177 contentBuf.append(" "); 178 } 179 if (v2Tag.getArtist() != null) { 180 extDoc.setProperty( (String ) propertyMap.get("Artist"), 181 v2Tag.getArtist()); 182 contentBuf.append(v2Tag.getArtist()); 183 contentBuf.append(" "); 184 } 185 } 186 } 187 188 tempFile.delete(); 189 extDoc.setContent(contentBuf.toString()); 190 return extDoc; 191 } 192 193 public void configure(Configuration configuration) throws ConfigurationException { 194 Enumeration instructions = configuration.getConfigurations("instruction"); 195 while (instructions.hasMoreElements()) { 196 Configuration extract = (Configuration)instructions.nextElement(); 197 String property = extract.getAttribute("property"); 198 String id = extract.getAttribute("id"); 199 propertyMap.put(id, property); 200 } 201 } 202 203 } 204 | Popular Tags |