X-Git-Url: http://git.osdn.net/view?p=mikutoga%2FPmd2XML.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fjp%2Fsfjp%2Fmikutoga%2Fpmd2xml%2FXmlInputUtil.java;h=f413cf158dc3ad8f94a649fcbd6980aa2fdb934d;hp=91d04067c38e7216a1bf486b240e1721cb2251af;hb=ceccd4a854175208e28020ab78e5015a60bb3a57;hpb=cc1d9977ddd7cc8a44b5ea4ee6217167c8a8f9a9 diff --git a/src/main/java/jp/sfjp/mikutoga/pmd2xml/XmlInputUtil.java b/src/main/java/jp/sfjp/mikutoga/pmd2xml/XmlInputUtil.java index 91d0406..f413cf1 100644 --- a/src/main/java/jp/sfjp/mikutoga/pmd2xml/XmlInputUtil.java +++ b/src/main/java/jp/sfjp/mikutoga/pmd2xml/XmlInputUtil.java @@ -7,6 +7,13 @@ package jp.sfjp.mikutoga.pmd2xml; +import java.io.BufferedInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URL; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; @@ -17,6 +24,7 @@ import jp.sfjp.mikutoga.xml.BotherHandler; import jp.sfjp.mikutoga.xml.LocalXmlResource; import jp.sfjp.mikutoga.xml.SchemaUtil; import jp.sfjp.mikutoga.xml.XmlResourceResolver; +import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; @@ -35,6 +43,60 @@ final class XmlInputUtil { /** + * å®å¨ãã¡ã¤ã«ããXMLå ¥åã½ã¼ã¹ãå¾ãã + * @param file å®å¨ãã¡ã¤ã« + * @return XMLå ¥åã½ã¼ã¹ + */ + static InputSource fileToSource(File file){ + assert file.exists(); + + URI uri = file.toURI(); + + URL url; + try{ + url = uri.toURL(); + }catch(MalformedURLException e){ + // å®å¨Fileç±æ¥ã®URLã§ã¯èµ·ããããªã + assert false; + throw new AssertionError(e); + } + + String systemId = url.toString(); + + InputSource source = new InputSource(systemId); + + return source; + } + + /** + * InputSourceããInputStreamãå¾ãã + *
å ¥åã½ã¼ã¹ã«ã¯ãå°ãªãã¨ããã¤ãã¹ããªã¼ã ã + * URLæåå(SystemId)ã®ãããããè¨å®ããã¦ããªããã°ãªããªãã + * @param source å ¥åã½ã¼ã¹ + * @return å ¥åãã¤ãã¹ããªã¼ã + * @throws IllegalArgumentException å ¥åã½ã¼ã¹ã®è¨å®ã足ããªãã + * @throws IOException å ¥åã½ã¼ã¹ã«ã¢ã¯ã»ã¹ä¸è½ã + */ + static InputStream openInputSource(InputSource source) + throws IllegalArgumentException, IOException{ + InputStream is; + + is = source.getByteStream(); + + if(is == null){ + String systemId = source.getSystemId(); + if(systemId == null) throw new IllegalArgumentException(); + + URL url = new URL(systemId); + is = url.openStream(); + } + + is = new BufferedInputStream(is); + + return is; + } + + /** * SAXãã¼ãµãã¡ã¯ããªãçæããã *