/** * From Lucene Search Engine. * code found at http://www.koders.com, decodeEntities() added. */ package org.das2.util; import java.awt.BorderLayout; import java.awt.Desktop; import java.awt.Font; import java.awt.event.ActionEvent; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.ListSelectionModel; /** * Encoder and decoder for HTML entities, like &rho; for ρ. * See https://shapecatcher.com/ to identify shapes and * also https://detexify.kirelabs.org/classify.html for identifying * LaTeX characters (which are not supported here). * * @author jbf */ public class Entities { /** * return true of the font supports each character of the string. * @param f * @param s * @return */ public static boolean fontSupports( Font f, String s ) { for ( char c: s.toCharArray() ) { if ( !f.canDisplay(c) ) { return false; } } return true; } /** * decoder goes from &rho; to "ρ" */ static final HashMap decoder = new HashMap(300); static final String[] encoder = new String[0x8000]; /** * utility method for decoding entities like &rho; into UNICODE. * Malformed entities (like B1; instead of α) are formatted as "???" * @param str string e.g. "&rho; degrees" * @return string with Unicode characters for entities. */ public static String decodeEntities(String str) { int i0=0, i; int MAX_ENTITY_LEN=10; StringBuilder result= new StringBuilder(); while ( true ) { i= str.indexOf("&",i0); if ( i==-1 ) { result.append( str.substring(i0) ); return result.toString(); } else { int i1= str.indexOf(";",i); if ( i1!=-1 && i1-i items= new ArrayList<>(); for ( Entry e: decoder.entrySet() ) { items.add( new String[] { e.getValue(), e.getKey()+";" } ); } rowData= items.toArray( new String[items.size()][] ); JTable t= new JTable(rowData,columns); t.setFont( Font.decode("sans-14") ); t.setSelectionMode( ListSelectionModel.SINGLE_SELECTION ); JScrollPane p= new JScrollPane(t); // also https://www.reilldesign.com/tutorials/character-entity-reference-chart.html JButton l= new JButton( new AbstractAction("See also https://www.freeformatter.com/html-entities.html") { @Override public void actionPerformed(ActionEvent e) { String target= "https://www.freeformatter.com/html-entities.html"; try { Desktop.getDesktop().browse( new URI(target) ); } catch (URISyntaxException | IOException ex) { } } }); JPanel panel= new JPanel(); panel.setLayout(new BorderLayout()); panel.add(l,BorderLayout.NORTH); panel.add(p); if ( JOptionPane.showConfirmDialog( null, panel, "HTML Entities", JOptionPane.OK_CANCEL_OPTION )==JOptionPane.OK_OPTION ) { if ( t.getSelectedRow()==-1 ) { return ""; } else { return (String)rowData[t.getSelectedRow()][1]; } } else { return ""; } } public static void main( String[] args ) { System.err.println("pick: " + pickEntityGUI() ); } }