List of usage examples for javax.xml.parsers DocumentBuilderFactory setIgnoringComments
public void setIgnoringComments(boolean ignoreComments)
From source file:org.dspace.app.util.DCInputsReader.java
private void buildInputs(String fileName) throws DCInputsReaderException { formDefns = new HashMap<String, List<Map<String, String>>>(); valuePairs = new HashMap<String, List<String>>(); String uri = "file:" + new File(fileName).getAbsolutePath(); try {/* w w w . j av a 2 s . co m*/ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setIgnoringComments(true); factory.setIgnoringElementContentWhitespace(true); DocumentBuilder db = factory.newDocumentBuilder(); Document doc = db.parse(uri); doNodes(doc); checkValues(); } catch (FactoryConfigurationError fe) { throw new DCInputsReaderException("Cannot create Submission form parser", fe); } catch (Exception e) { throw new DCInputsReaderException("Error creating submission forms: " + e); } }
From source file:org.dspace.app.util.SubmissionConfigReader.java
/** * Parse an XML encoded item submission configuration file. * <P>//from w w w. j av a 2 s. c o m * Creates two main hashmaps: * <ul> * <li>Hashmap of Collection to Submission definition mappings - * defines which Submission process a particular collection uses * <li>Hashmap of all Submission definitions. List of all valid * Submision Processes by name. * </ul> */ private void buildInputs(String fileName) throws SubmissionConfigReaderException { collectionToSubmissionConfig = new HashMap<String, String>(); submitDefns = new HashMap<String, List<Map<String, String>>>(); String uri = "file:" + new File(fileName).getAbsolutePath(); try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setIgnoringComments(true); factory.setIgnoringElementContentWhitespace(true); DocumentBuilder db = factory.newDocumentBuilder(); Document doc = db.parse(uri); doNodes(doc); } catch (FactoryConfigurationError fe) { throw new SubmissionConfigReaderException("Cannot create Item Submission Configuration parser", fe); } catch (Exception e) { throw new SubmissionConfigReaderException("Error creating Item Submission Configuration: " + e); } }
From source file:org.dspace.content.packager.RoleIngester.java
/** * Ingest roles from an InputStream./*from w w w . ja va 2s . c o m*/ * * @param context * DSpace Context * @param parent * the Parent DSpaceObject * @param stream * the XML Document InputStream * @throws PackageException * @throws SQLException * @throws AuthorizeException */ public static void ingestStream(Context context, DSpaceObject parent, PackageParameters params, InputStream stream) throws PackageException, SQLException, AuthorizeException { Document document; try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setIgnoringComments(true); dbf.setCoalescing(true); DocumentBuilder db = dbf.newDocumentBuilder(); document = db.parse(stream); } catch (ParserConfigurationException e) { throw new PackageException(e); } catch (SAXException e) { throw new PackageException(e); } catch (IOException e) { throw new PackageException(e); } /* * TODO ? finally { close(stream); } */ ingestDocument(context, parent, params, document); }
From source file:org.dspace.content.packager.RoleIngester.java
@Override public DSpaceObject ingest(Context context, DSpaceObject parent, File pkgFile, PackageParameters params, String license)/*from w w w .j a v a 2s . c o m*/ throws PackageException, CrosswalkException, AuthorizeException, SQLException, IOException { Document document; try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setIgnoringComments(true); dbf.setCoalescing(true); DocumentBuilder db = dbf.newDocumentBuilder(); document = db.parse(pkgFile); } catch (ParserConfigurationException e) { throw new PackageException(e); } catch (SAXException e) { throw new PackageException(e); } ingestDocument(context, parent, params, document); /* Does not create a DSpaceObject */ return null; }
From source file:org.dspace.submit.lookup.ArXivFileDataLoader.java
@Override public RecordSet getRecords() throws MalformedSourceException { RecordSet recordSet = new RecordSet(); try {//from w w w . ja v a2 s . c o m InputStream inputStream = new FileInputStream(new File(filename)); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setIgnoringComments(true); factory.setIgnoringElementContentWhitespace(true); DocumentBuilder db = factory.newDocumentBuilder(); Document inDoc = db.parse(inputStream); Element xmlRoot = inDoc.getDocumentElement(); List<Element> dataRoots = XMLUtils.getElementList(xmlRoot, "entry"); for (Element dataRoot : dataRoots) { Record record = ArxivUtils.convertArxixDomToRecord(dataRoot); if (record != null) { recordSet.addRecord(convertFields(record)); } } } catch (FileNotFoundException e) { log.error(e.getMessage(), e); } catch (ParserConfigurationException e) { log.error(e.getMessage(), e); } catch (SAXException e) { log.error(e.getMessage(), e); } catch (IOException e) { log.error(e.getMessage(), e); } return recordSet; }
From source file:org.dspace.submit.lookup.CiNiiFileDataLoader.java
@Override public RecordSet getRecords() throws MalformedSourceException { RecordSet recordSet = new RecordSet(); try {//from w w w. ja v a 2 s. co m InputStream inputStream = new FileInputStream(new File(filename)); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setIgnoringComments(true); factory.setIgnoringElementContentWhitespace(true); DocumentBuilder db = factory.newDocumentBuilder(); Document inDoc = db.parse(inputStream); Element xmlRoot = inDoc.getDocumentElement(); // There is no element to represent an record, so we can not process // multi records at once. Record record = CiNiiUtils.convertCiNiiDomToRecord(xmlRoot); if (record != null) { recordSet.addRecord(convertFields(record)); } } catch (FileNotFoundException e) { log.error(e.getMessage(), e); } catch (ParserConfigurationException e) { log.error(e.getMessage(), e); } catch (SAXException e) { log.error(e.getMessage(), e); } catch (IOException e) { log.error(e.getMessage(), e); } return recordSet; }
From source file:org.dspace.submit.lookup.CiNiiService.java
/** * Get metadata by searching CiNii RDF API with CiNii NAID * *///from ww w . j a v a 2 s .c o m private Record search(String id, String appId) throws IOException, HttpException { GetMethod method = null; try { HttpClient client = new HttpClient(); client.setTimeout(timeout); method = new GetMethod("http://ci.nii.ac.jp/naid/" + id + ".rdf?appid=" + appId); // Execute the method. int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { if (statusCode == HttpStatus.SC_BAD_REQUEST) throw new RuntimeException("CiNii RDF is not valid"); else throw new RuntimeException("CiNii RDF Http call failed: " + method.getStatusLine()); } try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setIgnoringComments(true); factory.setIgnoringElementContentWhitespace(true); DocumentBuilder db = factory.newDocumentBuilder(); Document inDoc = db.parse(method.getResponseBodyAsStream()); Element xmlRoot = inDoc.getDocumentElement(); return CiNiiUtils.convertCiNiiDomToRecord(xmlRoot); } catch (Exception e) { throw new RuntimeException("CiNii RDF identifier is not valid or not exist"); } } finally { if (method != null) { method.releaseConnection(); } } }
From source file:org.dspace.submit.lookup.CiNiiService.java
/** * Get CiNii NAIDs by searching CiNii OpenURL API with title, author and year * */// w w w . j a va2 s .c om private List<String> getCiNiiIDs(String title, String author, int year, int maxResults, String appId) throws IOException, HttpException { // Need at least one query term if (title == null && author == null && year == -1) { return null; } GetMethod method = null; List<String> ids = new ArrayList<String>(); try { HttpClient client = new HttpClient(); client.setTimeout(timeout); StringBuilder query = new StringBuilder(); query.append("format=rss&appid=").append(appId).append("&count=").append(maxResults); if (title != null) { query.append("&title=").append(URLEncoder.encode(title, "UTF-8")); } if (author != null) { query.append("&author=").append(URLEncoder.encode(author, "UTF-8")); } if (year != -1) { query.append("&year_from=").append(String.valueOf(year)); query.append("&year_to=").append(String.valueOf(year)); } method = new GetMethod("http://ci.nii.ac.jp/opensearch/search?" + query.toString()); // Execute the method. int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { if (statusCode == HttpStatus.SC_BAD_REQUEST) throw new RuntimeException("CiNii OpenSearch query is not valid"); else throw new RuntimeException("CiNii OpenSearch call failed: " + method.getStatusLine()); } try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setIgnoringComments(true); factory.setIgnoringElementContentWhitespace(true); DocumentBuilder db = factory.newDocumentBuilder(); Document inDoc = db.parse(method.getResponseBodyAsStream()); Element xmlRoot = inDoc.getDocumentElement(); List<Element> items = XMLUtils.getElementList(xmlRoot, "item"); int url_len = "http://ci.nii.ac.jp/naid/".length(); for (Element item : items) { String about = item.getAttribute("rdf:about"); if (about.length() > url_len) { ids.add(about.substring(url_len)); } } return ids; } catch (Exception e) { throw new RuntimeException("CiNii OpenSearch results is not valid or not exist"); } } finally { if (method != null) { method.releaseConnection(); } } }
From source file:org.dspace.submit.lookup.CrossRefFileDataLoader.java
@Override public RecordSet getRecords() throws MalformedSourceException { RecordSet recordSet = new RecordSet(); try {/* ww w.j a v a2 s. c om*/ InputStream inputStream = new FileInputStream(new File(filename)); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setIgnoringComments(true); factory.setIgnoringElementContentWhitespace(true); DocumentBuilder db = factory.newDocumentBuilder(); Document inDoc = db.parse(inputStream); Element xmlRoot = inDoc.getDocumentElement(); Element queryResult = XMLUtils.getSingleElement(xmlRoot, "query_result"); Element body = XMLUtils.getSingleElement(queryResult, "body"); Element dataRoot = XMLUtils.getSingleElement(body, "query"); Record record = CrossRefUtils.convertCrossRefDomToRecord(dataRoot); recordSet.addRecord(convertFields(record)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return recordSet; }
From source file:org.dspace.submit.lookup.PubmedFileDataLoader.java
@Override public RecordSet getRecords() throws MalformedSourceException { RecordSet recordSet = new RecordSet(); try {//from w w w. j a va2s. c o m InputStream inputStream = new FileInputStream(new File(filename)); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setIgnoringComments(true); factory.setIgnoringElementContentWhitespace(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document inDoc = builder.parse(inputStream); Element xmlRoot = inDoc.getDocumentElement(); List<Element> pubArticles = XMLUtils.getElementList(xmlRoot, "PubmedArticle"); for (Element xmlArticle : pubArticles) { Record record = null; try { record = PubmedUtils.convertPubmedDomToRecord(xmlArticle); recordSet.addRecord(convertFields(record)); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return recordSet; }