package com.github.tkurz.media.fragments;

import java.io.StringReader;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * Represents a media fragment uri (like specified in http://www.w3.org/TR/media-frags/)
 * <p/>
 * Author: Thomas Kurz (tkurz@apache.org)
 */
public class MediaFragmentURI {

    private URI uri;
    private MediaFragment mediaFragment;

    /**
     * Creates a media fragment from string. If no fragment is specified, an empty media fragment is created
     * @param uri_string a uri (may have media fragments)
     * @throws MediaFragmentURISyntaxException
     */
    public MediaFragmentURI(String uri_string) throws MediaFragmentURISyntaxException {
        try {
            this.uri = new URI(uri_string);
            if(uri.getFragment() != null) {
                FragmentParser p1 = new FragmentParser(new StringReader(uri.getFragment()));
                mediaFragment = p1.run();
            } else {
                mediaFragment = new MediaFragment();
            }
        } catch (URISyntaxException | ParseException e) {
            throw new MediaFragmentURISyntaxException(e);
        }
    }

    /**
     * returns the media fragment
     * @return a media fragment
     */
    public MediaFragment getMediaFragment() {
        return mediaFragment;
    }

    public String toString() {
        StringBuilder b = new StringBuilder();
        b.append(uri.getScheme());
        b.append("://");
        if(uri.getAuthority() != null) b.append(uri.getAuthority());
        if(uri.getPort() > -1) {
            b.append(":");
            b.append(uri.getPort());
        }
        b.append(uri.getPath());
        if(mediaFragment != null) b.append(mediaFragment.toString());
        return b.toString();
    }
}

@@ -1150,85 +1150,151 @@
     
-/**%0A
+public String getScheme() %7B%0A 
      
-*
+ 
  return
-s t
+ uri.getSc
 he
- 
 me
-dia fragment
+();%0A    %7D%0A%0A    public String getAuthority() %7B
 %0A     
-* @
+   
 return 
-a media fragment
+uri.getAuthority();
 %0A    
- */
+%7D%0A
 %0A   
@@ -1305,38 +1305,22 @@
 lic 
-MediaFragment getMediaFragment
+String getPath
 () %7B
@@ -1335,29 +1335,29 @@
  return 
-mediaFragment
+uri.getPath()
 ;%0A    %7D%0A
@@ -1375,24 +1375,31 @@
  String 
-toString
+getAbsolutePath
 () %7B%0A   
@@ -1502,34 +1502,40 @@
 %22://%22);%0A        
-if
+b.append
 (uri.getAuthorit
@@ -1537,25 +1537,26 @@
 hority()
- != null)
+);%0A       
  b.appen
@@ -1560,33 +1560,28 @@
 pend(uri.get
-Authority
+Path
 ());%0A       
@@ -1585,96 +1585,163 @@
     
-if(uri.getPort() %3E -1) %7B
+return b.toString();%0A    %7D%0A
 %0A    
+/**%0A
      
-   b.append(%22:%22);%0A            b.append(uri.getPort());
+* returns the media fragment%0A     * @return a media fragment%0A     */%0A    public MediaFragment getMediaFragment() %7B
 %0A   
@@ -1749,42 +1749,67 @@
     
-%7D
+return mediaFragment;
 %0A    
+%7D%0A%0A
     
-b.append(uri.getPath());
+public String toString() %7B
 %0A   
@@ -1813,19 +1813,23 @@
         
-if(
+return 
 mediaFra
@@ -1845,19 +1845,31 @@
 null
-) b.append(
+ ? getAbsolutePath() + 
 medi
@@ -1892,36 +1892,26 @@
 ng()
-);%0A        return b.toString
+ : getAbsolutePath
 ();%0A

add some uri-specific functions to media URI