LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / reportbuilder / java / org / libreoffice / report / SOImageService.java
blobf472d284d02b81d527edfa2c38d95ead535a7f48
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package org.libreoffice.report;
20 import com.sun.star.awt.Size;
21 import com.sun.star.beans.PropertyValue;
22 import com.sun.star.beans.UnknownPropertyException;
23 import com.sun.star.beans.XPropertySet;
24 import com.sun.star.beans.XPropertySetInfo;
25 import com.sun.star.graphic.XGraphicProvider;
26 import com.sun.star.io.IOException;
27 import com.sun.star.io.XInputStream;
28 import com.sun.star.lang.WrappedTargetException;
29 import com.sun.star.lang.XMultiComponentFactory;
30 import com.sun.star.lib.uno.adapter.ByteArrayToXInputStreamAdapter;
31 import com.sun.star.uno.UnoRuntime;
32 import com.sun.star.uno.XComponentContext;
35 public class SOImageService implements ImageService
38 private final XGraphicProvider m_xGraphicProvider;
40 /**
41 * Creates a new instance of SOImageService
43 public SOImageService(final XComponentContext xCompContext)
44 throws ReportExecutionException, com.sun.star.uno.Exception
46 if (xCompContext == null)
48 throw new ReportExecutionException("SOImageService constructed with null Component Context");
52 final XMultiComponentFactory xMCF = xCompContext.getServiceManager();
53 m_xGraphicProvider = UnoRuntime.queryInterface(XGraphicProvider.class,
54 xMCF.createInstanceWithContext("com.sun.star.graphic.GraphicProvider", xCompContext));
56 if (m_xGraphicProvider == null)
58 throw new ReportExecutionException("There is no graphic-provider available for SOImageService.");
62 private Size getImageSize(final XInputStream image) throws ReportExecutionException
64 final Size dim = new Size();
65 try
67 final PropertyValue[] value = new PropertyValue[]
69 new PropertyValue()
71 value[0].Name = "InputStream";
72 value[0].Value = image;
74 final XPropertySet xImage = UnoRuntime.queryInterface(XPropertySet.class,
75 m_xGraphicProvider.queryGraphic(value));
77 if (xImage != null)
79 final XPropertySetInfo xInfo = xImage.getPropertySetInfo();
80 if (xInfo.hasPropertyByName("Size100thMM"))
82 Size imageSize = (Size) xImage.getPropertyValue("Size100thMM");
83 dim.Width = imageSize.Width;
84 dim.Height = imageSize.Height;
85 if (dim.Height == 0 && dim.Width == 0)
87 imageSize = (Size) xImage.getPropertyValue("SizePixel");
88 final int dpi = java.awt.Toolkit.getDefaultToolkit().getScreenResolution();
89 final double fac = 2540 / (double) dpi;
90 dim.Width = (int) (imageSize.Width * fac);
91 dim.Height = (int) (imageSize.Height * fac);
94 else if (xInfo.hasPropertyByName("SizePixel"))
96 final Size imageSize = (Size) xImage.getPropertyValue("SizePixel");
97 final int dpi = java.awt.Toolkit.getDefaultToolkit().getScreenResolution();
98 final double fac = 2540.0 / dpi;
99 dim.Width = (int) (imageSize.Width * fac);
100 dim.Height = (int) (imageSize.Height * fac);
104 catch (Exception ex)
106 throw new ReportExecutionException("Failed to query Image-Size", ex);
108 return dim;
111 public Size getImageSize(final byte[] image) throws ReportExecutionException
113 return getImageSize(new ByteArrayToXInputStreamAdapter(image));
116 private String getMimeType(final XInputStream image) throws ReportExecutionException
120 final PropertyValue[] value = new PropertyValue[]
122 new PropertyValue()
124 value[0].Name = "InputStream";
125 value[0].Value = image;
127 final XPropertySet xImage = UnoRuntime.queryInterface(XPropertySet.class,
128 m_xGraphicProvider.queryGraphic(value));
130 if (xImage != null)
132 final XPropertySetInfo xInfo = xImage.getPropertySetInfo();
133 if (xInfo.hasPropertyByName("MimeType"))
135 return (String) xImage.getPropertyValue("MimeType");
139 catch (UnknownPropertyException ex)
141 throw new ReportExecutionException(ex);
143 catch (WrappedTargetException ex)
145 throw new ReportExecutionException(ex);
147 catch (com.sun.star.lang.IllegalArgumentException ex)
149 throw new ReportExecutionException(ex);
151 catch (IOException ex)
153 throw new ReportExecutionException(ex);
155 return null;
158 public String getMimeType(final byte[] image) throws ReportExecutionException
160 return getMimeType(new ByteArrayToXInputStreamAdapter(image));