org.jbibtex.ParseException Java Examples

The following examples show how to use org.jbibtex.ParseException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MendeleyClient.java    From slr-toolkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * This methods is used to obtain all documents via the GET https://api.mendeley.com/documents endpoint.
 * 
 * @return This returns a JSON String of all documents from the /documents endpoint
 */
public String getAllDocumentsJSON() throws MalformedURLException, IOException, TokenMgrException, ParseException{
	refreshTokenIfNecessary();
	
	String resource_url = "https://api.mendeley.com/documents?&view=bib&limit=500";
	
	HttpURLConnection resource_cxn = (HttpURLConnection)(new URL(resource_url).openConnection());
    resource_cxn.addRequestProperty("Authorization", "Bearer " + this.access_token);
    resource_cxn.setRequestMethod("GET");
    
    InputStream resource = resource_cxn.getInputStream();
	
    BufferedReader r = new BufferedReader(new InputStreamReader(resource, "UTF-8"));
    String line = null;
    String json_str = "";
    while ((line = r.readLine()) != null) {
    	json_str = json_str + line;
    }
    
	return json_str;
}
 
Example #2
Source File: MendeleyClient.java    From slr-toolkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * This methods is used to obtain all Folder from a Mendeley profile via the GET https://api.mendeley.com/folders endpoint.
 * The response model of this endpoint only includes name, creation date and id of a folder.
 * 
 * @return This Method returns a JSON String of all Mendeley Folders
 * @throws MalformedURLException
 * @throws IOException
 * @throws TokenMgrException
 * @throws ParseException
 */
public String getAllFolders() throws MalformedURLException, IOException, TokenMgrException, ParseException{
	refreshTokenIfNecessary();
	
	String resource_url = "https://api.mendeley.com/folders";
	
	HttpURLConnection resource_cxn = (HttpURLConnection)(new URL(resource_url).openConnection());
    resource_cxn.addRequestProperty("Authorization", "Bearer " + this.access_token);
    resource_cxn.setRequestMethod("GET");
    
    InputStream resource = resource_cxn.getInputStream();
	
    BufferedReader r = new BufferedReader(new InputStreamReader(resource, "UTF-8"));
    String line = null;
    String json_str = "";
    while ((line = r.readLine()) != null) {
    	json_str = json_str + line;
    }
    
	return json_str;
}
 
Example #3
Source File: BibTeXConverter.java    From citeproc-java with Apache License 2.0 6 votes vote down vote up
/**
 * <p>Loads a BibTeX database from a stream.</p>
 * <p>This method does not close the given stream. The caller is
 * responsible for closing it.</p>
 * @param is the input stream to read from
 * @return the BibTeX database
 * @throws ParseException if the database is invalid
 */
public BibTeXDatabase loadDatabase(InputStream is) throws ParseException {
    Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8);
    BibTeXParser parser = new BibTeXParser() {
        @Override
        public void checkStringResolution(Key key, BibTeXString string) {
            // ignore
        }
    };
    try {
        return parser.parse(reader);
    } catch (TokenMgrException err) {
        throw new ParseException("Could not parse BibTeX library: " +
                err.getMessage());
    }
}
 
Example #4
Source File: MendeleyClient.java    From slr-toolkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * This methods is used to obtain all Mendeley documents that are stored in a specific Folder
 * via the GET https://api.mendeley.com/documents endpoint.
 * 
 * @param id Pass the ID of a Mendeley Folder to get its documents
 * @return This Method returns a JSON String of all documents stored in a specific Mendeley Folder
 * @throws MalformedURLException
 * @throws IOException
 * @throws TokenMgrException
 * @throws ParseException
 */
public String getDocumentsByFolderJSON(String id) throws MalformedURLException, IOException, TokenMgrException, ParseException{
	refreshTokenIfNecessary();
	
	String resource_url = "https://api.mendeley.com/documents?folder_id=" + id + "&view=bib&limit=500";
	
	HttpURLConnection resource_cxn = (HttpURLConnection)(new URL(resource_url).openConnection());
    resource_cxn.addRequestProperty("Authorization", "Bearer " + this.access_token);
    resource_cxn.setRequestMethod("GET");
    InputStream resource = resource_cxn.getInputStream();
    
    BufferedReader r = new BufferedReader(new InputStreamReader(resource, "UTF-8"));
    String line = null;
    String json_str = "";
    while ((line = r.readLine()) != null) {
    	json_str = json_str + line;
    }
    
	return json_str;
}
 
