- Timestamp:
- 2014-10-27T23:53:47+01:00 (10 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/io
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/NoteReader.java
r7474 r7663 2 2 package org.openstreetmap.josm.io; 3 3 4 import java.io.ByteArrayInputStream; 4 5 import java.io.IOException; 5 6 import java.io.InputStream; 7 import java.nio.charset.StandardCharsets; 6 8 import java.text.ParseException; 7 9 import java.text.SimpleDateFormat; … … 218 220 219 221 /** 222 * Initializes the reader with a string as a source 223 * @param source UTF-8 string containing Notes XML to parse 224 * @throws IOException 225 */ 226 public NoteReader(String source) throws IOException { 227 this.inputSource = new InputSource(new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8))); 228 } 229 230 /** 220 231 * Parses the InputStream given to the constructor and returns 221 232 * the resulting Note objects -
trunk/src/org/openstreetmap/josm/io/OsmApi.java
r7656 r7663 15 15 import java.io.StringReader; 16 16 import java.io.StringWriter; 17 import java.io.UnsupportedEncodingException; 17 18 import java.net.ConnectException; 18 19 import java.net.HttpURLConnection; … … 20 21 import java.net.SocketTimeoutException; 21 22 import java.net.URL; 23 import java.net.URLEncoder; 22 24 import java.nio.charset.StandardCharsets; 23 25 import java.util.Collection; 24 26 import java.util.Collections; 25 27 import java.util.HashMap; 28 import java.util.List; 26 29 import java.util.Map; 27 30 … … 29 32 30 33 import org.openstreetmap.josm.Main; 34 import org.openstreetmap.josm.data.coor.LatLon; 35 import org.openstreetmap.josm.data.notes.Note; 31 36 import org.openstreetmap.josm.data.osm.Changeset; 32 37 import org.openstreetmap.josm.data.osm.IPrimitive; … … 782 787 this.changeset = changeset; 783 788 } 789 790 /** 791 * Create a new note on the server 792 * @param latlon Location of note 793 * @param text Comment entered by user to open the note 794 * @param monitor Progress monitor 795 * @return Note as it exists on the server after creation (ID assigned) 796 * @throws OsmTransferException 797 */ 798 public Note createNote(LatLon latlon, String text, ProgressMonitor monitor) throws OsmTransferException { 799 initialize(monitor); 800 String url = new StringBuilder() 801 .append("notes?lat=") 802 .append(latlon.lat()) 803 .append("&lon=") 804 .append(latlon.lon()) 805 .append("&text=") 806 .append(urlEncode(text)).toString(); 807 808 String response = sendRequest("POST", url, null, monitor, true, false); 809 return parseSingleNote(response); 810 } 811 812 /** 813 * Add a comment to an existing note. 814 * @param note The note to add a comment to 815 * @param comment Text of the comment 816 * @param monitor Progress monitor 817 * @return Note returned by the API after the comment was added 818 * @throws OsmTransferException 819 */ 820 public Note addCommentToNote(Note note, String comment, ProgressMonitor monitor) throws OsmTransferException { 821 initialize(monitor); 822 String url = new StringBuilder() 823 .append("notes/") 824 .append(note.getId()) 825 .append("/comment?text=") 826 .append(urlEncode(comment)).toString(); 827 828 String response = sendRequest("POST", url, null, monitor, true, false); 829 return parseSingleNote(response); 830 } 831 832 /** 833 * Close a note 834 * @param note Note to close. Must currently be open 835 * @param closeMessage Optional message supplied by the user when closing the note 836 * @param monitor Progress monitor 837 * @return Note returned by the API after the close operation 838 * @throws OsmTransferException 839 */ 840 public Note closeNote(Note note, String closeMessage, ProgressMonitor monitor) throws OsmTransferException { 841 initialize(monitor); 842 String encodedMessage = urlEncode(closeMessage); 843 StringBuilder urlBuilder = new StringBuilder() 844 .append("notes/") 845 .append(note.getId()) 846 .append("/close"); 847 if (encodedMessage != null && !encodedMessage.trim().isEmpty()) { 848 urlBuilder.append("?text="); 849 urlBuilder.append(encodedMessage); 850 } 851 852 String response = sendRequest("POST", urlBuilder.toString(), null, monitor, true, false); 853 return parseSingleNote(response); 854 } 855 856 /** 857 * Reopen a closed note 858 * @param note Note to reopen. Must currently be closed 859 * @param reactivateMessage Optional message supplied by the user when reopening the note 860 * @param monitor Progress monitor 861 * @return Note returned by the API after the reopen operation 862 * @throws OsmTransferException 863 */ 864 public Note reopenNote(Note note, String reactivateMessage, ProgressMonitor monitor) throws OsmTransferException { 865 initialize(monitor); 866 String encodedMessage = urlEncode(reactivateMessage); 867 StringBuilder urlBuilder = new StringBuilder() 868 .append("notes/") 869 .append(note.getId()) 870 .append("/reopen"); 871 if (encodedMessage != null && !encodedMessage.trim().isEmpty()) { 872 urlBuilder.append("?text="); 873 urlBuilder.append(encodedMessage); 874 } 875 876 String response = sendRequest("POST", urlBuilder.toString(), null, monitor, true, false); 877 return parseSingleNote(response); 878 } 879 880 /** Method for parsing API responses for operations on individual notes */ 881 private Note parseSingleNote(String xml) throws OsmTransferException { 882 try { 883 List<Note> newNotes = new NoteReader(xml).parse(); 884 if(newNotes.size() == 1) { 885 return newNotes.get(0); 886 } 887 //Shouldn't ever execute. Server will either respond with an error (caught elsewhere) or one note 888 throw new OsmTransferException(tr("Note upload failed")); 889 } catch (SAXException|IOException e) { 890 Main.error(e, true); 891 throw new OsmTransferException(tr("Error parsing note response from server"), e); 892 } 893 } 894 895 /** URL encodes a string. Useful for transforming user input into URL query strings*/ 896 private String urlEncode(String string) throws OsmTransferException { 897 try { 898 return URLEncoder.encode(string, "UTF-8"); 899 } catch (UnsupportedEncodingException e) { 900 Main.error(e, true); 901 throw new OsmTransferException(tr("Error encoding string: {0}", string), e); 902 } 903 } 784 904 }
Note:
See TracChangeset
for help on using the changeset viewer.