Java源码示例:org.fit.cssbox.io.DefaultDocumentSource
示例1
public StyleImport(String urlstring) throws IOException, SAXException
{
//Open the network connection
DocumentSource docSource = new DefaultDocumentSource(urlstring);
//Parse the input document
DOMSource parser = new DefaultDOMSource(docSource);
doc = parser.parse();
//Create the CSS analyzer
DOMAnalyzer da = new DOMAnalyzer(doc, docSource.getURL());
da.getStyleSheets(); //load the author style sheets
da.localizeStyles(); //put the style sheets into the document header
docSource.close();
}
示例2
/**
* Creates a new config with default values of the options.
*/
public BrowserConfig()
{
viewportBackgroundColor = new Color(255, 255, 255);
loadImages = true;
loadBackgroundImages = true;
loadFonts = true;
imageLoadTimeout = 500;
useHTML = true;
replaceImagesWithAlt = false;
clipViewport = false;
documentSourceClass = DefaultDocumentSource.class;
domSourceClass = DefaultDOMSource.class;
contentObserver = null;
logicalFonts = getDefaultLogicalFonts();
}
示例3
/**
* @param args the command line arguments
*/
public static void main(String args[])
{
if (args.length != 1)
{
System.err.println("Usage: SimpleBrowser <url>");
System.exit(0);
}
try {
//Open the network connection
DocumentSource docSource = new DefaultDocumentSource(args[0]);
//Parse the input document
DOMSource parser = new DefaultDOMSource(docSource);
Document doc = parser.parse();
//Create the CSS analyzer
DOMAnalyzer da = new DOMAnalyzer(doc, docSource.getURL());
da.attributesToStyles(); //convert the HTML presentation attributes to inline styles
da.addStyleSheet(null, CSSNorm.stdStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the standard style sheet
da.addStyleSheet(null, CSSNorm.userStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the additional style sheet
da.addStyleSheet(null, CSSNorm.formsStyleSheet(), DOMAnalyzer.Origin.AGENT); //render form fields using css
da.getStyleSheets(); //load the author style sheets
//Display the result
SimpleBrowser test = new SimpleBrowser(da.getRoot(), docSource.getURL(), da);
test.setSize(1275, 750);
test.setVisible(true);
docSource.close();
} catch (Exception e) {
System.out.println("Error: "+e.getMessage());
e.printStackTrace();
}
}
示例4
private Document loadDocument(String urlstring) throws IOException, SAXException
{
docSource = new DefaultDocumentSource(urlstring);
parser = new DefaultDOMSource(docSource);
Document doc = parser.parse();
return doc;
}
示例5
private BufferedImage renderDocumentWithCssBox(String url) throws IOException, SAXException {
DocumentSource docSource = new DefaultDocumentSource(ensureUrlHasProtocol(url));
final Dimension windowSize = new Dimension(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
final String mediaType = "screen";
// Parse the input document
DOMSource parser = new DefaultDOMSource(docSource);
Document doc = parser.parse();
// Create the media specification
MediaSpec media = new MediaSpec(mediaType);
media.setDimensions(windowSize.width, windowSize.height);
media.setDeviceDimensions(windowSize.width, windowSize.height);
// Create the CSS analyzer
DOMAnalyzer analyzer = new DOMAnalyzer(doc, docSource.getURL());
analyzer.setMediaSpec(media);
// Convert the HTML presentation attributes to inline styles
analyzer.attributesToStyles();
// Use the standard style sheet
analyzer.addStyleSheet(null, CSSNorm.stdStyleSheet(), DOMAnalyzer.Origin.AGENT);
// Use the additional style sheet
analyzer.addStyleSheet(null, CSSNorm.userStyleSheet(), DOMAnalyzer.Origin.AGENT);
// Render form fields using css
analyzer.addStyleSheet(null, CSSNorm.formsStyleSheet(), DOMAnalyzer.Origin.AGENT);
// Load the author style sheets
analyzer.getStyleSheets();
BrowserCanvas contentCanvas = new BrowserCanvas(analyzer.getRoot(), analyzer, docSource.getURL());
// We have a correct media specification, do not update
contentCanvas.setAutoMediaUpdate(false);
contentCanvas.getConfig().setClipViewport(false);
contentCanvas.getConfig().setLoadImages(true);
contentCanvas.getConfig().setLoadBackgroundImages(true);
contentCanvas.getConfig().setImageLoadTimeout((int) TimeUnit.SECONDS.toMillis(IMAGE_LOADING_TIMEOUT));
contentCanvas.createLayout(windowSize);
return contentCanvas.getImage();
}
示例6
/**
* Renders the URL and prints the result to the specified output stream in the specified
* format.
* @param urlstring the source URL
* @param out output stream
* @return true in case of success, false otherwise
* @throws SAXException
*/
public boolean renderURL(String urlstring, OutputStream out) throws IOException, SAXException
{
if (!urlstring.startsWith("http:") &&
!urlstring.startsWith("https:") &&
!urlstring.startsWith("ftp:") &&
!urlstring.startsWith("file:"))
urlstring = "http://" + urlstring;
//Open the network connection
DocumentSource docSource = new DefaultDocumentSource(urlstring);
//Parse the input document
DOMSource parser = new DefaultDOMSource(docSource);
Document doc = parser.parse();
//create the media specification
MediaSpec media = new MediaSpec(mediaType);
media.setDimensions(windowSize.width, windowSize.height);
media.setDeviceDimensions(windowSize.width, windowSize.height);
//Create the CSS analyzer
DOMAnalyzer da = new DOMAnalyzer(doc, docSource.getURL());
da.setMediaSpec(media);
da.attributesToStyles(); //convert the HTML presentation attributes to inline styles
da.addStyleSheet(null, CSSNorm.stdStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the standard style sheet
da.addStyleSheet(null, CSSNorm.userStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the additional style sheet
da.addStyleSheet(null, CSSNorm.formsStyleSheet(), DOMAnalyzer.Origin.AGENT); //render form fields using css
da.getStyleSheets(); //load the author style sheets
GraphicsEngine contentCanvas = new GraphicsEngine(da.getRoot(), da, docSource.getURL());
contentCanvas.setAutoMediaUpdate(false); //we have a correct media specification, do not update
contentCanvas.getConfig().setClipViewport(cropWindow);
contentCanvas.getConfig().setLoadImages(loadImages);
contentCanvas.getConfig().setLoadBackgroundImages(loadBackgroundImages);
contentCanvas.createLayout(windowSize);
ImageIO.write(contentCanvas.getImage(), "png", out);
docSource.close();
return true;
}
示例7
/**
* @param args
*/
public static void main(String[] args)
{
if (args.length != 2)
{
System.err.println("Usage: ComputeStyles <url> <output_file>");
System.exit(0);
}
try {
//Open the network connection
DocumentSource docSource = new DefaultDocumentSource(args[0]);
//Parse the input document
DOMSource parser = new DefaultDOMSource(docSource);
Document doc = parser.parse();
//Create the CSS analyzer
DOMAnalyzer da = new DOMAnalyzer(doc, docSource.getURL());
da.attributesToStyles(); //convert the HTML presentation attributes to inline styles
da.addStyleSheet(null, CSSNorm.stdStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the standard style sheet
da.addStyleSheet(null, CSSNorm.userStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the additional style sheet
da.getStyleSheets(); //load the author style sheets
//Compute the styles
System.err.println("Computing style...");
da.stylesToDomInherited();
//Save the output
OutputStream os = new FileOutputStream(args[1]);
Output out = new NormalOutput(doc);
out.dumpTo(os);
os.close();
docSource.close();
System.err.println("Done.");
} catch (Exception e) {
System.err.println("Error: "+e.getMessage());
e.printStackTrace();
}
}
示例8
/**
* main method
*/
public static void main(String[] args)
{
if (args.length != 1)
{
System.err.println("Usage: TextBoxes <url>");
System.exit(0);
}
try {
//Open the network connection
DocumentSource docSource = new DefaultDocumentSource(args[0]);
//Parse the input document
DOMSource parser = new DefaultDOMSource(docSource);
Document doc = parser.parse();
//Create the CSS analyzer
DOMAnalyzer da = new DOMAnalyzer(doc, docSource.getURL());
da.attributesToStyles(); //convert the HTML presentation attributes to inline styles
da.addStyleSheet(null, CSSNorm.stdStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the standard style sheet
da.addStyleSheet(null, CSSNorm.userStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the additional style sheet
da.getStyleSheets(); //load the author style sheets
//Create the browser canvas
GraphicsEngine browser = new GraphicsEngine(da.getRoot(), da, docSource.getURL());
//Disable the image loading
browser.getConfig().setLoadImages(false);
browser.getConfig().setLoadBackgroundImages(false);
//Create the layout for 1000x600 pixels
browser.createLayout(new Dimension(1000, 600));
//Display the result
printTextBoxes(browser.getViewport());
docSource.close();
} catch (Exception e) {
System.err.println("Error: "+e.getMessage());
e.printStackTrace();
}
}