Example #5
Source File: BibtexResourceImpl.java    From slr-toolkit with Eclipse Public License 1.0 5 votes vote down vote up
private String parseLaTeX(String latexString) {
	String plainString = "";
	try {
		LaTeXParser parser = new LaTeXParser();
		List<LaTeXObject> latexObjects = parser.parse(latexString);
		LaTeXPrinter printer = new LaTeXPrinter();
		plainString = printer.print(latexObjects);
	} catch (TokenMgrException | ParseException e) {
		System.out.println(e.getMessage());
		return latexString;
	}
	return plainString;
}
 
Example #6
Source File: BibTeXConverterTest.java    From citeproc-java with Apache License 2.0 5 votes vote down vote up
/**
 * Test if the field {@code genre} of the CSL entry is set for theses.
 */
@Test
public void shouldSetGenreFromTypeForTheses() throws IOException, ParseException {
    BibTeXDatabase db = loadUnixDatabase();
    BibTeXEntry e = db.resolveEntry(new Key("Wang:2002:DIR"));
    BibTeXConverter conv = new BibTeXConverter();
    CSLItemData cid = conv.toItemData(e);
    assertEquals("CSL 'type' not set", CSLType.THESIS, cid.getType());
    assertEquals("CSL 'genre' not set", "Thesis (Ph.D.)", cid.getGenre());
}
 
Example #7
Source File: BibTeXConverterTest.java    From citeproc-java with Apache License 2.0 5 votes vote down vote up
/**
 * Test if the field {@code language} of BibTeX entry is read.
 */
@Test
public void shouldReadTheLanguageFieldOfASingleEntry() throws IOException, ParseException {
    BibTeXDatabase db = loadUnixDatabase();
    BibTeXEntry e = db.resolveEntry(new Key("Bach:1986:UTS"));
    BibTeXConverter conv = new BibTeXConverter();
    CSLItemData cid = conv.toItemData(e);
    assertEquals("Bach:1986:UTS", cid.getId());
    assertEquals("German", cid.getLanguage());
}
 
Example #8
Source File: AbstractBibTeXTest.java    From citeproc-java with Apache License 2.0 5 votes vote down vote up
/**
 * Loads the <code>unix.bib</code> database from the classpath
 * @return the database
 * @throws IOException if the database could not be loaded
 * @throws ParseException if the database is invalid
 */
protected static BibTeXDatabase loadUnixDatabase() throws IOException, ParseException {
    BibTeXDatabase db;
    try (InputStream is = AbstractBibTeXTest.class.getResourceAsStream("/unix.bib.gz")) {
        GZIPInputStream gis = new GZIPInputStream(is);
        db = new BibTeXConverter().loadDatabase(gis);
    }
    return db;
}
 
Example #9
Source File: BibTeXConverter.java    From citeproc-java with Apache License 2.0 5 votes vote down vote up
/**
 * Default constructor
 */
public BibTeXConverter() {
    try {
        latexParser = new LaTeXParser();
    } catch (ParseException e) {
        // can actually never happen because the default constructor
        // of LaTeXParser doesn't throw
        throw new RuntimeException(e);
    }
    latexPrinter = new LaTeXPrinter();
}
 
Example #10
Source File: MendeleyClient.java    From slr-toolkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * This Methods uses the refresh Token to retrieve a renewed access token
 * 
 * @param code Refresh Token
 * @return This returns if the request was successful.
 * @throws IOException
 * @throws TokenMgrException
 * @throws ParseException
 */
public boolean requestRefreshAccessToken(String code) throws IOException, TokenMgrException, ParseException {
 try {
   RefreshTokenRequest request =
       new RefreshTokenRequest(new NetHttpTransport(), new JacksonFactory(),
           new GenericUrl("https://api.mendeley.com/oauth/token"),code)
       	  .setRefreshToken(code)
       	  .set("redirect_uri", "https://localhost")
           .setGrantType("refresh_token")
           .setClientAuthentication(
               new BasicAuthentication("4335", "sSFcbUA38RS9Cpm7"));
   
   TokenResponse response = request.execute();
   
   this.access_token = response.getAccessToken();
   this.refresh_token = response.getRefreshToken();
   this.expires_at = this.generateExpiresAtFromExpiresIn(response.getExpiresInSeconds().intValue());
   
   updatePreferenceStore();
   refreshTokenIfNecessary();
   
   return true;
 } catch (TokenResponseException e) {
   if (e.getDetails() != null) {
     System.err.println("Error: " + e.getDetails().getError());
     if (e.getDetails().getErrorDescription() != null) {
       System.err.println(e.getDetails().getErrorDescription());
     }
     if (e.getDetails().getErrorUri() != null) {
       System.err.println(e.getDetails().getErrorUri());
     }
   } else {
     System.err.println(e.getMessage());
   }
   return false;
 }
}
 
