Jsp My Titles4Java

From Fxp Wiki

Jump to: navigation, search
 
package vendo;
 
import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import java.io.*;
import java.util.*;
 
public class MyTitles4 extends DefaultHandler {
 
private Writer out;
	String monfile;
boolean local0 = false;
int counter=0;
String[] result = new String[12];
String[] inpt = new String[12];
String[] outp = new String[12];
	// constructor
    	public MyTitles4 (String xmlFile, Writer out) throws SAXException, IOException {
 
        		this.out = out;
		monfile = xmlFile;
        		//  create a Xerces SAX parser
        		SAXParser parser = new SAXParser();
 
        		//  set the content handler
        		parser.setContentHandler(this);
        		//  parse the document
        		try {
            			parser.parse(xmlFile);
			out.flush();
        		} catch (IOException e) {
            			// something went wrong!
        		}
			catch (SAXException e) {
            			// something went wrong!
        		}
    	}
 
	// call at document start
	public void startDocument() throws SAXException {
 
		try {
			out.write ("<h3>" + monfile + "</h3>");
		}
		catch (Exception e) {
                               			 System.err.println(" You generated an exception 1 :" +e);
			} 
 
	}
 
	// call at element start
    	public void startElement (String uri, String local, String qName,
Attributes atts) throws SAXException {
 
		try {
			//out.write ("<br /> counter:" + counter );
			//out.write (qName);
			counter++;
			//out.write (" counter:" + counter );
			//out.write (" local0:" + local0 + "<br />");
			if (local.equals("div")) {
				
				String AttributeName,AttributeType,AttributeValue = "";
				for (int i = 0; i < atts.getLength(); i++) {
					AttributeName = atts.getLocalName(i);
					AttributeType = atts.getType(AttributeName);
					AttributeValue = atts.getValue(AttributeName);
					//out.write ("<br /><b>Attr: </b>" + " Name:" + AttributeName + " Type:" + AttributeType + " Value:" + AttributeValue + "<br/>");
					if ((AttributeName.equals("class")) && (AttributeValue.equals("titles"))){
						local0 = true;
						//counter = 0;
						out.write ("&lt;rdf:Description&gt; \n" );
						out.write ("&lt;dc:title&gt; \n" );
						out.write ("&lt;rdf:Alt&gt; \n" );						
						outp[counter]="&lt;/rdf:Alt&gt; \n &lt;/dc:title&gt; \n &lt;/rdf:Description&gt; \n";
 
					}
				}
			}
			else {
				if (local0) {
					String AttributeName,AttributeType,AttributeValue,AttributeUri,AttributeFullName = "";
					for (int i = 0; i < atts.getLength(); i++) {
						AttributeName = atts.getLocalName(i);
						AttributeUri = atts.getURI(i);
						AttributeType = atts.getType(AttributeName);
						AttributeValue = atts.getValue(AttributeName);
						//out.write ("<br /><b>Attr: </b>" + " Name:" + AttributeName + " URI : " + AttributeUri + " Type:" + AttributeType + " Value:" + AttributeValue + "<br/>");
						if ((AttributeName.equals("lang")) && (AttributeUri.equals("http://www.w3.org/XML/1998/namespace")) ) {
							out.write ("&lt;rdf:Li&gt; \n" );
							out.write ("<b>" + AttributeValue + " : </b> " );
							outp[counter]="&lt;/rdf:Li&gt; \n";
						}
						/*if ((AttributeName.equals("class")) && (AttributeValue.equals("lgfr"))){
							out.write ("&lt;rdf:Li&gt; \n" );
							out.write ("Fran�ais : " );
							outp[counter]="&lt;/rdf:Li&gt; \n";
							out.write (" TEST : "+ outp[counter] );
						
						}
						*/
					}
				}
			}
		     } catch (Exception e) {
			 System.err.println(" You generated an exception 2 :" +e);
			}
	}
	// call when cdata found
    	public void characters(char[] text, int start, int length) throws SAXException {
 
		try {
			if (local0){
			String Content = new String(text, start, length);
	        		if (!Content.trim().equals("")){
				out.write(Content);
				}
			}
		} catch (Exception e) {
			System.err.println(" You generated an exception 3 :" +e);
		}
    	}
 
	// call at element end
	public void endElement (String uri, String local, String qName) throws SAXException {
		try {
			if (local0) {
				//out.write (" closing " + counter +" ");
				if (!(outp[counter]).equals("") ) {
				out.write (outp[counter]);
				}
				else {
				//out.write (" outp[] is Null ");
				}
				//out.write (" closed ");
					counter--;
					if (counter == 1){
						throw new SAXException("finished");
					}
			}
		} catch (IOException e) {
			           			 System.err.println(" You generated an exception 4: " +e);
		}
	}
 
	// call at document end
	public void endDocument() {
		try {
			//out.write ("<h1>Document ends</h1><br>");
		} catch (Exception e) {
			           			 System.err.println(" You generated an exception 5: " +e);
		}
	}
}
 
 
Personal tools