use of org.opencastproject.caption.api.CaptionConverterException in project opencast by opencast.
the class CaptionServiceImpl method importCaptions.
/**
* Imports captions using registered converter engine and specified language.
*
* @param input
* file containing captions
* @param inputFormat
* format of imported captions
* @param language
* (optional) captions' language
* @return {@link List} of parsed captions
* @throws UnsupportedCaptionFormatException
* if there is no registered engine for given format
* @throws IllegalCaptionFormatException
* if parser encounters exception
*/
private List<Caption> importCaptions(File input, String inputFormat, String language) throws UnsupportedCaptionFormatException, CaptionConverterException {
// get input format
CaptionConverter converter = getCaptionConverter(inputFormat);
if (converter == null) {
logger.error("No available caption format found for {}.", inputFormat);
throw new UnsupportedCaptionFormatException(inputFormat);
}
FileInputStream fileStream = null;
try {
fileStream = new FileInputStream(input);
List<Caption> collection = converter.importCaption(fileStream, language);
return collection;
} catch (FileNotFoundException e) {
throw new CaptionConverterException("Could not locate file " + input);
} finally {
IOUtils.closeQuietly(fileStream);
}
}
use of org.opencastproject.caption.api.CaptionConverterException in project opencast by opencast.
the class DFXPCaptionConverter method importCaption.
/**
* {@inheritDoc} Parser used for parsing XML document is DOM parser. Language parameter will determine which language
* is searched for and parsed. If there is no matching language, empty collection is returned. If language parameter
* is <code>null</code> first language found is parsed.
*
* @see org.opencastproject.caption.api.CaptionConverter#importCaption(java.io.InputStream, java.lang.String)
*/
@Override
public List<Caption> importCaption(InputStream in, String language) throws CaptionConverterException {
// create new collection
List<Caption> collection = new ArrayList<Caption>();
Document doc;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.parse(in);
doc.getDocumentElement().normalize();
} catch (ParserConfigurationException e) {
throw new CaptionConverterException("Could not parse captions", e);
} catch (SAXException e) {
throw new CaptionConverterException("Could not parse captions", e);
} catch (IOException e) {
throw new CaptionConverterException("Could not parse captions", e);
}
// get all <div> elements since they contain information about language
NodeList divElements = doc.getElementsByTagName("div");
Element targetDiv = null;
if (language != null) {
// find first <div> element with matching language
for (int i = 0; i < divElements.getLength(); i++) {
Element n = (Element) divElements.item(i);
if (n.getAttribute("xml:lang").equals(language)) {
targetDiv = n;
break;
}
}
} else {
if (divElements.getLength() > 1) {
// more than one existing <div> element, no language specified
logger.warn("More than one <div> element available. Parsing first one...");
}
if (divElements.getLength() != 0) {
targetDiv = (Element) divElements.item(0);
}
}
// check if we found node
if (targetDiv == null) {
logger.warn("No suitable <div> element found for language {}", language);
} else {
NodeList pElements = targetDiv.getElementsByTagName("p");
// initialize start time
Time time = null;
try {
time = new TimeImpl(0, 0, 0, 0);
} catch (IllegalTimeFormatException e1) {
}
for (int i = 0; i < pElements.getLength(); i++) {
try {
Caption caption = parsePElement((Element) pElements.item(i));
// check time
if (caption.getStartTime().compareTo(time) < 0 || caption.getStopTime().compareTo(caption.getStartTime()) <= 0) {
logger.warn("Caption with invalid time encountered. Skipping...");
continue;
}
collection.add(caption);
} catch (IllegalTimeFormatException e) {
logger.warn("Caption with invalid time format encountered. Skipping...");
}
}
}
// return collection
return collection;
}
use of org.opencastproject.caption.api.CaptionConverterException in project opencast by opencast.
the class DFXPCaptionConverter method getLanguageList.
/**
* {@inheritDoc} Uses SAX parser to quickly read the document and retrieve available languages.
*
* @see org.opencastproject.caption.api.CaptionConverter#getLanguageList(java.io.InputStream)
*/
@Override
public String[] getLanguageList(InputStream input) throws CaptionConverterException {
// create lang list
final List<String> langList = new LinkedList<String>();
// get SAX parser
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
SAXParser parser = factory.newSAXParser();
// create handler
DefaultHandler handler = new DefaultHandler() {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if ("div".equals(qName)) {
// we found div tag - let's make a lookup for language
String lang = attributes.getValue("xml:lang");
if (lang == null) {
// should never happen
logger.warn("Missing xml:lang attribute for div element.");
} else if (langList.contains(lang)) {
logger.warn("Multiple div elements with same language.");
} else {
langList.add(lang);
}
}
}
};
// parse stream
parser.parse(input, handler);
} catch (ParserConfigurationException e) {
// should not happen
throw new RuntimeException(e);
} catch (SAXException e) {
throw new CaptionConverterException("Could not parse captions", e);
} catch (IOException e) {
throw new RuntimeException(e);
}
return langList.toArray(new String[0]);
}
use of org.opencastproject.caption.api.CaptionConverterException in project opencast by opencast.
the class CaptionServiceRemoteImpl method getLanguageList.
/**
* @see org.opencastproject.caption.api.CaptionService#getLanguageList(MediaPackageElement, String)
*/
@Override
public String[] getLanguageList(MediaPackageElement input, String format) throws UnsupportedCaptionFormatException, CaptionConverterException {
HttpPost post = new HttpPost("/languages");
try {
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("captions", MediaPackageElementParser.getAsXml(input)));
params.add(new BasicNameValuePair("input", format));
post.setEntity(new UrlEncodedFormEntity(params));
} catch (Exception e) {
throw new CaptionConverterException(e);
}
HttpResponse response = null;
try {
response = getResponse(post);
if (response != null) {
List<String> langauges = new ArrayList<String>();
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(EntityUtils.toString(response.getEntity(), "UTF-8"));
NodeList languages = doc.getElementsByTagName("languages");
for (int i = 0; i < languages.getLength(); i++) {
Node item = languages.item(i);
langauges.add(item.getTextContent());
}
logger.info("Catalog languages received from remote caption service");
return langauges.toArray(new String[langauges.size()]);
}
} catch (Exception e) {
throw new CaptionConverterException("Unable to get catalog languages " + input + " using a remote caption service", e);
} finally {
closeConnection(response);
}
throw new CaptionConverterException("Unable to get catalog languages" + input + " using a remote caption service");
}
Aggregations