Example #11
Source File: MendeleyClient.java    From slr-toolkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * This Method exchanges the authorization code for an access token. 
 * If successful the Tokens and Expiration Date will be stored.
 * 
 * @param code The authorization code from the user interface response has to be passed
 * @throws IOException
 * @throws TokenMgrException
 * @throws ParseException
 */
public void requestAccessToken(String code) throws IOException, TokenMgrException, ParseException {
 try {
   TokenResponse response =
       new AuthorizationCodeTokenRequest(new NetHttpTransport(), new JacksonFactory(),
           new GenericUrl("https://api.mendeley.com/oauth/token"),code)
           .setRedirectUri("https://localhost")
           .setGrantType("authorization_code")
           .setClientAuthentication(
               new BasicAuthentication("4335", "sSFcbUA38RS9Cpm7")).execute();
   
   this.access_token = response.getAccessToken();
   this.refresh_token = response.getRefreshToken();
   this.expires_at = this.generateExpiresAtFromExpiresIn(response.getExpiresInSeconds().intValue());
   
   updatePreferenceStore();
   refreshTokenIfNecessary();
 } catch (TokenResponseException e) {
   if (e.getDetails() != null) {
     System.err.println("Error: " + e.getDetails().getError());
     if (e.getDetails().getErrorDescription() != null) {
       System.err.println(e.getDetails().getErrorDescription());
     }
     if (e.getDetails().getErrorUri() != null) {
       System.err.println(e.getDetails().getErrorUri());
     }
   } else {
     System.err.println(e.getMessage());
   }
 }
}
 
Example #12
Source File: MendeleyClient.java    From slr-toolkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
   * This methods is used to obtain all BibTeXEntries of a Mendeley Document via the GET https://api.mendeley.com/documents/{id} endpoint.
   * 
   * @param id Document ID tha is stored in Mendeley Folder.
   * @return This Method returns a BibTeXDatabase of the given document id
   * @throws MalformedURLException
   * @throws IOException
   * @throws TokenMgrException
   * @throws ParseException
   */
  public BibTeXDatabase getDocumentBibTex(String id) throws MalformedURLException, IOException, TokenMgrException, ParseException{
  	refreshTokenIfNecessary();
  	
  	String resource_url = "https://api.mendeley.com/documents/" + id + "?view=bib&limit=500";
  	
  	HttpURLConnection resource_cxn = (HttpURLConnection)(new URL(resource_url).openConnection());
      resource_cxn.addRequestProperty("Authorization", "Bearer " + this.access_token);
      resource_cxn.setRequestMethod("GET");
      resource_cxn.setRequestProperty("Accept","application/x-bibtex");
      
      InputStream resource = resource_cxn.getInputStream();
  	
      BufferedReader r = new BufferedReader(new InputStreamReader(resource, "UTF-8"));
      BibTeXDatabase db = new BibTeXDatabase();
  	try(Reader reader = r){
  		CharacterFilterReader filterReader = new CharacterFilterReader(reader);
  		BibTeXParser parser = new BibTeXParser();
  		db = parser.parse(filterReader);
  		
  		/*
  		 * Mendeley API returns a parsing error concerning the 'title' field
  		 * 	- 	The additional characters '{' at the beginning of a Title and '}' at the end of a title
  		 * 		must be removed
  		 */
  		for(BibTeXEntry entry: db.getEntries().values()){
  			String fixedTitleStr = getFixedString(entry.getField(new Key("title")).toUserString());
  			StringValue fixedTitleValue = new StringValue(fixedTitleStr, StringValue.Style.BRACED);
  			entry.removeField(new Key("title"));
  			entry.addField(new Key("title"), fixedTitleValue);
  		}
  	}catch (TokenMgrException | IOException |ParseException e) {
	e.printStackTrace();
}
 
  	return db;
  }
 
Example #13
Source File: MCRItemDataProvider.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
public void addBibTeX(MCRContent bibTeX) throws IOException {
    InputStream in = bibTeX.getInputStream();
    BibTeXDatabase db;
    try {
        db = new BibTeXConverter().loadDatabase(in);
    } catch (ParseException ex) {
        throw new IOException(ex);
    }
    in.close();

    wrappedProvider.addDatabase(db);
}
 
