List of usage examples for javax.xml.parsers SAXParser parse
public void parse(InputSource is, DefaultHandler dh) throws SAXException, IOException
From source file:org.opencastproject.caption.converters.DFXPCaptionConverter.java
/** * {@inheritDoc} Uses SAX parser to quickly read the document and retrieve available languages. * //from w w w .j a v a2 s .co m * @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]); }
From source file:org.opencastproject.dictionary.impl.parser.PopulateDictionary.java
public static void populate(String lang, File in, int minWordCount, int minWordLength) { // Create the sax parser and start parsing WikipediaSAXHandler saxHandler = new WikipediaSAXHandler(lang, minWordCount, minWordLength); SAXParserFactory saxFactory = SAXParserFactory.newInstance(); SAXParser parser; try {/* www.j ava 2 s . c o m*/ parser = saxFactory.newSAXParser(); parser.parse(in, saxHandler); } catch (Exception e) { e.printStackTrace(); } }
From source file:org.openddr.simpleapi.oddr.ODDRService.java
public void initialize(String defaultVocabularyIRI, Properties prprts) throws NameException, InitializationException { if (defaultVocabularyIRI == null || defaultVocabularyIRI.trim().length() == 0) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new NullPointerException("defaultVocabularyIRI can not be null")); }//from w w w. j a v a 2s . c o m /*Initializing VocabularyHolder*/ ODDRVocabularyService oddrVocabularyService = new ODDRVocabularyService(); oddrVocabularyService.initialize(prprts); vocabularyHolder = oddrVocabularyService.getVocabularyHolder(); vocabularyHolder.existVocabulary(defaultVocabularyIRI); String oddrUaDeviceBuilderPath = prprts.getProperty(ODDR_UA_DEVICE_BUILDER_PATH_PROP); String oddrUaDeviceDatasourcePath = prprts.getProperty(ODDR_UA_DEVICE_DATASOURCE_PATH_PROP); String oddrUaDeviceBuilderPatchPaths = prprts.getProperty(ODDR_UA_DEVICE_BUILDER_PATCH_PATHS_PROP); String oddrUaDeviceDatasourcePatchPaths = prprts.getProperty(ODDR_UA_DEVICE_DATASOURCE_PATCH_PATHS_PROP); String oddrUaBrowserDatasourcePaths = prprts.getProperty(ODDR_UA_BROWSER_DATASOURCE_PATH_PROP); String oddrUaOperatingSystemDatasourcePaths = prprts .getProperty(ODDR_UA_OPERATINGSYSTEM_DATASOURCE_PATH_PROP); InputStream oddrUaDeviceBuilderStream = null; InputStream oddrUaDeviceDatasourceStream = null; InputStream[] oddrUaDeviceBuilderPatchStreams = null; InputStream[] oddrUaDeviceDatasourcePatchStreams = null; InputStream oddrUaBrowserDatasourceStream = null; InputStream oddrUaOperatingSystemDatasourceStream = null; try { oddrUaDeviceBuilderStream = (InputStream) prprts.get(ODDR_UA_DEVICE_BUILDER_STREAM_PROP); } catch (Exception ex) { oddrUaDeviceBuilderStream = null; } try { oddrUaDeviceDatasourceStream = (InputStream) prprts.get(ODDR_UA_DEVICE_DATASOURCE_STREAM_PROP); } catch (Exception ex) { oddrUaDeviceDatasourceStream = null; } try { oddrUaDeviceBuilderPatchStreams = (InputStream[]) prprts.get(ODDR_UA_DEVICE_BUILDER_PATCH_STREAMS_PROP); } catch (Exception ex) { oddrUaDeviceBuilderPatchStreams = null; } try { oddrUaDeviceDatasourcePatchStreams = (InputStream[]) prprts .get(ODDR_UA_DEVICE_DATASOURCE_PATCH_STREAMS_PROP); } catch (Exception ex) { oddrUaDeviceDatasourcePatchStreams = null; } try { oddrUaBrowserDatasourceStream = (InputStream) prprts.get(ODDR_UA_BROWSER_DATASOURCE_STREAM_PROP); } catch (Exception ex) { oddrUaBrowserDatasourceStream = null; } try { oddrUaOperatingSystemDatasourceStream = (InputStream) prprts .get(ODDR_UA_OPERATINGSYSTEM_DATASOURCE_STREAM_PROP); } catch (Exception ex) { oddrUaOperatingSystemDatasourceStream = null; } String oddrThreshold = prprts.getProperty(ODDR_THRESHOLD_PROP); if ((oddrUaDeviceBuilderPath == null || oddrUaDeviceBuilderPath.trim().length() == 0) && oddrUaDeviceBuilderStream == null) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalArgumentException("Can not find property " + ODDR_UA_DEVICE_BUILDER_PATH_PROP)); } if ((oddrUaDeviceDatasourcePath == null || oddrUaDeviceDatasourcePath.trim().length() == 0) && oddrUaDeviceDatasourceStream == null) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalArgumentException("Can not find property " + ODDR_UA_DEVICE_DATASOURCE_PATH_PROP)); } String oddrUaDeviceBuilderPatchPathArray[] = null; if (oddrUaDeviceBuilderPatchPaths != null && oddrUaDeviceBuilderPatchPaths.trim().length() != 0) { oddrUaDeviceBuilderPatchPathArray = oddrUaDeviceBuilderPatchPaths.split(","); } else { oddrUaDeviceBuilderPatchPathArray = new String[0]; } String ooddrUaDeviceDatasourcePatchPathArray[] = null; if (oddrUaDeviceDatasourcePatchPaths != null && oddrUaDeviceDatasourcePatchPaths.trim().length() != 0) { ooddrUaDeviceDatasourcePatchPathArray = oddrUaDeviceDatasourcePatchPaths.split(","); } else { ooddrUaDeviceDatasourcePatchPathArray = new String[0]; } if (oddrUaDeviceBuilderPatchStreams == null) { oddrUaDeviceBuilderPatchStreams = new InputStream[0]; } if (oddrUaDeviceDatasourcePatchStreams == null) { oddrUaDeviceDatasourcePatchStreams = new InputStream[0]; } if ((oddrUaBrowserDatasourcePaths == null || oddrUaBrowserDatasourcePaths.trim().length() == 0) && oddrUaBrowserDatasourceStream == null) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalArgumentException("Can not find property " + ODDR_UA_BROWSER_DATASOURCE_PATH_PROP)); } if ((oddrUaOperatingSystemDatasourcePaths == null || oddrUaOperatingSystemDatasourcePaths.trim().length() == 0) && oddrUaOperatingSystemDatasourceStream == null) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalArgumentException( "Can not find property " + ODDR_UA_OPERATINGSYSTEM_DATASOURCE_PATH_PROP)); } if (oddrThreshold == null || oddrThreshold.trim().length() == 0) { this.threshold = ODDR_DEFAULT_THRESHOLD; } else { try { this.threshold = Integer.parseInt(oddrThreshold); if (this.threshold <= 0) { this.threshold = ODDR_DEFAULT_THRESHOLD; } } catch (NumberFormatException x) { this.threshold = ODDR_DEFAULT_THRESHOLD; } } Map<String, Device> devices = new HashMap<String, Device>(); DeviceDatasourceHandler deviceDatasourceHandler = new DeviceDatasourceHandler(devices, vocabularyHolder); InputStream stream = null; SAXParser parser = null; try { if (oddrUaDeviceDatasourceStream != null) { stream = oddrUaDeviceDatasourceStream; } else { stream = new FileInputStream(new File(oddrUaDeviceDatasourcePath)); } } catch (IOException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalArgumentException("Can not open " + ODDR_UA_DEVICE_DATASOURCE_PATH_PROP + " " + oddrUaDeviceDatasourcePath)); } try { parser = SAXParserFactory.newInstance().newSAXParser(); } catch (ParserConfigurationException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException("Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException("Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } try { parser.parse(stream, deviceDatasourceHandler); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException("Can not parse document: " + oddrUaDeviceDatasourcePath)); } catch (IOException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException( "Can not open " + ODDR_UA_DEVICE_DATASOURCE_PATH_PROP + " :" + oddrUaDeviceDatasourcePath)); } try { stream.close(); } catch (IOException ex) { logger.warn("", ex); } deviceDatasourceHandler.setPatching(true); if (oddrUaDeviceDatasourcePatchStreams != null) { for (int i = 0; i < oddrUaDeviceDatasourcePatchStreams.length; i++) { stream = oddrUaDeviceDatasourcePatchStreams[i]; try { parser = SAXParserFactory.newInstance().newSAXParser(); } catch (ParserConfigurationException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException( "Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException( "Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } try { parser.parse(stream, deviceDatasourceHandler); } catch (Exception ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException("Can not parse DeviceDatasource input stream " + i)); } try { stream.close(); } catch (IOException ex) { logger.warn("", ex); } } } else { for (int i = 0; i < ooddrUaDeviceDatasourcePatchPathArray.length; i++) { try { stream = new FileInputStream(new File(ooddrUaDeviceDatasourcePatchPathArray[i])); } catch (IOException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalArgumentException("Can not open " + ODDR_UA_DEVICE_DATASOURCE_PATH_PROP + " " + ooddrUaDeviceDatasourcePatchPathArray[i])); } try { parser = SAXParserFactory.newInstance().newSAXParser(); } catch (ParserConfigurationException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException( "Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException( "Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } try { parser.parse(stream, deviceDatasourceHandler); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException( "Can not parse document: " + ooddrUaDeviceDatasourcePatchPathArray[i])); } catch (IOException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException("Can not open " + ODDR_UA_DEVICE_DATASOURCE_PATH_PROP + " :" + ooddrUaDeviceDatasourcePatchPathArray[i])); } try { stream.close(); } catch (IOException ex) { logger.warn("", ex); } } } List<DeviceBuilder> builders = new ArrayList<DeviceBuilder>(); DeviceBuilderHandler deviceBuilderHandler = new DeviceBuilderHandler(builders); try { if (oddrUaDeviceBuilderStream != null) { stream = oddrUaDeviceBuilderStream; } else { stream = new FileInputStream(new File(oddrUaDeviceBuilderPath)); } } catch (IOException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalArgumentException( "Can not open " + ODDR_UA_DEVICE_BUILDER_PATH_PROP + " " + oddrUaDeviceBuilderPath)); } try { parser = SAXParserFactory.newInstance().newSAXParser(); } catch (ParserConfigurationException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException("Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException("Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } try { parser.parse(stream, deviceBuilderHandler); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException("Can not parse document: " + oddrUaDeviceBuilderPath)); } catch (IOException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException( "Can not open " + ODDR_UA_DEVICE_DATASOURCE_PATH_PROP + " :" + oddrUaDeviceBuilderPath)); } try { stream.close(); } catch (IOException ex) { logger.warn("", ex); } if (oddrUaDeviceBuilderPatchStreams != null) { for (int i = 0; i < oddrUaDeviceBuilderPatchStreams.length; i++) { stream = oddrUaDeviceBuilderPatchStreams[i]; try { parser = SAXParserFactory.newInstance().newSAXParser(); } catch (ParserConfigurationException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException( "Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException( "Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } try { parser.parse(stream, deviceBuilderHandler); } catch (Exception ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException("Can not parse DeviceBuilder input stream " + i)); } try { stream.close(); } catch (IOException ex) { logger.warn("", ex); } } } else { for (int i = 0; i < oddrUaDeviceBuilderPatchPathArray.length; i++) { try { stream = new FileInputStream(new File(oddrUaDeviceBuilderPatchPathArray[i])); } catch (IOException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalArgumentException("Can not open " + ODDR_UA_DEVICE_BUILDER_PATCH_PATHS_PROP + " " + oddrUaDeviceBuilderPatchPathArray[i])); } try { parser = SAXParserFactory.newInstance().newSAXParser(); } catch (ParserConfigurationException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException( "Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException( "Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } try { parser.parse(stream, deviceBuilderHandler); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException( "Can not parse document: " + oddrUaDeviceBuilderPatchPathArray[i])); } catch (IOException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException("Can not open " + ODDR_UA_DEVICE_DATASOURCE_PATH_PROP + " :" + oddrUaDeviceBuilderPatchPathArray[i])); } try { stream.close(); } catch (IOException ex) { logger.warn("", ex); } } } BrowserDatasourceHandler browserDatasourceHandler = new BrowserDatasourceHandler(vocabularyHolder); try { if (oddrUaBrowserDatasourceStream != null) { stream = oddrUaBrowserDatasourceStream; } else { stream = new FileInputStream(new File(oddrUaBrowserDatasourcePaths)); } } catch (IOException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalArgumentException("Can not open " + ODDR_UA_BROWSER_DATASOURCE_PATH_PROP + " " + oddrUaBrowserDatasourcePaths)); } try { parser = SAXParserFactory.newInstance().newSAXParser(); } catch (ParserConfigurationException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException("Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException("Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } try { parser.parse(stream, browserDatasourceHandler); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException("Can not parse document: " + oddrUaBrowserDatasourcePaths)); } catch (IOException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException( "Can not open " + ODDR_UA_BROWSER_DATASOURCE_PATH_PROP + " :" + oddrUaBrowserDatasourcePaths)); } try { stream.close(); } catch (IOException ex) { logger.warn("", ex); } OperatingSystemDatasourceHandler operatingSystemDatasourceHandler = new OperatingSystemDatasourceHandler( vocabularyHolder); try { if (oddrUaOperatingSystemDatasourceStream != null) { stream = oddrUaOperatingSystemDatasourceStream; } else { stream = new FileInputStream(new File(oddrUaOperatingSystemDatasourcePaths)); } } catch (IOException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalArgumentException("Can not open " + ODDR_UA_OPERATINGSYSTEM_DATASOURCE_PATH_PROP + " " + oddrUaOperatingSystemDatasourcePaths)); } try { parser = SAXParserFactory.newInstance().newSAXParser(); } catch (ParserConfigurationException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException("Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException("Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } try { parser.parse(stream, operatingSystemDatasourceHandler); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException("Can not parse document: " + oddrUaOperatingSystemDatasourcePaths)); } catch (IOException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException("Can not open " + ODDR_UA_OPERATINGSYSTEM_DATASOURCE_PATH_PROP + " :" + oddrUaOperatingSystemDatasourcePaths)); } try { stream.close(); } catch (IOException ex) { logger.warn("", ex); } deviceIdentificator = new DeviceIdentificator(deviceBuilderHandler.getBuilders(), deviceDatasourceHandler.getDevices()); deviceIdentificator.completeInit(); browserIdentificator = new BrowserIdentificator(new Builder[] { DefaultBrowserBuilder.getInstance() }, browserDatasourceHandler.getBrowsers()); browserIdentificator.completeInit(); osIdentificator = new OSIdentificator(new Builder[] { DefaultOSBuilder.getInstance() }, operatingSystemDatasourceHandler.getOperatingSystems()); osIdentificator.completeInit(); deviceDatasourceHandler = null; deviceBuilderHandler = null; browserDatasourceHandler = null; operatingSystemDatasourceHandler = null; this.defaultVocabularyIRI = defaultVocabularyIRI; parser = null; oddrVocabularyService = null; return; }
From source file:org.openddr.simpleapi.oddr.VitaminedODDRService.java
public void initialize(String defaultVocabularyIRI, Properties prprts) throws NameException, InitializationException { if (defaultVocabularyIRI == null || defaultVocabularyIRI.trim().length() == 0) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new NullPointerException("defaultVocabularyIRI can not be null")); }//from w ww .java 2 s . c o m /*Initializing VocabularyHolder*/ ODDRVocabularyService oddrVocabularyService = new ODDRVocabularyService(); oddrVocabularyService.initialize(prprts); vocabularyHolder = oddrVocabularyService.getVocabularyHolder(); vocabularyHolder.existVocabulary(defaultVocabularyIRI); String oddrUaDeviceBuilderPath = prprts.getProperty(ODDR_UA_DEVICE_BUILDER_PATH_PROP); String oddrUaDeviceDatasourcePath = prprts.getProperty(ODDR_UA_DEVICE_DATASOURCE_PATH_PROP); String oddrUaDeviceBuilderPatchPaths = prprts.getProperty(ODDR_UA_DEVICE_BUILDER_PATCH_PATHS_PROP); String oddrUaDeviceDatasourcePatchPaths = prprts.getProperty(ODDR_UA_DEVICE_DATASOURCE_PATCH_PATHS_PROP); String oddrUaBrowserDatasourcePaths = prprts.getProperty(ODDR_UA_BROWSER_DATASOURCE_PATH_PROP); String oddrUaOperatingSystemDatasourcePaths = prprts .getProperty(ODDR_UA_OPERATINGSYSTEM_DATASOURCE_PATH_PROP); Integer maxDeviceCache = new Integer(prprts.getProperty(ODDR_CACHE_DEVICE_SIZE)); Integer maxBrowserCache = new Integer(prprts.getProperty(ODDR_CACHE_BROWSER_SIZE)); Integer maxOsCache = new Integer(prprts.getProperty(ODDR_CACHE_OS_SIZE)); Integer maxUnknownCache = new Integer(prprts.getProperty(ODDR_CACHE_UNKNOWNUAS_SIZE)); InputStream oddrUaDeviceBuilderStream = null; InputStream oddrUaDeviceDatasourceStream = null; InputStream[] oddrUaDeviceBuilderPatchStreams = null; InputStream[] oddrUaDeviceDatasourcePatchStreams = null; InputStream oddrUaBrowserDatasourceStream = null; InputStream oddrUaOperatingSystemDatasourceStream = null; try { oddrUaDeviceBuilderStream = (InputStream) prprts.get(ODDR_UA_DEVICE_BUILDER_STREAM_PROP); } catch (Exception ex) { oddrUaDeviceBuilderStream = null; } try { oddrUaDeviceDatasourceStream = (InputStream) prprts.get(ODDR_UA_DEVICE_DATASOURCE_STREAM_PROP); } catch (Exception ex) { oddrUaDeviceDatasourceStream = null; } try { oddrUaDeviceBuilderPatchStreams = (InputStream[]) prprts.get(ODDR_UA_DEVICE_BUILDER_PATCH_STREAMS_PROP); } catch (Exception ex) { oddrUaDeviceBuilderPatchStreams = null; } try { oddrUaDeviceDatasourcePatchStreams = (InputStream[]) prprts .get(ODDR_UA_DEVICE_DATASOURCE_PATCH_STREAMS_PROP); } catch (Exception ex) { oddrUaDeviceDatasourcePatchStreams = null; } try { oddrUaBrowserDatasourceStream = (InputStream) prprts.get(ODDR_UA_BROWSER_DATASOURCE_STREAM_PROP); } catch (Exception ex) { oddrUaBrowserDatasourceStream = null; } try { oddrUaOperatingSystemDatasourceStream = (InputStream) prprts .get(ODDR_UA_OPERATINGSYSTEM_DATASOURCE_STREAM_PROP); } catch (Exception ex) { oddrUaOperatingSystemDatasourceStream = null; } String oddrThreshold = prprts.getProperty(ODDR_THRESHOLD_PROP); if ((oddrUaDeviceBuilderPath == null || oddrUaDeviceBuilderPath.trim().length() == 0) && oddrUaDeviceBuilderStream == null) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalArgumentException("Can not find property " + ODDR_UA_DEVICE_BUILDER_PATH_PROP)); } if ((oddrUaDeviceDatasourcePath == null || oddrUaDeviceDatasourcePath.trim().length() == 0) && oddrUaDeviceDatasourceStream == null) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalArgumentException("Can not find property " + ODDR_UA_DEVICE_DATASOURCE_PATH_PROP)); } String oddrUaDeviceBuilderPatchPathArray[] = null; if (oddrUaDeviceBuilderPatchPaths != null && oddrUaDeviceBuilderPatchPaths.trim().length() != 0) { oddrUaDeviceBuilderPatchPathArray = oddrUaDeviceBuilderPatchPaths.split(","); } else { oddrUaDeviceBuilderPatchPathArray = new String[0]; } String ooddrUaDeviceDatasourcePatchPathArray[] = null; if (oddrUaDeviceDatasourcePatchPaths != null && oddrUaDeviceDatasourcePatchPaths.trim().length() != 0) { ooddrUaDeviceDatasourcePatchPathArray = oddrUaDeviceDatasourcePatchPaths.split(","); } else { ooddrUaDeviceDatasourcePatchPathArray = new String[0]; } if (oddrUaDeviceBuilderPatchStreams == null) { oddrUaDeviceBuilderPatchStreams = new InputStream[0]; } if (oddrUaDeviceDatasourcePatchStreams == null) { oddrUaDeviceDatasourcePatchStreams = new InputStream[0]; } if ((oddrUaBrowserDatasourcePaths == null || oddrUaBrowserDatasourcePaths.trim().length() == 0) && oddrUaBrowserDatasourceStream == null) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalArgumentException("Can not find property " + ODDR_UA_BROWSER_DATASOURCE_PATH_PROP)); } if ((oddrUaOperatingSystemDatasourcePaths == null || oddrUaOperatingSystemDatasourcePaths.trim().length() == 0) && oddrUaOperatingSystemDatasourceStream == null) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalArgumentException( "Can not find property " + ODDR_UA_OPERATINGSYSTEM_DATASOURCE_PATH_PROP)); } if (oddrThreshold == null || oddrThreshold.trim().length() == 0) { this.threshold = ODDR_DEFAULT_THRESHOLD; } else { try { this.threshold = Integer.parseInt(oddrThreshold); if (this.threshold <= 0) { this.threshold = ODDR_DEFAULT_THRESHOLD; } } catch (NumberFormatException x) { this.threshold = ODDR_DEFAULT_THRESHOLD; } } Map<String, Device> devices = new HashMap<String, Device>(); DeviceDatasourceHandler deviceDatasourceHandler = new DeviceDatasourceHandler(devices, vocabularyHolder); InputStream stream = null; SAXParser parser = null; try { if (oddrUaDeviceDatasourceStream != null) { stream = oddrUaDeviceDatasourceStream; } else { stream = new FileInputStream(new File(oddrUaDeviceDatasourcePath)); } } catch (IOException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalArgumentException("Can not open " + ODDR_UA_DEVICE_DATASOURCE_PATH_PROP + " " + oddrUaDeviceDatasourcePath)); } try { parser = SAXParserFactory.newInstance().newSAXParser(); } catch (ParserConfigurationException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException("Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException("Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } try { parser.parse(stream, deviceDatasourceHandler); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException("Can not parse document: " + oddrUaDeviceDatasourcePath)); } catch (IOException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException( "Can not open " + ODDR_UA_DEVICE_DATASOURCE_PATH_PROP + " :" + oddrUaDeviceDatasourcePath)); } try { stream.close(); } catch (IOException ex) { logger.warn("", ex); } deviceDatasourceHandler.setPatching(true); if (oddrUaDeviceDatasourcePatchStreams != null) { for (int i = 0; i < oddrUaDeviceDatasourcePatchStreams.length; i++) { stream = oddrUaDeviceDatasourcePatchStreams[i]; try { parser = SAXParserFactory.newInstance().newSAXParser(); } catch (ParserConfigurationException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException( "Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException( "Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } try { parser.parse(stream, deviceDatasourceHandler); } catch (Exception ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException("Can not parse DeviceDatasource input stream " + i)); } try { stream.close(); } catch (IOException ex) { logger.warn("", ex); } } } else { for (int i = 0; i < ooddrUaDeviceDatasourcePatchPathArray.length; i++) { try { stream = new FileInputStream(new File(ooddrUaDeviceDatasourcePatchPathArray[i])); } catch (IOException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalArgumentException("Can not open " + ODDR_UA_DEVICE_DATASOURCE_PATH_PROP + " " + ooddrUaDeviceDatasourcePatchPathArray[i])); } try { parser = SAXParserFactory.newInstance().newSAXParser(); } catch (ParserConfigurationException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException( "Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException( "Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } try { parser.parse(stream, deviceDatasourceHandler); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException( "Can not parse document: " + ooddrUaDeviceDatasourcePatchPathArray[i])); } catch (IOException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException("Can not open " + ODDR_UA_DEVICE_DATASOURCE_PATH_PROP + " :" + ooddrUaDeviceDatasourcePatchPathArray[i])); } try { stream.close(); } catch (IOException ex) { logger.warn("", ex); } } } List<DeviceBuilder> builders = new ArrayList<DeviceBuilder>(); DeviceBuilderHandler deviceBuilderHandler = new DeviceBuilderHandler(builders); try { if (oddrUaDeviceBuilderStream != null) { stream = oddrUaDeviceBuilderStream; } else { stream = new FileInputStream(new File(oddrUaDeviceBuilderPath)); } } catch (IOException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalArgumentException( "Can not open " + ODDR_UA_DEVICE_BUILDER_PATH_PROP + " " + oddrUaDeviceBuilderPath)); } try { parser = SAXParserFactory.newInstance().newSAXParser(); } catch (ParserConfigurationException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException("Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException("Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } try { parser.parse(stream, deviceBuilderHandler); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException("Can not parse document: " + oddrUaDeviceBuilderPath)); } catch (IOException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException( "Can not open " + ODDR_UA_DEVICE_DATASOURCE_PATH_PROP + " :" + oddrUaDeviceBuilderPath)); } try { stream.close(); } catch (IOException ex) { logger.warn("", ex); } if (oddrUaDeviceBuilderPatchStreams != null) { for (int i = 0; i < oddrUaDeviceBuilderPatchStreams.length; i++) { stream = oddrUaDeviceBuilderPatchStreams[i]; try { parser = SAXParserFactory.newInstance().newSAXParser(); } catch (ParserConfigurationException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException( "Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException( "Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } try { parser.parse(stream, deviceBuilderHandler); } catch (Exception ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException("Can not parse DeviceBuilder input stream " + i)); } try { stream.close(); } catch (IOException ex) { logger.warn("", ex); } } } else { for (int i = 0; i < oddrUaDeviceBuilderPatchPathArray.length; i++) { try { stream = new FileInputStream(new File(oddrUaDeviceBuilderPatchPathArray[i])); } catch (IOException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalArgumentException("Can not open " + ODDR_UA_DEVICE_BUILDER_PATCH_PATHS_PROP + " " + oddrUaDeviceBuilderPatchPathArray[i])); } try { parser = SAXParserFactory.newInstance().newSAXParser(); } catch (ParserConfigurationException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException( "Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException( "Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } try { parser.parse(stream, deviceBuilderHandler); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException( "Can not parse document: " + oddrUaDeviceBuilderPatchPathArray[i])); } catch (IOException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException("Can not open " + ODDR_UA_DEVICE_DATASOURCE_PATH_PROP + " :" + oddrUaDeviceBuilderPatchPathArray[i])); } try { stream.close(); } catch (IOException ex) { logger.warn("", ex); } } } BrowserDatasourceHandler browserDatasourceHandler = new BrowserDatasourceHandler(vocabularyHolder); try { if (oddrUaBrowserDatasourceStream != null) { stream = oddrUaBrowserDatasourceStream; } else { stream = new FileInputStream(new File(oddrUaBrowserDatasourcePaths)); } } catch (IOException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalArgumentException("Can not open " + ODDR_UA_BROWSER_DATASOURCE_PATH_PROP + " " + oddrUaBrowserDatasourcePaths)); } try { parser = SAXParserFactory.newInstance().newSAXParser(); } catch (ParserConfigurationException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException("Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException("Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } try { parser.parse(stream, browserDatasourceHandler); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException("Can not parse document: " + oddrUaBrowserDatasourcePaths)); } catch (IOException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException( "Can not open " + ODDR_UA_BROWSER_DATASOURCE_PATH_PROP + " :" + oddrUaBrowserDatasourcePaths)); } try { stream.close(); } catch (IOException ex) { logger.warn("", ex); } OperatingSystemDatasourceHandler operatingSystemDatasourceHandler = new OperatingSystemDatasourceHandler( vocabularyHolder); try { if (oddrUaOperatingSystemDatasourceStream != null) { stream = oddrUaOperatingSystemDatasourceStream; } else { stream = new FileInputStream(new File(oddrUaOperatingSystemDatasourcePaths)); } } catch (IOException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalArgumentException("Can not open " + ODDR_UA_OPERATINGSYSTEM_DATASOURCE_PATH_PROP + " " + oddrUaOperatingSystemDatasourcePaths)); } try { parser = SAXParserFactory.newInstance().newSAXParser(); } catch (ParserConfigurationException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException("Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException("Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } try { parser.parse(stream, operatingSystemDatasourceHandler); } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException("Can not parse document: " + oddrUaOperatingSystemDatasourcePaths)); } catch (IOException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException("Can not open " + ODDR_UA_OPERATINGSYSTEM_DATASOURCE_PATH_PROP + " :" + oddrUaOperatingSystemDatasourcePaths)); } try { stream.close(); } catch (IOException ex) { logger.warn("", ex); } deviceIdentificator = new CachedDeviceIdentificator(deviceBuilderHandler.getBuilders(), deviceDatasourceHandler.getDevices(), maxDeviceCache, maxUnknownCache); deviceIdentificator.completeInit(); browserIdentificator = new CachedBrowserIdentificator(new Builder[] { DefaultBrowserBuilder.getInstance() }, browserDatasourceHandler.getBrowsers(), maxBrowserCache, maxUnknownCache); browserIdentificator.completeInit(); osIdentificator = new CachedOSIdentificator(new Builder[] { DefaultOSBuilder.getInstance() }, operatingSystemDatasourceHandler.getOperatingSystems(), maxOsCache, maxUnknownCache); osIdentificator.completeInit(); deviceDatasourceHandler = null; deviceBuilderHandler = null; browserDatasourceHandler = null; operatingSystemDatasourceHandler = null; this.defaultVocabularyIRI = defaultVocabularyIRI; parser = null; oddrVocabularyService = null; return; }
From source file:org.openhealthtools.openatna.anom.codes.CodeParser.java
public static void parse(Collection<String> codes) { try {//from w ww. j a va 2 s . c o m SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setValidating(false); javax.xml.parsers.SAXParser sp = spf.newSAXParser(); Handler handler = new Handler(); for (String code : codes) { try { URL url = new URL(code); InputSource input = new InputSource(url.openStream()); input.setSystemId(code.toString()); sp.parse(input, handler); } catch (Exception e) { log.warn("Error parsing codes at:" + code); } } } catch (Exception e) { throw new RuntimeException("Error loading system codes", e); } }
From source file:org.openlmis.odkapi.service.ODKSubmissionService.java
public void saveODKSubmissionData(Set submissionsFilesSet) throws ODKAccountNotFoundException, FacilityPictureNotFoundException, ODKCollectXMLSubmissionFileNotFoundException, ODKCollectXMLSubmissionParserConfigurationException, ODKCollectXMLSubmissionSAXException, FacilityNotFoundException, ODKXFormNotFoundException { // first get the XML submission file submissionPictures = new HashMap<String, MultipartFile>(); Iterator iterator = submissionsFilesSet.iterator(); MultipartFile XMLSubmissionFile = null; while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); String fileName = (String) entry.getKey(); if (fileName.equals("xml_submission_file")) { XMLSubmissionFile = (MultipartFile) entry.getValue(); } else {//w w w. ja v a2s .c om submissionPictures.put(fileName, (MultipartFile) entry.getValue()); } } // first parse the xml file only to get the form id SAXParserFactory odkXFormIDParserFactory = SAXParserFactory.newInstance(); ODKSubmissionXFormIDSAXHandler odkXFormIDHandler = new ODKSubmissionXFormIDSAXHandler(); try { SAXParser odkXFormIDParser = odkXFormIDParserFactory.newSAXParser(); odkXFormIDParser.parse(XMLSubmissionFile.getInputStream(), odkXFormIDHandler); } catch (SAXException exception) { throw new ODKCollectXMLSubmissionSAXException(); } catch (IOException e) { throw new ODKCollectXMLSubmissionFileNotFoundException(); } catch (ParserConfigurationException e) { throw new ODKCollectXMLSubmissionParserConfigurationException(); } // get the form build id formBuildID = odkXFormIDHandler.getFormBuildId(); // get the xform survey type using the form build id ODKXForm tempXForm = odkxFormService.getXFormByFormId(formBuildID); if (tempXForm == null) { throw new ODKXFormNotFoundException(); } ODKXFormSurveyType surveyType = odkxFormService.getXFormSurveyTypeById(tempXForm.getODKXFormSurveyTypeId()); if (surveyType.getSurveyName().equals("Facility GPS Location and Pictures")) { saveFacilityGPSLocationAndPicturesSurveyData(XMLSubmissionFile); } else if (surveyType.getSurveyName().equals("Stock Status")) { saveStockStatusSurveyData(XMLSubmissionFile); } else if (surveyType.getSurveyName().equals("Proof of Delivery Survey")) { saveProofOfDeliverySurveyData(XMLSubmissionFile); } else if (surveyType.getSurveyName().equals("ZNZ Survey")) { saveZNZSurveyData(XMLSubmissionFile); } else { // Handle } }
From source file:org.openlmis.odkapi.service.ODKSubmissionService.java
public void saveZNZSurveyData(MultipartFile XMLSubmissionFile) throws ODKAccountNotFoundException, FacilityPictureNotFoundException, ODKCollectXMLSubmissionFileNotFoundException, ODKCollectXMLSubmissionParserConfigurationException, ODKCollectXMLSubmissionSAXException { SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); ODKZNZSurveySubmissionSAXHandler handler; handler = new ODKZNZSurveySubmissionSAXHandler(); String filePath = "/public/odk-collect-submissions/"; InputStream inputStream;//from www . ja v a2 s .c o m FileOutputStream outputStream; // first save the xml submission file try { inputStream = XMLSubmissionFile.getInputStream(); File newFile = new File(servletContext.getRealPath(filePath + XMLSubmissionFile.getOriginalFilename())); outputStream = new FileOutputStream(newFile); int read = 0; byte[] bytes = new byte[1024]; while ((read = inputStream.read(bytes)) != -1) { outputStream.write(bytes, 0, read); } inputStream.close(); outputStream.close(); } catch (Exception ex) { ex.printStackTrace(); } // read the submission file into a string String xmlString = ""; try { File savedSubmissionFile = new File( servletContext.getRealPath(filePath + XMLSubmissionFile.getOriginalFilename())); xmlString = readFile(servletContext.getRealPath(filePath + XMLSubmissionFile.getOriginalFilename()), Charset.defaultCharset()); } catch (Exception ex) { ex.printStackTrace(); } // use XML Formatter XmlFormatter formatter = new XmlFormatter(); String formattedXmlSubmission = formatter.format(xmlString); // get input stream from the document object of the formatted xml Document xmlSubmissionDocument; try { xmlSubmissionDocument = stringToDom(formattedXmlSubmission); } catch (SAXException exception) { throw new ODKCollectXMLSubmissionSAXException(); } catch (IOException e) { throw new ODKCollectXMLSubmissionFileNotFoundException(); } catch (ParserConfigurationException e) { throw new ODKCollectXMLSubmissionParserConfigurationException(); } ByteArrayOutputStream outputStreamForDocument = new ByteArrayOutputStream(); Source xmlSource = new DOMSource(xmlSubmissionDocument); Result outputTarget = new StreamResult(outputStreamForDocument); try { TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget); } catch (TransformerConfigurationException transformerConfigurationException) { transformerConfigurationException.printStackTrace(); } catch (TransformerException transformerException) { transformerException.printStackTrace(); } InputStream inputStreamForDocument = new ByteArrayInputStream(outputStreamForDocument.toByteArray()); try { SAXParser saxParser = saxParserFactory.newSAXParser(); saxParser.parse(inputStreamForDocument, handler); } catch (SAXException exception) { throw new ODKCollectXMLSubmissionSAXException(); } catch (IOException e) { throw new ODKCollectXMLSubmissionFileNotFoundException(); } catch (ParserConfigurationException e) { throw new ODKCollectXMLSubmissionParserConfigurationException(); } odkSubmission = handler.getOdkSubmission(); odkAccount = handler.getOdkAccount(); pictures = handler.getPictures(); ODKAccount tempAccount; tempAccount = odkAccountService.authenticate(odkAccount); odkSubmission.setOdkAccountId(tempAccount.getId()); odkSubmission.setActive(true); this.insertODKSubmission(odkSubmission); Long lastODKSubmissionId = odkSubmissionRepository.getLastSubmissionId(); // save storage part with gps coordinates and pictures odkStorageSurveySubmission = handler.getOdkStorageSurveySubmission(); odkStorageSurveySubmission.setODKSubmissionId(lastODKSubmissionId); ArrayList<byte[]> facilityPictures = getZNZSurveyPictures(); for (int count = 0; count < facilityPictures.size(); count++) { try { Method picMethod = odkStorageSurveySubmission.getClass().getMethod(pictureSetMethods[count], new Class[] { byte[].class }); picMethod.invoke(odkStorageSurveySubmission, facilityPictures.get(count)); } catch (IllegalAccessException ex) { // TO DO handle } catch (NoSuchMethodException ex) { // TO DO handle } catch (SecurityException ex) { // TO DO handle } catch (IllegalArgumentException ex) { // TO DO handle } catch (InvocationTargetException ex) { // TO DO handle } } odkStorageSurveySubmission.setTotalPercentage(getTotalPercentage(odkStorageSurveySubmission, 15)); odkStorageSurveySubmissionRepository.insert(odkStorageSurveySubmission); // save lmis part odkLmisSurveySubmission = handler.getOdkLmisSurveySubmission(); odkLmisSurveySubmission.setODKSubmissionId(lastODKSubmissionId); odkLmisSurveySubmission.setTotalPercentage(getTotalPercentage(odkLmisSurveySubmission, 11)); odklmisSurveySubmissionRepository.insert(odkLmisSurveySubmission); // save r and r part odkRandRSubmission = handler.getOdkRandRSubmission(); odkRandRSubmission.setODKSubmissionId(lastODKSubmissionId); odkRandRSubmission.setTotalPercentage(getTotalPercentage(odkRandRSubmission, 8)); odkRandRSubmissionRepository.insert(odkRandRSubmission); }
From source file:org.openlmis.odkapi.service.ODKSubmissionService.java
public void saveProofOfDeliverySurveyData(MultipartFile XMLSubmissionFile) throws ODKAccountNotFoundException, FacilityPictureNotFoundException, ODKCollectXMLSubmissionFileNotFoundException, ODKCollectXMLSubmissionParserConfigurationException, ODKCollectXMLSubmissionSAXException, ODKXFormNotFoundException { SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); ODKProofOfDeliverySubmissionSAXHandler handler; handler = new ODKProofOfDeliverySubmissionSAXHandler(); String filePath = "/public/odk-collect-submissions/"; InputStream inputStream;/*from w ww .j av a 2 s .c o m*/ FileOutputStream outputStream; // first save the xml submission file try { inputStream = XMLSubmissionFile.getInputStream(); File newFile = new File(servletContext.getRealPath(filePath + XMLSubmissionFile.getOriginalFilename())); outputStream = new FileOutputStream(newFile); int read = 0; byte[] bytes = new byte[1024]; while ((read = inputStream.read(bytes)) != -1) { outputStream.write(bytes, 0, read); } inputStream.close(); outputStream.close(); } catch (Exception ex) { ex.printStackTrace(); } // read the submission file into a string String xmlString = ""; try { File savedSubmissionFile = new File( servletContext.getRealPath(filePath + XMLSubmissionFile.getOriginalFilename())); xmlString = readFile(servletContext.getRealPath(filePath + XMLSubmissionFile.getOriginalFilename()), Charset.defaultCharset()); } catch (Exception ex) { ex.printStackTrace(); } // use XML Formatter XmlFormatter formatter = new XmlFormatter(); String formattedXmlSubmission = formatter.format(xmlString); // get input stream from the document object of the formatted xml Document xmlSubmissionDocument; try { xmlSubmissionDocument = stringToDom(formattedXmlSubmission); } catch (SAXException exception) { throw new ODKCollectXMLSubmissionSAXException(); } catch (IOException e) { throw new ODKCollectXMLSubmissionFileNotFoundException(); } catch (ParserConfigurationException e) { throw new ODKCollectXMLSubmissionParserConfigurationException(); } ByteArrayOutputStream outputStreamForDocument = new ByteArrayOutputStream(); Source xmlSource = new DOMSource(xmlSubmissionDocument); Result outputTarget = new StreamResult(outputStreamForDocument); try { TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget); } catch (TransformerConfigurationException transformerConfigurationException) { transformerConfigurationException.printStackTrace(); } catch (TransformerException transformerException) { transformerException.printStackTrace(); } InputStream inputStreamForDocument = new ByteArrayInputStream(outputStreamForDocument.toByteArray()); try { SAXParser saxParser = saxParserFactory.newSAXParser(); saxParser.parse(inputStreamForDocument, handler); } catch (SAXException exception) { throw new ODKCollectXMLSubmissionSAXException(); } catch (IOException e) { throw new ODKCollectXMLSubmissionFileNotFoundException(); } catch (ParserConfigurationException e) { throw new ODKCollectXMLSubmissionParserConfigurationException(); } odkSubmission = handler.getOdkSubmission(); odkAccount = handler.getOdkAccount(); ODKAccount tempAccount; tempAccount = odkAccountService.authenticate(odkAccount); odkSubmission.setOdkAccountId(tempAccount.getId()); odkSubmission.setActive(true); this.insertODKSubmission(odkSubmission); Long lastODKSubmissionId = odkSubmissionRepository.getLastSubmissionId(); odkProofOfDeliverySubmissionData = handler.getOdkProofOfDeliverySubmissionData(); this.insertODKProofOfDeliverySubmissionData(odkProofOfDeliverySubmissionData); }
From source file:org.openlmis.odkapi.service.ODKSubmissionService.java
public void saveStockStatusSurveyData(MultipartFile XMLSubmissionFile) throws ODKAccountNotFoundException, FacilityPictureNotFoundException, ODKCollectXMLSubmissionFileNotFoundException, ODKCollectXMLSubmissionParserConfigurationException, ODKCollectXMLSubmissionSAXException { SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); ODKStockStatusSubmissionSAXHandler handler; handler = new ODKStockStatusSubmissionSAXHandler(); // To Do : the xml is not well formatted when stock status submission is done , find out why // this is a temporary solution String filePath = "/public/odk-collect-submissions/"; InputStream inputStream;//from www . j a v a 2 s. c o m FileOutputStream outputStream; // first save the xml submission file try { inputStream = XMLSubmissionFile.getInputStream(); File newFile = new File(servletContext.getRealPath(filePath + XMLSubmissionFile.getOriginalFilename())); outputStream = new FileOutputStream(newFile); int read = 0; byte[] bytes = new byte[1024]; while ((read = inputStream.read(bytes)) != -1) { outputStream.write(bytes, 0, read); } inputStream.close(); outputStream.close(); } catch (Exception ex) { ex.printStackTrace(); } // read the submission file into a string String xmlString = ""; try { File savedSubmissionFile = new File( servletContext.getRealPath(filePath + XMLSubmissionFile.getOriginalFilename())); xmlString = readFile(servletContext.getRealPath(filePath + XMLSubmissionFile.getOriginalFilename()), Charset.defaultCharset()); } catch (Exception ex) { ex.printStackTrace(); } // use XML Formatter XmlFormatter formatter = new XmlFormatter(); String formattedXmlSubmission = formatter.format(xmlString); // get input stream from the document object of the formatted xml Document xmlSubmissionDocument; try { xmlSubmissionDocument = stringToDom(formattedXmlSubmission); } catch (SAXException exception) { throw new ODKCollectXMLSubmissionSAXException(); } catch (IOException e) { throw new ODKCollectXMLSubmissionFileNotFoundException(); } catch (ParserConfigurationException e) { throw new ODKCollectXMLSubmissionParserConfigurationException(); } ByteArrayOutputStream outputStreamForDocument = new ByteArrayOutputStream(); Source xmlSource = new DOMSource(xmlSubmissionDocument); Result outputTarget = new StreamResult(outputStreamForDocument); try { TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget); } catch (TransformerConfigurationException transformerConfigurationException) { transformerConfigurationException.printStackTrace(); } catch (TransformerException transformerException) { transformerException.printStackTrace(); } InputStream inputStreamForDocument = new ByteArrayInputStream(outputStreamForDocument.toByteArray()); try { SAXParser saxParser = saxParserFactory.newSAXParser(); saxParser.parse(inputStreamForDocument, handler); } catch (SAXException exception) { throw new ODKCollectXMLSubmissionSAXException(); } catch (IOException e) { throw new ODKCollectXMLSubmissionFileNotFoundException(); } catch (ParserConfigurationException e) { throw new ODKCollectXMLSubmissionParserConfigurationException(); } odkSubmission = handler.getOdkSubmission(); odkAccount = handler.getOdkAccount(); ODKAccount tempAccount; tempAccount = odkAccountService.authenticate(odkAccount); odkSubmission.setOdkAccountId(tempAccount.getId()); odkSubmission.setActive(true); this.insertODKSubmission(odkSubmission); Long lastODKSubmissionId = odkSubmissionRepository.getLastSubmissionId(); listODKStockStatusSubmissions = handler.getListODKStockStatusSubmissions(); for (ODKStockStatusSubmission odkStockStatusSubmission : listODKStockStatusSubmissions) { odkStockStatusSubmission.setODKSubmissionId(lastODKSubmissionId); odkStockStatusSubmission.setActive(true); this.insertStockStatus(odkStockStatusSubmission); } }
From source file:org.openlmis.odkapi.service.ODKSubmissionService.java
public void saveFacilityGPSLocationAndPicturesSurveyData(MultipartFile XMLSubmissionFile) throws ODKAccountNotFoundException, FacilityPictureNotFoundException, ODKCollectXMLSubmissionFileNotFoundException, ODKCollectXMLSubmissionParserConfigurationException, ODKCollectXMLSubmissionSAXException, FacilityNotFoundException { // parse the xml file SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); ODKSubmissionSAXHandler handler;//from www .j a v a 2 s .c o m handler = new ODKSubmissionSAXHandler(); try { SAXParser saxParser = saxParserFactory.newSAXParser(); saxParser.parse(XMLSubmissionFile.getInputStream(), handler); } catch (SAXException exception) { throw new ODKCollectXMLSubmissionSAXException(); } catch (IOException e) { throw new ODKCollectXMLSubmissionFileNotFoundException(); } catch (ParserConfigurationException e) { throw new ODKCollectXMLSubmissionParserConfigurationException(); } odkSubmission = handler.getOdkSubmission(); odkAccount = handler.getOdkAccount(); ODKAccount tempAccount; tempAccount = odkAccountService.authenticate(odkAccount); odkSubmission.setOdkAccountId(tempAccount.getId()); odkSubmission.setActive(true); this.insertODKSubmission(odkSubmission); listOfODKSubmissionData = handler.getListOfODKSubmissionData(); associatedFacilityPictures = handler.getFacilityPictures(); assignFacilityPictures(); saveFacilityData(); saveODKSubmissionData(); }