1 2 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
   | package reference.util.export;
 
import java.util.Stack;
 
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.LocatorImpl;
 
import reference.util.iText.ITextFormat;
 
import com.lowagie.text.Document;
 
public class SimpleContentHandler implements ContentHandler {
 
		private Locator locator;
 
		private Document document;
 
		private Stack textStack; 
		private Stack baliseStack = new Stack(); 
 
		private boolean multiOpen = false;
		private boolean multiClose = false;
		private boolean closeToOpen = false;
		private boolean openToClose = false;
 
		private String partStr;
 
        public SimpleContentHandler(Document doc, Stack text, String partName ) {
        	super();
        	// On definit le locator par defaut.
        	locator = new LocatorImpl();
        	document = doc; 
        	textStack = text;  
        	partStr = partName;//initialisation du nom de la partie
        }
 
        public void setDocumentLocator(Locator value) {
        	locator =  value;
        }
 
        public void startDocument() throws SAXException {
        	}
 
        public void endDocument() throws SAXException {
        	}
 
        public void startPrefixMapping(String prefix, String URI) throws SAXException {
        	}
 
        public void endPrefixMapping(String prefix) throws SAXException {
        	}
 
        public void startElement(String nameSpaceURI, String localName, String rawName, Attributes attributs) throws SAXException {
 
        	if (multiOpen) textStack.add(""/*"multiOpen"*/);
        	else if (closeToOpen) textStack.add(""/*"closeToOpen"*/);
    		baliseStack.add(localName); 
    		ITextFormat.addBaliseList(baliseStack);
 
    		openToClose = true;
    		multiOpen = true;
    		closeToOpen = false;
    		multiClose = false;
 
            //System.out.println("Ouverture de la balise : '" + localName +"'"); 
        	/*if ( ! "".equals(nameSpaceURI)) { // espace de nommage particulier
        		System.out.println("  appartenant a l'espace de nom : "  + nameSpaceURI);
        	}*/   
    		for (int i = 0; i < attributs.getLength(); i++) { // on parcourt la liste des attributs 
    			//System.out.println( i +  attributs.getLocalName(i) + "' = '" + attributs.getValue(i) +"'");  
    		} 
        }
 
        public void endElement(String nameSpaceURI, String localName, String rawName) throws SAXException {
 
        	if (multiClose) textStack.add(""/*"miltiClose"*/);
        	else if (openToClose) textStack.add(""/*"openToClose"*/);
 
        	baliseStack.remove(baliseStack.lastElement());  
    		ITextFormat.addBaliseList(baliseStack); 
 
    		multiOpen = false;
    		closeToOpen = true;
    		multiClose = true;
    		openToClose = false;
 
        	//System.out.print("Fermeture de la balise : '" + localName + "'");
        	/*if ( ! "".equals(nameSpaceURI)) { // name space non null
        		System.out.print("appartenant a l'espace de nommage : " + localName);
        	}*/ 
        }
 
        public void characters(char[] ch, int start, int end) throws SAXException {
 
        	multiOpen = false;
        	closeToOpen = false;
        	multiClose = false;
        	openToClose = false;
 
        	String content = new String(ch, start, end);
                /* System.out.println("#PCDATA : '" + content + "'"); */
                textStack.add(content);
        }
 
        public void ignorableWhitespace(char[] ch, int start, int end) throws SAXException {
                System.out.println("espaces inutiles rencontres : ..." + new String(ch, start, end) +  "...");
        }
 
        public void processingInstruction(String target, String data) throws SAXException {
                System.out.println("Instruction de fonctionnement : " + target);
                System.out.println("  dont les arguments sont : " + data);
        }
 
        public void skippedEntity(String arg0) throws SAXException {}
 
} | 
Partager