Example #14
Source File: MendeleyClient.java    From slr-toolkit with Eclipse Public License 1.0 4 votes vote down vote up
/**
   * This methods is used to obtain a BibTeXDatabase containing all documents of a specific Mendeley Folder
   * via the GET https://api.mendeley.com/documents endpoint.
   * 
   * @param mf You can pass a specific MendeleyFolder object to get its BibTeXDatabase
   * @return This Method returns a BibTeXDatabase 
   * @throws MalformedURLException
   * @throws IOException
   * @throws TokenMgrException
   * @throws ParseException
   */
  public BibTeXDatabase getDocumentsByFolderBibTex(MendeleyFolder mf) throws MalformedURLException, IOException, TokenMgrException, ParseException{
  	refreshTokenIfNecessary();
  	
  	String resource_url = "https://api.mendeley.com/documents?folder_id=" + mf.getId() + "&view=bib&limit=500";
  	
  	HttpURLConnection resource_cxn = (HttpURLConnection)(new URL(resource_url).openConnection());
      resource_cxn.addRequestProperty("Authorization", "Bearer " + this.access_token);
      resource_cxn.setRequestMethod("GET");
      resource_cxn.setRequestProperty("Accept","application/x-bibtex");
      InputStream resource = resource_cxn.getInputStream();
          	
      BufferedReader r = new BufferedReader(new InputStreamReader(resource, "UTF-8"));

      BibTeXDatabase db = new BibTeXDatabase();
  	try(Reader reader = r){
  		CharacterFilterReader filterReader = new CharacterFilterReader(reader);
  		BibTeXParser parser = new BibTeXParser();
  		db = parser.parse(filterReader);
  		
  		/*
  		 * Mendeley API returns a parsing error concerning the 'title' field
  		 * 	- 	The additional characters '{' at the beginning of a Title and '}' at the end of a title
  		 * 		must be removed
  		 */
  		for(BibTeXEntry entry: db.getEntries().values()){
  			String fixedTitleStr = getFixedString(entry.getField(new Key("title")).toUserString());
  			StringValue fixedTitleValue = new StringValue(fixedTitleStr, StringValue.Style.BRACED);
  			entry.removeField(new Key("title"));
  			entry.addField(new Key("title"), fixedTitleValue);
  			
  			Value value = entry.getField(new Key("title"));
  			if(value != null){
  				MendeleyDocument document = mf.getDocumentByTitle(value.toUserString());
  				if(document != null){
  					// get the Note containing the additional classes field
  					String notes = document.getNotes();
  					if(notes != null){
  						// Specify regex pattern for classes
  						Pattern pattern = Pattern.compile("\\[classes\\](.*?)\\[/classes\\]");
  						Matcher matcher = pattern.matcher(notes);
  						String classes_str = "";
  						
  						// Extract only classes string inside of [classes] tags
  						if(matcher.find()){
  							classes_str = matcher.group(1);
  							StringValue classes = new StringValue(classes_str, StringValue.Style.BRACED);
  							
  							// Add classes to BibTeXDatabase
  							entry.addField(new Key("classes"), classes);
  						}
  					}
  				}
  			}
  		}
  		
  	}catch (TokenMgrException | IOException |ParseException e) {
	e.printStackTrace();
}
  	return db;
  }
 
Example #15
Source File: MendeleyClient.java    From slr-toolkit with Eclipse Public License 1.0 4 votes vote down vote up
/**
   * This methods is used to obtain a Mendeley Folder from a specific Folder via the GET https://api.mendeley.com/documents endpoint.
   * All Mendeley Documents included in this folder and the respective BibTeXDatabase will be included.
   * 
   * @param folderId The ID of a Folder that is stored in your Mendeley profile
   * @return This method returns a Mendeley Folder containing all respective documents
   * @throws MalformedURLException
   * @throws TokenMgrException
   * @throws IOException
   * @throws ParseException
   */
  public MendeleyFolder getMendeleyFolder(String folderId) throws MalformedURLException, TokenMgrException, IOException, ParseException{
  	refreshTokenIfNecessary();
  	
  	// The 'folder_id' parameter is used to specify the folder you want to retrieve.
  	String resource_url = "https://api.mendeley.com/documents?folder_id=" + folderId + "&view=bib&limit=500";
  	
  	HttpURLConnection resource_cxn = (HttpURLConnection)(new URL(resource_url).openConnection());
      resource_cxn.addRequestProperty("Authorization", "Bearer " + this.access_token);
      resource_cxn.setRequestMethod("GET");
      InputStream resource = resource_cxn.getInputStream();
      
      BufferedReader r = new BufferedReader(new InputStreamReader(resource, "UTF-8"));
      String line = null;
      String json_str = "";
      while ((line = r.readLine()) != null) {
      	json_str = json_str + line;
      }
      
      Gson gson = new Gson();
      MendeleyDocument[] documents = gson.fromJson(json_str, MendeleyDocument[].class);
      
   MendeleyFolder m_folder = new MendeleyFolder();
   m_folder.setId(folderId);
   m_folder.setDocuments(documents);
   
      try {
	String folder_content_str = this.getDocumentsByFolderJSON(m_folder.getId());
	
	MendeleyDocument[] folder_content = gson.fromJson(folder_content_str, MendeleyDocument[].class);
	m_folder.setDocuments(folder_content);
	m_folder.setType("Folder");
	
	for(MendeleyDocument md : m_folder.getDocuments()){
		String notes = md.getNotes();
		MendeleyAnnotation[] annotations = getAnnotations(md);
		for(MendeleyAnnotation annotation: annotations){
			if(annotation.getType().equals("note")){
				notes = annotation.getText();
			}
		}
		md.setNotes(notes);
	}
	BibTeXDatabase bibdb = this.getDocumentsByFolderBibTex(m_folder);
	m_folder.setBibtexDatabase(bibdb);
	
} catch (TokenMgrException | IOException | ParseException e) {
	e.printStackTrace();
}
      return m_folder;
      
  }
 
