| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 
 | public class LoadXSLTinWebview extends Activity {
 
 WebView webview;
 /** Called when the activity is first created. */
   @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_PROGRESS);
    WebView webview = new WebView(this);
    setContentView(webview);
     webview.loadUrl("C:/Users/Admin/AndroidStudioProjects/Oranguide/app/src/main/assets/cin2.css");
    //Reading XSLT
    String strXSLT = GetStyleSheet(R.raw.cinemaxsl);
    //Reading XML
    String strXML = GetStyleSheet(R.raw.cinemaxml);
    /*
     * Loading XSLT...
     */
    //Transform ...
    String html=StaticTransform(strXSLT, strXML);
    //Loading the above transformed XSLT in to Webview...
    webview.loadData(html,"text/html","utf-8");
}
 
/*
 * Transform XSLT to HTML string
 */
public static String StaticTransform(String strXsl, String strXml) {
    String html = "";
 
    try {
 
        InputStream ds = null;
        ds = new ByteArrayInputStream(strXml.getBytes("UTF-8"));
 
        Source xmlSource = new StreamSource(ds);
 
        InputStream xs = new ByteArrayInputStream(strXsl.getBytes("UTF-8"));
        Source xsltSource = new StreamSource(xs);
 
        StringWriter writer = new StringWriter();
        Result result = new StreamResult(writer);
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer(xsltSource);
        transformer.transform(xmlSource, result);
 
        html = writer.toString();
 
        ds.close();
        xs.close();
 
        xmlSource = null;
        xsltSource = null;
 
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
 
    return html;
}
 
/*
 * Read file from res/raw...
 */
private String GetStyleSheet(int fileId) {
    String strXsl = null;
 
    InputStream raw = getResources().openRawResource(fileId);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    int size = 0;
    // Read the entire resource into a local byte buffer.
    byte[] buffer = new byte[1024];
    try {
        while ((size = raw.read(buffer, 0, 1024)) >= 0) {
            outputStream.write(buffer, 0, size);
        }
        raw.close();
 
        strXsl = outputStream.toString();
 
        Log.v("Log", "xsl ==> " + strXsl);
    } catch (IOException e) {
        e.printStackTrace();
    }
 
    return strXsl;
 
}
 
} | 
Partager