/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @author Igor A. Pyankov
*/
package org.apache.harmony.x.print.cups;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Vector;
import javax.print.DocFlavor;
import javax.print.MultiDocPrintService;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.AttributeSet;
import org.apache.harmony.x.print.DefaultPrintService;
import org.apache.harmony.x.print.ipp.IppAttribute;
import org.apache.harmony.x.print.ipp.IppAttributeGroup;
import org.apache.harmony.x.print.ipp.IppClient;
import org.apache.harmony.x.print.ipp.IppOperation;
import org.apache.harmony.x.print.ipp.IppPrinter;
import org.apache.harmony.x.print.ipp.IppRequest;
import org.apache.harmony.x.print.ipp.IppResponse;
/*
* The class extends PrintServiceLookup and is intended for
* looking up CUPS/IPP printers
*
* 1. The class allways looks printers on http://localhost:631
* This URL is default URL for default installation of CUPS server
* 2. The class accepts two properties:
* print.cups.servers - a list of CUPS servers
* print.ipp.printers - a list of IPP printers
* (note, that CUPS printer is IPP printer too)
*/
public class CUPSPrintServiceProvider extends PrintServiceLookup {
private static String cupsdefault = "http://localhost:631";
private static ArrayList services = new ArrayList();
/*
* 0 - no
* 1 - more
* 2 - more and more
* ...
*/
private static int verbose = 0;
static {
String verbose_property = (String) AccessController
.doPrivileged(new PrivilegedAction() {
public Object run() {
return System.getProperty("print.cups.verbose");
}
});
if (verbose_property != null) {
try {
Integer v = new Integer(verbose_property);
setVerbose(v.intValue());
} catch (NumberFormatException e) {
setVerbose(0);
}
}
}
public CUPSPrintServiceProvider() {
super();
}
/*
* The method returns array of URLs of CUPS servers
*/
private static String[] getCUPSServersByProperty() {
ArrayList cupslist = new ArrayList();
cupslist.add(cupsdefault);
String cupspath = (String) AccessController
.doPrivileged(new PrivilegedAction() {
public Object run() {
return System.getProperty("print.cups.servers");
}
});
String pathsep = ",";
if (cupspath != null && !cupspath.equals("")) {
String[] cupss = cupspath.split(pathsep);
for (int i = 0, ii = cupss.length; i < ii; i++) {
if (!cupss[i].equals("")) {
try {
URI cupsuri = new URI(cupss[i]);
cupslist.add(cupsuri.toString());
} catch (URISyntaxException e) {
if (verbose > 0) {
System.err.println("CUPS url: " + cupss[i]);
e.printStackTrace();
} else {
// IGNORE bad URI exception
}
}
}
}
}
return (String[]) cupslist.toArray(new String[0]);
}
/*
* The method returns array of URLs of IPP printers
*/
private static String[] getIppPrintersByProperty() {
ArrayList ipplist = new ArrayList();
String ipppath = (String) AccessController
.doPrivileged(new PrivilegedAction() {
public Object run() {
return System.getProperty("print.ipp.printers");
}
});
String pathsep = ","; //System.getProperty("path.separator");
if (ipppath != null && !ipppath.equals("")) {
String[] ipps = ipppath.split(pathsep);
for (int i = 0, ii = ipps.length; i < ii; i++) {
if (!ipps[i].equals("")) {
try {
URI cupsuri = new URI(ipps[i]);
ipplist.add(cupsuri.toString());
} catch (URISyntaxException e) {
if (verbose > 0) {
System.err.println("IPP url: " + ipps[i]);
e.printStackTrace();
} else {
// IGNORE bad URI exception
}
}
}
}
}
return (String[]) ipplist.toArray(new String[0]);
}
/*
* @see javax.print.PrintServiceLookup#getDefaultPrintService()
*/
public PrintService getDefaultPrintService() {
synchronized (this) {
String defaultService = findDefaultPrintService();
if (defaultService != null) {
PrintService service = getServiceStored(defaultService,
services);
if (service != null) {
return service;
}
CUPSClient client;
try {
client = new CUPSClient(defaultService);
service = new DefaultPrintService(defaultService, client);
services.add(service);
return service;
} catch (PrintException e) {
// just ignore
e.printStackTrace();
}
}
if (services.size() == 0) {
getPrintServices();
}
if (services.size() > 0) {
return (PrintService) services.get(0);
}
}
return null;
}
/*
* @see javax.print.PrintServiceLookup#getPrintServices()
*/
public PrintService[] getPrintServices() {
synchronized (this) {
String[] serviceNames = findPrintServices();
if (serviceNames == null || serviceNames.length == 0) {
services.clear();
return new PrintService[0];
}
ArrayList newServices = new ArrayList();
for (int i = 0; i < serviceNames.length; i++) {
PrintService service = getServiceStored(serviceNames[i],
services);
if (service != null) {
newServices.add(service);
} else if (getServiceStored(serviceNames[i], newServices) == null) {
try {
CUPSClient client = new CUPSClient(serviceNames[i]);
service = new DefaultPrintService(serviceNames[i],
client);
newServices.add(service);
} catch (PrintException e) {
// just ignore
e.printStackTrace();
}
}
}
services.clear();
services = newServices;
return (services.size() == 0) ? new PrintService[0]
: (PrintService[]) services.toArray(new PrintService[0]);
}
}
/*
* find printers on particular CUPS server
*/
private PrintService[] getCUPSPrintServices(String cups) {
synchronized (this) {
// just update static field 'services'
findPrintServices();
// next find services on server 'cups'
String[] serviceNames = (String[]) findCUPSPrintServices(cups)
.toArray(new String[0]);
if (serviceNames == null || serviceNames.length == 0) {
return new PrintService[0];
}
// return only those are stored in field 'services'
ArrayList newServices = new ArrayList();
for (int i = 0; i < serviceNames.length; i++) {
PrintService service = getServiceStored(serviceNames[i],
services);
if (service != null) {
newServices.add(service);
}
}
return (newServices.size() == 0) ? new PrintService[0]
: (PrintService[]) services.toArray(new PrintService[0]);
}
}
/*
* find printers on localhost only
*/
public PrintService[] getPrintServicesOnLocalHost() {
return getCUPSPrintServices(cupsdefault);
}
/*
* find service which name is same as serviceName
*/
private PrintService getServiceStored(String serviceName,
ArrayList servicesList) {
for (int i = 0; i < servicesList.size(); i++) {
PrintService service = (PrintService) servicesList.get(i);
if (service.getName().equals(serviceName)) {
return service;
}
}
return null;
}
/*
* @see javax.print.PrintServiceLookup#getPrintServices(javax.print.DocFlavor
* , javax.print.attribute.AttributeSet)
*/
public PrintService[] getPrintServices(DocFlavor flavor,
AttributeSet attributes) {
PrintService[] cupsservices = getPrintServices();
if (flavor == null && attributes == null) {
return cupsservices;
}
ArrayList requestedServices = new ArrayList();
for (int i = 0; i < cupsservices.length; i++) {
try {
AttributeSet unsupportedSet = cupsservices[i]
.getUnsupportedAttributes(flavor, attributes);
if (unsupportedSet == null) {
requestedServices.add(cupsservices[i]);
}
} catch (IllegalArgumentException iae) {
// DocFlavor not supported by service, skiping.
}
}
return (requestedServices.size() == 0) ? new PrintService[0]
: (PrintService[]) requestedServices
.toArray(new PrintService[0]);
}
/*
* @see javax.print.PrintServiceLookup#getMultiDocPrintServices(javax.print.DocFlavor[]
* , javax.print.attribute.AttributeSet)
*/
public MultiDocPrintService[] getMultiDocPrintServices(DocFlavor[] flavors,
AttributeSet attributes) {
// No multidoc print services available, yet.
return new MultiDocPrintService[0];
}
/*
* find all printers
*/
private static String[] findPrintServices() {
ArrayList ippservices = new ArrayList();
/*
* First, find on localhost and servers from print.cups.servers property
* and add them to full list
*/
String[] cupses = CUPSPrintServiceProvider.getCUPSServersByProperty();
for (int j = 0; j < cupses.length; j++) {
ippservices.addAll(findCUPSPrintServices(cupses[j]));
}
/*
* Then, check URLs from print.ipp.printers property and
* if is valid ipp printer add them to full list
*/
String[] ippp = CUPSPrintServiceProvider.getIppPrintersByProperty();
for (int j = 0; j < ippp.length; j++) {
try {
URI ippuri = new URI(ippp[j]);
IppPrinter printer = new IppPrinter(ippuri);
IppResponse response;
response = printer.requestPrinterAttributes(
"printer-uri-supported", null);
Vector gg = response
.getGroupVector(IppAttributeGroup.TAG_GET_PRINTER_ATTRIBUTES);
if (gg != null) {
for (int i = 0, ii = gg.size(); i < ii; i++) {
IppAttributeGroup g = (IppAttributeGroup) gg.get(i);
int ai = g.findAttribute("printer-uri-supported");
if (ai >= 0) {
IppAttribute a = (IppAttribute) g.get(ai);
Vector v = a.getValue();
if (v.size() > 0) {
ippservices.add(new String((byte[]) v.get(0)));
}
}
}
}
} catch (Exception e) {
if (verbose > 0) {
System.err.println("IPP url: " + ippp[j]);
e.printStackTrace();
} else {
// IGNORE - connection refused due to no server, etc.
}
}
}
// return array of printers
return (String[]) ippservices.toArray(new String[0]);
}
/*
* find ipp printers on CUPS server 'cups'
*/
public static ArrayList findCUPSPrintServices(String cups) {
ArrayList ippservices = new ArrayList();
URI cupsuri = null;
IppClient c = null;
IppRequest request;
IppResponse response;
IppAttributeGroup agroup;
Vector va = new Vector();
request = new IppRequest(1, 1, IppOperation.TAG_CUPS_GET_PRINTERS,
"utf-8", "en-us");
agroup = request.getGroup(IppAttributeGroup.TAG_OPERATION_ATTRIBUTES);
va.add("printer-uri-supported".getBytes());
agroup.add(new IppAttribute(IppAttribute.TAG_KEYWORD,
"requested-attributes", va));
try {
cupsuri = new URI(cups);
c = new IppClient(cupsuri);
response = c.request(request.getBytes());
Vector gg = response
.getGroupVector(IppAttributeGroup.TAG_GET_PRINTER_ATTRIBUTES);
if (gg != null) {
for (int i = 0, ii = gg.size(); i < ii; i++) {
IppAttributeGroup g = (IppAttributeGroup) gg.get(i);
int ai = g.findAttribute("printer-uri-supported");
if (ai >= 0) {
IppAttribute a = (IppAttribute) g.get(ai);
Vector v = a.getValue();
if (v.size() > 0) {
ippservices.add(new String((byte[]) v.get(0)));
}
}
}
}
} catch (Exception e) {
if (verbose > 0) {
System.err.println("CUPS url: " + cups);
System.err.println("CUPS uri: " + cupsuri);
System.err.println("Ipp client: " + c);
System.err.println(request.toString());
e.printStackTrace();
} else {
// IGNORE - connection refused due to no server, etc.
}
}
return ippservices;
}
/*
* find default printer
* At first, try to find default printer on CUPS servers and return first found
* If failed, return first found IPP printer
* If failed return null
*/
private static String findDefaultPrintService() {
String serviceName = null;
String[] cupses = CUPSPrintServiceProvider.getCUPSServersByProperty();
for (int i = 0; i < cupses.length; i++) {
try {
URI cupsuri = new URI(cupses[i]);
IppClient c = new IppClient(cupsuri);
IppRequest request;
IppResponse response;
IppAttributeGroup agroup;
Vector va = new Vector();
request = new IppRequest(1, 1,
IppOperation.TAG_CUPS_GET_DEFAULT, "utf-8", "en-us");
agroup = request
.getGroup(IppAttributeGroup.TAG_OPERATION_ATTRIBUTES);
va.add("printer-uri-supported".getBytes());
agroup.add(new IppAttribute(IppAttribute.TAG_KEYWORD,
"requested-attributes", va));
response = c.request(request.getBytes());
IppAttributeGroup g = response
.getGroup(IppAttributeGroup.TAG_GET_PRINTER_ATTRIBUTES);
if (g != null) {
int ai = g.findAttribute("printer-uri-supported");
if (ai >= 0) {
IppAttribute a = (IppAttribute) g.get(ai);
Vector v = a.getValue();
if (v.size() > 0) {
serviceName = new String((byte[]) v.get(0));
break;
}
}
}
} catch (URISyntaxException e) {
//e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
//e.printStackTrace();
}
}
if (serviceName != null && !serviceName.equals("")) {
return serviceName;
}
String[] ippp = CUPSPrintServiceProvider.getIppPrintersByProperty();
for (int i = 0; i < ippp.length; i++) {
try {
URI ippuri = new URI(ippp[i]);
IppClient c = new IppClient(ippuri);
IppRequest request;
IppResponse response;
IppAttributeGroup agroup;
Vector va = new Vector();
request = new IppRequest(1, 1,
IppOperation.GET_PRINTER_ATTRIBUTES, "utf-8", "en-us");
agroup = request
.getGroup(IppAttributeGroup.TAG_OPERATION_ATTRIBUTES);
va.add("printer-uri-supported".getBytes());
agroup.add(new IppAttribute(IppAttribute.TAG_KEYWORD,
"requested-attributes", va));
response = c.request(request.getBytes());
IppAttributeGroup g = response
.getGroup(IppAttributeGroup.TAG_GET_PRINTER_ATTRIBUTES);
if (g != null) {
int ai = g.findAttribute("printer-uri-supported");
if (ai >= 0) {
IppAttribute a = (IppAttribute) g.get(ai);
Vector v = a.getValue();
if (v.size() > 0) {
serviceName = new String((byte[]) v.get(0));
break;
}
}
}
} catch (URISyntaxException e) {
//e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
//e.printStackTrace();
}
}
return serviceName;
}
public static int isVerbose() {
return verbose;
}
public static void setVerbose(int newverbose) {
CUPSPrintServiceProvider.verbose = newverbose;
CUPSClient.setVerbose(newverbose);
}
}
分享到:
相关推荐
ta_lib-0.5.1-cp312-cp312-win32.whl
课程设计 在线实时的斗兽棋游戏,时间赶,粗暴的使用jQuery + websoket 实现实时H5对战游戏 + java.zip课程设计
ta_lib-0.5.1-cp310-cp310-win_amd64.whl
基于springboot+vue物流系统源码数据库文档.zip
GEE训练教程——Landsat5、8和Sentinel-2、DEM和各2哦想指数下载
知识图谱
333498005787635解决keil下载失败的文件.zip
【微信机器人原理与实现】 微信机器人是通过模拟微信客户端的行为,自动处理消息、发送消息的程序。在Python中实现微信机器人的主要库是WeChatBot,它提供了丰富的接口,允许开发者方便地进行微信消息的接收与发送。这个项目标题中的"基于python实现的微信机器人源码"指的是使用Python编程语言编写的微信机器人程序。 1. **Python基础**:Python是一种高级编程语言,以其简洁的语法和强大的功能深受开发者喜爱。在实现微信机器人时,你需要熟悉Python的基本语法、数据类型、函数、类以及异常处理等概念。 2. **微信API与WeChatBot库**:微信为开发者提供了微信公共平台和微信开放平台,可以获取到必要的API来实现机器人功能。WeChatBot库是Python中一个用于微信开发的第三方库,它封装了微信的API,简化了消息处理的流程。使用WeChatBot,开发者可以快速搭建起一个微信机器人。 3. **微信OAuth2.0授权**:为了能够接入微信,首先需要通过OAuth2.0协议获取用户的授权。用户授权后,机器人可以获取到微信用户的身份信息,从而进行
基于springboot实验室研究生信息管理系统源码数据库文档.zip
张力控制,色标跟踪,多轴同步,电子凸轮,横切等工艺控制案例。
在Python编程环境中,处理Microsoft Word文档是一项常见的任务。Python提供了几个库来实现这一目标,如`python-docx`,它可以让我们创建、修改和操作.docx文件。本教程将重点介绍如何利用Python进行Word文档的合并、格式转换以及转换为PDF。 1. **合并Word文档(merge4docx)** 合并多个Word文档是一项实用的功能,特别是在处理大量报告或文档集合时。在Python中,可以使用`python-docx`库实现。我们需要导入`docx`模块,然后读取每个文档并将其内容插入到主文档中。以下是一个基本示例: ```python from docx import Document def merge4docx(file_list, output_file): main_doc = Document() for file in file_list: doc = Document(file) for paragraph in doc.paragraphs: main_doc.add_paragraph(paragraph.text) m
基于springboot+Javaweb的二手图书交易系统源码数据库文档.zip
基于springboot餐品美食论坛源码数据库文档.zip
基于springboot亚运会志愿者管理系统源码数据库文档.zip
使用WPF的数据样式绑定,切换对象数据值来完成控件动态切换背景渐变动画效果。 使用动画样式渲染比线程修改性能消耗更低更稳定
基于SpringBoot的企业客源关系管理系统源码数据库文档.zip
基于springboot+vue的桂林旅游网站系统源码数据库文档.zip
基于springboot嗨玩旅游网站源码数据库文档.zip
基于springboot的流浪动物管理系统源码数据库文档.zip
基于springboot课件通中小学教学课件共享平台源码数据库文档.zip