Example #16
Source File: MendeleyClient.java    From slr-toolkit with Eclipse Public License 1.0 4 votes vote down vote up
/**
   * This methods is used to update the Mendeley Folder that are stored in this client class 
   * by obtaining all Folders of a Mendeley profile including the respective Mendeley Documents.
   * The updated Mendeley Folders are a requirement for starting the Mendeley Sync Wizard
   * 
   * @param monitor 
   * @return Returns the IStatus of the Job that is used to inform the User about the current state
   * @throws MalformedURLException
   * @throws TokenMgrException
   * @throws IOException
   * @throws ParseException
   */
  public IStatus updateMendeleyFolders(IProgressMonitor monitor) throws MalformedURLException, TokenMgrException, IOException, ParseException{
  	refreshTokenIfNecessary();
  	
  	// Get all Folder IDs and Folder Names of your Mendeley profile
      String folders_str = this.getAllFolders();
      Gson gson = new Gson();
      
      // Parse JSON String of Folders into MendeleyFolder objects
      MendeleyFolder[] m_folders = gson.fromJson(folders_str, MendeleyFolder[].class);
      
      // Number of tasks equals number of Folder that are present in your Mendeley profile
      SubMonitor subMonitor = SubMonitor.convert(monitor, m_folders.length);

      for(int i = 0; i < m_folders.length;i++){
	subMonitor.setTaskName("Working on Folder " + String.valueOf(i+1) + " of " + String.valueOf(m_folders.length) +  ": " + m_folders[i].getName() );
	
	String folder_content_str;
	try {
		// get all documents of a specific folder
		folder_content_str = this.getDocumentsByFolderJSON(m_folders[i].getId());
		
		// get all BibTeXEntries of a specific folder
		BibTeXDatabase bibdb = this.getDocumentsByFolderBibTex(m_folders[i]);
		m_folders[i].setBibtexDatabase(bibdb);
		
		MendeleyDocument[] folder_content = gson.fromJson(folder_content_str, MendeleyDocument[].class);
		m_folders[i].setDocuments(folder_content);
		m_folders[i].setType("Folder");
		
		// get the notes of all Documents to find the 'classes' field that is missing in the Mendeley BibTeX response model
		for(MendeleyDocument md : m_folders[i].getDocuments()){
			subMonitor.subTask("Download Document \"" + md.getTitle() + "\"");
			String notes = md.getNotes();
			MendeleyAnnotation[] annotations = getAnnotations(md);
			for(MendeleyAnnotation annotation: annotations){
				if(annotation.getType().equals("note")){
					notes = annotation.getText();
				}
			}
			// add notes to the 'notes' variable
			md.setNotes(notes);
		}
	} catch (TokenMgrException | IOException | ParseException e) {
		e.printStackTrace();
	}
	subMonitor.worked(1);
	if (subMonitor.isCanceled()) return Status.CANCEL_STATUS;
      }
      mendeleyFolders = m_folders;
return Status.OK_STATUS;
  }
 
Example #17
Source File: Converter.java    From citeproc-java with Apache License 2.0 votes vote down vote up
Library loadLibrary(InputStream is) throws IOException, ParseException {
    Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8);
    $name parser = new $name();
    return parser.parse(reader);
}