1 23 24 package com.sun.enterprise.config.serverbeans.validation; 25 26 import java.io.FilterReader ; 27 import java.io.IOException ; 28 import java.io.Reader ; 29 import java.io.StringWriter ; 30 import javax.xml.parsers.ParserConfigurationException ; 31 import javax.xml.parsers.SAXParserFactory ; 32 import org.xml.sax.ContentHandler ; 33 import org.xml.sax.DTDHandler ; 34 import org.xml.sax.EntityResolver ; 35 import org.xml.sax.ErrorHandler ; 36 import org.xml.sax.InputSource ; 37 import org.xml.sax.SAXException ; 38 import org.xml.sax.SAXNotRecognizedException ; 39 import org.xml.sax.SAXNotSupportedException ; 40 import org.xml.sax.XMLFilter ; 41 import org.xml.sax.XMLReader ; 42 import org.xml.sax.helpers.XMLFilterImpl ; 43 import java.io.InputStreamReader ; 44 import com.sun.org.apache.xml.internal.serializer.ToXMLStream; 45 import java.io.StringReader ; 46 import java.io.BufferedWriter ; 47 48 52 53 63 66 public class VariableResolver extends XMLFilterImpl 67 { 68 69 private final FrameHolderBuilder frameHolderBuilder = new FrameHolderBuilder(); 70 private final VariableExpander ve = new VariableExpander(); 71 72 73 VariableResolver() throws SAXException { 74 super.setParent(getXMLReader()); 75 } 76 77 78 84 85 public void parse(InputSource is) throws IOException , SAXException { 86 final InputSource is2 = copyInputSource(is); 87 super.setContentHandler(frameHolderBuilder); 88 super.parse(is2); 89 is2.getCharacterStream().reset(); 90 ve.setFramer(new Framer(frameHolderBuilder.getFrameHolder())); 91 ve.setParent(getParent()); 92 ve.parse(is2); 93 } 94 95 private InputSource copyInputSource(InputSource is) throws IOException , SAXException { 96 final StringWriter out = new StringWriter (); 97 final ToXMLStream xml = new ToXMLStream(); 98 xml.setWriter(out); 99 final XMLReader r = getXMLReader(); 100 r.setContentHandler(xml); 101 if (super.getEntityResolver() != null){ 102 r.setEntityResolver(super.getEntityResolver()); 103 } 104 r.parse(is); 105 final InputSource result= new InputSource (new StringReader (out.toString()){ public void close(){} }); 106 result.setPublicId(is.getPublicId()); 107 result.setSystemId(is.getSystemId()); 108 return result; 109 } 110 111 private XMLReader getXMLReader() throws SAXException { 112 try{ 113 final XMLReader xr = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); 114 xr.setFeature("http://xml.org/sax/features/namespaces", true); 115 xr.setFeature("http://xml.org/sax/features/namespace-prefixes", false); 116 return xr; 117 } 118 catch (ParserConfigurationException e){ 119 throw new SAXException (e); 120 } 121 } 122 123 public void setContentHandler(ContentHandler contentHandler) { 124 ve.setContentHandler(contentHandler); 125 } 126 127 } 128 | Popular Tags |