You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by mr...@apache.org on 2004/01/21 18:03:14 UTC

cvs commit: xml-xerces/java/src/org/apache/xerces/xinclude XIncludeTextReader.java XIncludeHandler.java

mrglavas    2004/01/21 09:03:14

  Modified:    java/src/org/apache/xerces/xinclude XIncludeTextReader.java
                        XIncludeHandler.java
  Log:
  Redistributing code for content negotation.
  
  We were reading the URLConnection to early.  We cannot set 
  those parameters in XIncludeHandler because we may lose some
  information if we process the URLConnection and then open a stream.  
  
  The parser needs a URLConnection to follow HTTP redirects and may 
  also need this object in the future to read external 
  encoding information which may be available in the Content-Type.
  We may also need to read the URLConnection to determine
  the encoding of a text include.
  
  Also fixed a bug involving the case of the attributes on the include
  element. Accept-Language -> accept-language.
  
  REVISIT: If we're going to support content negotation for
  parse=xml we need to find a clean way to propogate the
  parameters to the child parser.
  
  Revision  Changes    Path
  1.6       +40 -5     xml-xerces/java/src/org/apache/xerces/xinclude/XIncludeTextReader.java
  
  Index: XIncludeTextReader.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/xinclude/XIncludeTextReader.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- XIncludeTextReader.java	17 Nov 2003 04:49:18 -0000	1.5
  +++ XIncludeTextReader.java	21 Jan 2004 17:03:14 -0000	1.6
  @@ -61,6 +61,7 @@
   import java.io.InputStream;
   import java.io.InputStreamReader;
   import java.io.Reader;
  +import java.net.HttpURLConnection;
   import java.net.URL;
   import java.net.URLConnection;
   import java.util.Locale;
  @@ -102,7 +103,12 @@
       private XIncludeHandler fHandler;
       private XMLInputSource fSource;
       private XMLErrorReporter fErrorReporter;
  -
  +    
  +    // Content negotation parameters
  +    private String fAccept;
  +    private String fAcceptCharset;
  +    private String fAcceptLanguage;
  + 
       /**
        * Construct the XIncludeReader using the XMLInputSource and XIncludeHandler.
        *
  @@ -125,6 +131,20 @@
       public void setErrorReporter(XMLErrorReporter errorReporter) {
           fErrorReporter = errorReporter;
       }
  +    
  +    /**
  +     * Sets content negotation parameters to 
  +     * be attached to an HTTP request.
  +     * 
  +     * @param accept the Accept HTTP request property
  +     * @param acceptCharset the Accept-Charset HTTP request property
  +     * @param acceptLanguage the Accept-Language HTTP request property
  +     */
  +    public void setHttpProperties(String accept, String acceptCharset, String acceptLanguage) {
  +        fAccept = accept;
  +        fAcceptCharset = acceptCharset;
  +        fAcceptLanguage = acceptLanguage;
  +    }
   
       /**
        * Return the Reader for given XMLInputSource.
  @@ -151,12 +171,27 @@
               }
               else {
                   String expandedSystemId = XMLEntityManager.expandSystemId(source.getSystemId(), source.getBaseSystemId(), false);
  +
                   URL url = new URL(expandedSystemId);
  +                URLConnection urlCon = url.openConnection();
  +				
  +                // If this is an HTTP connection attach any 
  +                // content negotation parameters to the request.
  +                if (urlCon instanceof HttpURLConnection) {
  +                    if( fAccept != null && fAccept.length() > 0) {
  +                        urlCon.setRequestProperty(XIncludeHandler.HTTP_ACCEPT, fAccept);
  +                    }
  +                    if( fAcceptCharset != null && fAcceptCharset.length() > 0) {
  +                        urlCon.setRequestProperty(XIncludeHandler.HTTP_ACCEPT_CHARSET, fAcceptCharset);
  +                    }
  +                    if( fAcceptLanguage != null && fAcceptLanguage.length() > 0) {
  +                        urlCon.setRequestProperty(XIncludeHandler.HTTP_ACCEPT_LANGUAGE, fAcceptLanguage);
  +                    }
  +                }
                   
                   // Wrap the InputStream so that it is possible to rewind it.
  -                stream = new BufferedInputStream(url.openStream());
  -                URLConnection urlCon = url.openConnection();
  -
  +                stream = new BufferedInputStream(urlCon.getInputStream());
  +                
                   // content type will be string like "text/xml; charset=UTF-8" or "text/xml"
                   String rawContentType = urlCon.getContentType();
                   
  
  
  
  1.16      +18 -33    xml-xerces/java/src/org/apache/xerces/xinclude/XIncludeHandler.java
  
  Index: XIncludeHandler.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/xinclude/XIncludeHandler.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- XIncludeHandler.java	2 Dec 2003 09:37:08 -0000	1.15
  +++ XIncludeHandler.java	21 Jan 2004 17:03:14 -0000	1.16
  @@ -161,6 +161,9 @@
       public final static String XINCLUDE_ATTR_HREF = "href".intern();
       public final static String XINCLUDE_ATTR_PARSE = "parse".intern();
       public final static String XINCLUDE_ATTR_ENCODING = "encoding".intern();
  +    public final static String XINCLUDE_ATTR_ACCEPT = "accept".intern();
  +    public final static String XINCLUDE_ATTR_ACCEPT_LANGUAGE = "accept-language".intern();
  +    public final static String XINCLUDE_ATTR_ACCEPT_CHARSET = "accept-charset".intern();    
   
       // Top Level Information Items have [included] property in infoset
       public final static String XINCLUDE_INCLUDED = "[included]".intern();
  @@ -1200,8 +1203,16 @@
                   false);
   
               try {
  +                // REVISIT: If we're going to support content negotation for
  +                // parse=xml we need to find a clean way to propogate the
  +                // parameters to the child parser. We cannot set those parameters
  +                // here because we may lose some information if we process the
  +                // URLConnection and then open a stream. The parser needs a
  +                // URLConnection to follow HTTP redirects and may also need
  +                // this object in the future to read external encoding information
  +                // which may be available in the Content-Type. -- mrglavas
  +
                   fNamespaceContext.pushScope();
  -				setHttpProperties(includedSource,attributes);
                   fChildConfig.parse(includedSource);
                   // necessary to make sure proper location is reported in errors
                   if (fErrorReporter != null) {
  @@ -1239,13 +1250,18 @@
   
               XIncludeTextReader reader = null;
               try {
  -				setHttpProperties(includedSource,attributes);
                   if (fIsXML11) {
                       reader = new XInclude11TextReader(includedSource, this);
                   }
                   else {
                       reader = new XIncludeTextReader(includedSource, this);
                   }
  +                if (includedSource.getCharacterStream() == null 
  +                    && includedSource.getByteStream() == null) {
  +                    reader.setHttpProperties(attributes.getValue(XINCLUDE_ATTR_ACCEPT), 
  +                        attributes.getValue(XINCLUDE_ATTR_ACCEPT_CHARSET), 
  +                        attributes.getValue(XINCLUDE_ATTR_ACCEPT_LANGUAGE));
  +                }
                   reader.setErrorReporter(fErrorReporter);
                   reader.parse();
               }
  @@ -2179,35 +2195,4 @@
               }
           }
       }
  -
  -	/**
  -		Set the Accept,Accept-Language,Accept-CharSet 
  -	*/
  -	protected void setHttpProperties(XMLInputSource source,XMLAttributes attributes) throws IOException{
  -        Reader reader = source.getCharacterStream();
  -        if (reader != null) 
  -			return; 
  -		String httpAcceptLang = attributes.getValue(HTTP_ACCEPT_LANGUAGE);
  -		String httpAccept = attributes.getValue(HTTP_ACCEPT);
  -		String httpAcceptchar = attributes.getValue(HTTP_ACCEPT_CHARSET);
  -		InputStream stream = source.getByteStream();
  -		if (stream == null) {
  -			String literalSystemId = source.getSystemId();
  -			String baseSystemId = source.getBaseSystemId();
  -        	String expandedSystemId = XMLEntityManager.expandSystemId(literalSystemId, 
  -													baseSystemId, false);
  -   	        URL location = new URL(expandedSystemId);
  -   	        URLConnection connect = location.openConnection();
  -			if (connect instanceof HttpURLConnection) {
  -				if( httpAcceptLang !=null && !httpAcceptLang.equals(""))
  -					connect.setRequestProperty(HTTP_ACCEPT_LANGUAGE,httpAcceptLang);
  -					if( httpAccept !=null && !httpAccept.equals(""))
  -						connect.setRequestProperty(HTTP_ACCEPT,httpAccept);
  -					if( httpAcceptchar !=null && !httpAcceptchar.equals(""))
  -						connect.setRequestProperty(HTTP_ACCEPT_CHARSET,httpAcceptchar);
  -   	         }
  -			stream = connect.getInputStream(); 
  -			source.setByteStream(stream);
  -		}
  -	}
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-cvs-help@xml.apache.org